Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to calculate execution time of all my project I know using this code
var watch = Stopwatch.StartNew();
// the code that you want to measure comes here
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
but I have
private void pb1_MouseClick(object sender, MouseEventArgs e)
       {   .....
         .... my sample code }

and other buttons to complete my work how can calculate execution time from pb1_MouseClick to the end of my work in buttons
Posted
Comments
Jason Gleim 15-Apr-15 15:02pm    
Your question isn't clear wrt what time interval you are trying to capture? Do you want just the execution time for a block of code (such as the block within the click event) or do you want the time the application is open or do you just want the combined time of several operations? Please clarify.
neveen neveen 15-Apr-15 15:30pm    
may project have event and many of buttons this buttons have some code to do some work I want to calculate execution time from the event when run with the time of execution code in buttons

Solution 1 missed one very important aspect: you need to exclude JIT-compilation from measurement. Please see: http://en.wikipedia.org/wiki/Just-in-time_compilation.

This is easy to do: you need to start time measuring when all the methods involved in the call after you start timing have been already called before at least once. For example, you can call everything to be timed at least once, and only then start your measurements.

Failure to do so is a very common mistake.

Another advice: collect enough statistics. The observed statistical dispersion of timing result can be surprisingly high, as timing depends on a lot of random factors.

—SA
 
Share this answer
 
v2
C#
private void pb1_MouseClick(object sender, MouseEventArgs e)
        {   
var watch = Stopwatch.StartNew();

          .... my sample code 
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
MessageBox.Show(elapsedMs.ToString());
}


If you want to execute how long it takes the user to interact with different buttons on the interface and stop the StopWatch after a process is complete then declare the StopWatch as a global and end it when necessary.
 
Share this answer
 
v3
Comments
neveen neveen 15-Apr-15 15:47pm    
@infamousjoe Ok I do this but I have this event and 3 buttons I put the watch = Stopwatch.StartNew(); in private void pb1_MouseClick(object sender, MouseEventArgs e) my execution should be 1- click event 2- click button1 3-click button2 4-click button3 in button3 I use watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
MessageBox.Show(elapsedMs.ToString()); is that right ???
Sergey Alexandrovich Kryukov 15-Apr-15 16:32pm    
I up-voted the answer, but one important point is missing, and this point is a very common mistake many developers make. Please see Solution 2.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900