Calculate the Execution Time of a Method

Calculate the execution time of a method

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.

How do I time a method's execution in Java?

There is always the old-fashioned way:

long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();

long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.

How to measure time taken by a function to execute


Using performance.now():

var startTime = performance.now()

doSomething() // <---- measured code goes between startTime and endTime

var endTime = performance.now()

console.log(`Call to doSomething took ${endTime - startTime} milliseconds`)

In Node.js it is required to import the performance class

importing performance

const { performance } = require('perf_hooks');


Using console.time: (living standard)

console.time('doSomething')

doSomething() // <---- The function you're measuring time for

console.timeEnd('doSomething')

Note:
The string being passed to the time() and timeEnd() methods must match
(for the timer to finish as expected).

console.time() documentations:

  • MDN documentation
  • Node.js documentation

How do I get time of a Python program's execution?

The simplest way in Python:

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

This assumes that your program takes at least a tenth of second to run.

Prints:

--- 0.764891862869 seconds ---

Best way to measure the execution time of methods

One way to do this would be to use an assembly weaver like 'Fody' with an extension that does exactly what you are looking for. Please see this link for an example extension: https://github.com/Fody/MethodTimer

How Fody works is it injects code into your code-base at compile time, utilising attributes as you have suggested in your question. The provided extension does just as you have described using a stopwatch to log the execution time of your code.

An example of usage:

Once the library is installed, you can add the annotation [Time] to the methods you wish to measure:

[Time]
public void TestMethod()
{
//code here
}

You can then create a custom interceptor (A static class that will be automatically picked up by the Fody extension) which you can use to add a metric track into application insights:

public static class MethodTimeLogger
{
public static void Log(MethodBase methodBase, long milliseconds)
{
var sample = new MetricTelemetry();
sample.Name = methodBase.Name;
sample.Value = milliseconds;
// Your telemetryClient here
telemetryClient.TrackMetric(sample);
}
}


Related Topics



Leave a reply



Submit