Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the execution time of a method

Possible Duplicate:
How do I measure how long a function is running?

I have an I/O time-taking method which copies data from a location to another. What's the best and most real way of calculating the execution time? Thread? Timer? Stopwatch? Any other solution? I want the most exact one, and briefest as much as possible.

like image 228
Mahdi Tahsildari Avatar asked Dec 24 '12 09:12

Mahdi Tahsildari


People also ask

How do you calculate execution time?

The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.

How do you calculate time in Java?

currentTimeMillis(); long elapsedTime = end - start; In the example above, we're using the “System. currentTimeMillis()” static method. The method returns a long value, which refers to the number of milliseconds since January 1st, 1970, in UTC.

How does C++ calculate execution time?

measure execution time of a program. Using time() function in C & C++. time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Prototype / Syntax : time_t time(time_t *tloc);

How does Python calculate time of execution?

To measure time elapsed during program's execution, either use time. clock() or time. time() functions. The python docs state that this function should be used for benchmarking purposes.


1 Answers

Stopwatch is designed for this purpose and is one of the best ways to measure time execution in .NET.

var watch = System.Diagnostics.Stopwatch.StartNew(); // the code that you want to measure comes here watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; 

Do not use DateTime to measure time execution in .NET.


UPDATE:

As pointed out by @series0ne in the comments section: If you want a real precise measurement of the execution of some code, you will have to use the performance counters that's built into the operating system. The following answer contains a nice overview.

like image 77
Darin Dimitrov Avatar answered Oct 15 '22 12:10

Darin Dimitrov