Measuring Execution time

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 22 02:32:13 PDT 2015


On Wednesday, 22 July 2015 at 09:23:36 UTC, Clayton wrote:
> How does one represent Duration in only Micro-seconds, or 
> milliseconds. Trying to measure the execution time of an 
> algorithm and I get "4 ms, 619 μs, and 8 hnsecs" , I want to 
> sum all these and get total hnsecs or μs .
>
> I would also  appreciate advise on  whether this is the best 
> way to measure the execution time of an algorithm.
>
>
>
> import std.datetime;
> import std.stdio;
>
> void algorithm( ){
> 	writeln("Hello!");
> }
> void main(){
>
>         auto stattime = Clock.currTime();
> 	algorithm( );
> 	endttime = Clock.currTime();
>
> 	auto duration = endttime - stattime;
>
> 	writeln("Hello Duration ==> ", duration);
>
> }

The normal way of doing this would be using 
std.datetime.StopWatch:

     StopWatch sw;
     sw.start();
     algorithm();
     long exec_ms = sw.peek().msecs;


More information about the Digitalmars-d-learn mailing list