Question

I'm new to the D language and need to measure the execution time of an algorithm. What are my options? Is there already some built-in solution? I could not find anything conclusive on the web.

Était-ce utile?

La solution

One way is to use -profile command line parameter. After you run the program, it will create file trace.log where you can find run time for each function. This of course will slow down your program as the compiler will insert time counting code into each your function. This method is used to find relative speed of functions, to identify which you should optimize to improve app speed with minimum effort.

Second options is to use std.datetime.StopWatch class. See the example in the link.

Or even better suited might be to directly use std.datetime.benchmark function.

Don't forget:

  1. When benchmarking use these dmd compiler flags to achieve maximum optimization -release -O -inline -noboundscheck.
  2. Never benchmark debug builds.
  3. Make sure you don't call any library code inside benchmarked functions - You would be benchmarking performance of library implementation instead of your own code.

Additionally you may consider using LDC or GDC compilers. Both of them provide better optimizations / app run speed than DMD.

Autres conseils

If your algorithm can be called from the commandline there's a nifty utility written in D that will run your program a number of time and print out the distribution of the average time taken and all sort of other useful numbers.

It's called avgtime and it's here: https://github.com/jmcabo/avgtime

std.benchmark is in the review queue.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top