netoops blog

Friday, 5 October 2012

Timing Operations in Visual Studio 2012

using System;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace SpeedTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            // set up the labels for the initial load
            lblTest1.Text = "Test 1:  Waiting...";
            lblTest2.Text = "Test 2:  Waiting...";
 
            // display whether or not the stop watch
            // supports high resolution counters
            if (Stopwatch.IsHighResolution)
                lblHighPrec.Text = "Stopwatch is high precision";
            else
                lblHighPrec.Text = "Stopwatch is not high precision";
        }
 
        private void btnTest_Click(object sender, EventArgs e)
        {
            // create a new instance of stopwatch and start it
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            // run the task
            RunTest1();
            //stop the stop watch
            sw1.Stop();
           
            // display the time required by the task to complete
            lblTest1.Text = "Test 1:  Elapsed time was "
                + sw1.Elapsed.Milliseconds.ToString() + " msec";
           
            // repeat the steps with the second task
            // so we can compare times
            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            RunTest2();
            sw2.Stop();
 
            lblTest2.Text = "Test 2:  Elapsed time was "
                + sw2.Elapsed.Milliseconds.ToString() + " msec";
           
        }
 
 
        /// <summary>
        /// Meaningless test 1
        /// </summary>
        public void RunTest1()
        {
            lblTest1.Text = "Test 1:  Running...";
 
            for (int i = 0; i < 100000; i++)
            {
                int test = int.Parse("105532");
            }
        }
 
 
        /// <summary>
        /// Meaningless test 2
        /// </summary>
        public void RunTest2()
        {
            lblTest2.Text = "Test 2:  Running...";
 
            for (int i = 0; i < 100000; i++)
            {
                int test = Convert.ToInt32("105532");
            }
        }
 
 
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...