www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Benchmark block

reply "Jonathan" <jadit2 gmail.com> writes:
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?
Mar 30 2015
next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
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?
I think this can be implemented via a library solution pretty neatly. I was playing with something recently, and I ended up writing an RAII benchmark thing. { auto b = Benchmark("name here"); // Run code here. } With the syntax above in mind, you can write a constructor and a destructor for Benchmark which start and stop a timer and then print the results. I got it working before, and it was kind of fun. Another idea is to do this. benchmark void nameHere() { // Run code here. } Then you can write something or other which finds functions with the benchmark attribute and runs a timer as before, etc. Why add extra syntax for what you can already do pretty nicely with a library?
Mar 30 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-03-31 01:40, w0rp wrote:

 Why add extra syntax for what you can already do pretty nicely with a
 library?
It would be nice if D could support a generic syntax for trailing delegates, i.e. benchmarks { } Would be lowered to: benchmarks({ }); Then it can be implemented as a library solution with a nice syntax that looks built-in. -- /Jacob Carlborg
Mar 31 2015
prev sibling next sibling parent "Robert burner Schadek" <rburners gmail.com> writes:
On Monday, 30 March 2015 at 23:29:40 UTC, Jonathan wrote:
 Thoughts?
Yes please, but as a part as phobos: https://github.com/D-Programming-Language/phobos/pull/2995
Mar 30 2015
prev sibling next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 3/30/15 4:29 PM, 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?
kind("benchmark") unittest { ... } Andrei
Mar 30 2015
parent "Jonathan" <jadit2 gmail.com> writes:
  kind("benchmark") unittest
Is this possible currently?
Mar 31 2015
prev sibling next sibling parent reply "lobo" <swamplobo gmail.com> writes:
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
Mar 30 2015
parent reply "Jonathan" <jadit2 gmail.com> writes:
 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
Well, I don't consider benchmarks <a kind of> unit test. So maybe just this: version(benchmark) { 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 myapp.d
Mar 31 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-03-31 19:05, Jonathan wrote:

 Well, I don't consider benchmarks <a kind of> unit test. So maybe just
 this:
It depends on how you look at it. I know Xcode has support for some kind of unit test benchmark. You can assign a upper value to a benchmark and if that value is exceed the benchmark will fail. Kind of like a unit test but it asserts on how long it takes to execute instead of some functionality. -- /Jacob Carlborg
Mar 31 2015
prev sibling next sibling parent "tcak" <tcak gmail.com> writes:
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?
Thinking that there is "unittest" already, and other people might want other code parts, this turns into labelled code parts. code( kind: benchmark ){ } code( kind: unittest ){ } code( kind: release ){ } etc.
Mar 31 2015
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
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?
version(benchmark) { ... }
Mar 31 2015