optimization - How to use StopWatch multiple times in C#? -
i have short code performs different operations , want measure time takes perform each. read here stopwatch class, , wanted optimize time measurements. functions calls 5 other functions , want measure each without declaring :
stopwatch sw1 = new stopwatch(); stopwatch sw2 = new stopwatch(); etc..
my function looks that:
public bool func() { .... func1() func2() .... .... func5() }
is there way measure time using 1 stopwatch instance?
thanks!!
use delegates pass method parameter function.
here used action delegates methods specified not return value.
you can modify accordingly if method has return type or parameter using function delegate
static void main(string[] args) { console.writeline("method 1 time elapsed (ms): {0}", timemethod(method1)); console.writeline("method 2 time elapsed (ms): {0}", timemethod(method2)); } static long timemethod(action methodtotime) { stopwatch stopwatch = new stopwatch(); stopwatch.start(); methodtotime(); stopwatch.stop(); return stopwatch.elapsedmilliseconds; } static void method1() { (int = 0; < 100000; i++) { (int j = 0; j < 1000; j++) { } } } static void method2() { (int = 0; < 5000; i++) { } } }
by using pass method want.
hope helps!
Comments
Post a Comment