c# - Measuring code execution time -


i want know how time procedure/function/order takes finish, testing purposes.

this did method wrong 'cause if difference of seconds 0 can't return elapsed milliseconds:

notice sleep value 500 ms elapsed seconds 0 can't return milliseconds.

    dim execution_start system.datetime = system.datetime.now     threading.thread.sleep(500)      dim execution_end system.datetime = system.datetime.now     msgbox(string.format("h:{0} m:{1} s:{2} ms:{3}", _     datediff(dateinterval.hour, execution_start, execution_end), _     datediff(dateinterval.minute, execution_start, execution_end), _     datediff(dateinterval.second, execution_start, execution_end), _     datediff(dateinterval.second, execution_start, execution_end) * 60)) 

can show me better way this? maybe timespan?

the solution:

dim execution_start new stopwatch execution_start.start()  threading.thread.sleep(500)  messagebox.show("h:" & execution_start.elapsed.hours & vbnewline & _        "m:" & execution_start.elapsed.minutes & vbnewline & _        "s:" & execution_start.elapsed.seconds & vbnewline & _        "ms:" & execution_start.elapsed.milliseconds & vbnewline, _        "code execution time", messageboxbuttons.ok, messageboxicon.information) 

a better way use stopwatch, instead of datetime differences.

stopwatch class - msdn

provides set of methods , properties can use accurately measure elapsed time.

stopwatch stopwatch = stopwatch.startnew(); //creates , start instance of stopwatch //your sample code system.threading.thread.sleep(500); stopwatch.stop(); console.writeline(stopwatch.elapsedmilliseconds); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -