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.
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
Post a Comment