Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

MetaTrader 5 Libraries | Benchmark

MetaTrader Experts, Indicators, Scripts and Libraries

The include file "Benchmark.mqh" has a set of macros to benchmark various functions for their execution speeds to decide which is the fastest one.

//+------------------------------------------------------------------+  //| Macro to measure the execution time a function.                  |  //| Prints the elapsed time in microseconds per call (µsec/call).    |  //| TimeIt(sum += ArrayBsearch(arr, value));                         |  //+------------------------------------------------------------------+  #define TimeIt(func_invocation)      //+------------------------------------------------------------------+  //| Macro to execute a function for a fixed number of times.         |  //| Prints the elapsed time in milliseconds (msec).                  |  //| Benchmark(repeats, sum += ArrayBsearch(arr, value));             |  //+------------------------------------------------------------------+  #define Benchmark(repeats, func_invocation)      //+------------------------------------------------------------------+  //| Macro to execute a function for a fixed duration in msec.        |  //| Prints the number of operations per second (ops/sec).            |  //| Benchmark_opsec(msec, sum += ArrayBsearch(arr, value));          |  //+------------------------------------------------------------------+  #define Benchmark_opsec(msec, func_invocation)    

The library is based on public source codes of timeit module in python https://docs.python.org/3/library/timeit.html and Stopwatch Class from C# https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch 

Here is an example script for usage:

//+------------------------------------------------------------------+  //|                                               benchmark_test.mq5 |  //|                                        Copyright © 2018, Amr Ali |  //|                             https://www.mql5.com/en/users/amrali |  //+------------------------------------------------------------------+  #include <Benchmark\Benchmark.mqh>    //+------------------------------------------------------------------+  //| The function returns integer numeric value closest from below.   |  //+------------------------------------------------------------------+  double FastFloor(const double v)    {     double k = (double)(long)v;     return k - (v < k);    }    //+------------------------------------------------------------------+  //|                                                                  |  //+------------------------------------------------------------------+  void OnStart(void)    {     int repeats = 1e8;     Print("repeats=",repeats);    //--- if result of the expression is not used, compiler optimizes out  //--- (i.e., ignores) the function call.     double sum = 0;       Benchmark(repeats, sum += MathFloor(__i));     Benchmark(repeats, sum += FastFloor(__i));       Print("sum=",sum);    }  //+------------------------------------------------------------------+    // sample output:  /*   repeats=100000000   sum+=MathFloor(__i) -> 599 msec   sum+=FastFloor(__i) -> 138 msec   sum=9999999922280040.0  */  
43910