www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - A small benchmarking lib

reply Etranger <nonvalidemail nowhere.com> writes:
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.
Aug 01
parent Etranger <nonvalidemail nowhere.com> writes:
On Tuesday, 1 August 2017 at 09:02:17 UTC, Etranger wrote:
 Hi all,
Forgot to put an example output and it seems we cannot edit our previous messages. So here is the output of the example in my previous post: ------------------------------------------------------------------------------------------------------------------------------------ Benchmark name | Time/iter | Best | Worst | Total | Iterations | ------------------------------------------------------------------------------------------------------------------------------------ test_fib_rec | 274.971 us (+/- 13.956 us) | 271.525 us | 285.482 us | 3.73613 sec | 271 | test_fib_for_loop | 4 ns (+/- 0 ns) | 4 ns | 4 ns | 276.258 msec | 1169755 | ------------------------------------------------------------------------------------------------------------------------------------ { "BenchmarksResults": [ { "mb_s": 0, "name": "test_fib_rec", "ns_iter_summ": { "iqr": 4803.25, "max": 285482.400000000023, "mean": 276296.097999999998, "median": 274971.5, "median_abs_dev": 3091.221, "median_abs_dev_pct": 1.12419687131211776, "min": 271525.900000000023, "quartiles": [ 273139.75, 274971.5, 277943 ], "std_dev": 4389.8878894842901, "std_dev_pct": 1.5888345587436743, "sum": 13814804.9000000004, "var": 19271115.6822408326 }, "total_iterations": 271, "total_ns": 3736127769 }, { "mb_s": 0, "name": "test_fib_for_loop", "ns_iter_summ": { "iqr": 0, "max": 4, "mean": 4, "median": 4, "median_abs_dev": 0, "median_abs_dev_pct": 0, "min": 4, "quartiles": [ 4, 4, 4 ], "std_dev": 0, "std_dev_pct": 0, "sum": 200, "var": 0 }, "total_iterations": 1169755, "total_ns": 276258388 } ] } which shows better in a terminal :/
Aug 02