Benchmark block

lobo via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 30 19:04:20 PDT 2015


On Monday, 30 March 2015 at 23:29:40 UTC, Jonathan wrote:
> I have no idea if this has been discussed yet, but I was 
> thinking it would be neat to have benchmark blocks that only 
> run when specified, like how unittest works.
>
> Code:
>
> benchmarks
> {
>  import std.conv : to;
>  int a;
>  void f() {auto b = to!string(a);}
>  auto r = benchmark!(f)(10_000);
>  auto f0Result = to!Duration(r[0]);
>  writeln(f0Result)
> }
>
> Example:
> rdmd -benchmarks -main myapp.d
>
> Alternatively, the writeln could be replaced with some kind of 
> standard benchmark output utility (similar to the idea of 
> assert when used for unit tests).
>
> Thoughts?

Would this do what you're after?

version(benchmark) {
unittest {
   import std.conv : to;
   int a;
   void f() {auto b = to!string(a);}
   auto r = benchmark!(f)(10_000);
   auto f0Result = to!Duration(r[0]);
   writeln(f0Result)
  }
}

rdmd -main -- -version=benchmark -unittest myapp.d


Or something along those lines.

This will run all the normal unit tests as well as the benchmark 
tests, which I'd argue is a good thing anyway.

bye,
lobo


More information about the Digitalmars-d mailing list