A small benchmarking lib

Etranger via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Tue Aug 1 02:02:17 PDT 2017


Hi all,

I don't know if it is the right place to post that but I wrote a 
little benchmarking lib [1] in order to learn the D programming 
language, and also because I was learning Rust at the same time 
and wanted to be able to compare their performances.

The lib is in part a translation of the Rust benchmarking 
functionality (which is integrated into the rust compiler) to D.

Example:

import std.stdio;
import std.json;

import simplebench;

immutable N = 25;
// Functions to bench
ulong fib_rec(immutable int n){
  ...
}
// Function to bench
ulong fib_for_loop(immutable int n) {
     ...
}

// The proper test function
void test_fib_rec(ref Bencher bencher){
   int n=N; // Init variables, allocate memory ...
   bencher.iter((){
       return fib_rec(n); // The real code to bench
   });
}

void main()
{
   // The test function have to be static
   static void test_fib_for_loop(ref Bencher bencher){
     int n=N;
     bencher.iter((){
         return fib_for_loop(n);
     });
   }

   assert(fib_for_loop(N) == fib_rec(N));
   // Run the benchmarks
   auto br = BenchMain!(test_fib_rec, test_fib_for_loop);
   // Convert the results to JSON
   writeln(br.toJSON.toPrettyString);

}

I hesitated to post it as I'm still learning the language but who 
knows maybe it can be useful for someone (And maybe someone will 
give me feedback for improvement).

[1] https://github.com/BigEpsilon/simpleBench

PS: I know there is a benchmark function in the standard lib but 
I found it quite limited.


More information about the Digitalmars-d-announce mailing list