Libraries: Benchmark

 

Benchmark:

A set of macros to benchmark small code snippets for their execution speeds.

Author: amrali

 

Update 22 April, 2023

Updated the TimeIt() macro for more precise measurement of the execution speed.

 
Try to use MACROS2.
#define MACROS \
do             \
{              \
  int i = 0;   \
} while(0);

#define MACROS2 \
{               \
  int i = 0;    \
}

void OnStart()
{
  MACROS
  MACROS
  
  MACROS2
  MACROS2 
}
 
fxsaber #:
Try to use MACROS2.

Using do{} while loop allows you to treat a function-like macro that has many lines as a single statement which can be terminated by semicolon. Using brackets only gives logical errors if inside nested if/else or switch() case constructs. 

If using brackets alone, you should be aware of the errors due to semicolons.

See https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros

Why use apparently meaningless do-while and if-else statements in macros?
Why use apparently meaningless do-while and if-else statements in macros?
  • 2008.09.30
  • jfm3 jfm3 36.6k 10 10 gold badges 32 32 silver badges 35 35 bronze badges
  • stackoverflow.com
In many C/C++ macros I'm seeing the code of the macro wrapped in what seems like a meaningless loop. Here are examples. I can't see what the is doing. Why not just write this without it?
 
amrali #:

Using do{} while loop allows you to treat a function-like macro that has many lines as a single statement which can be terminated by semicolon. Using brackets only gives logical errors if inside nested if/else or switch() case constructs. 

If using brackets alone, you should be aware of the errors due to semicolons.

See https://stackoverflow.com/questions/154136/why-use-apparently-meaningless-do-while-and-if-else-statements-in-macros

Thanks.

 
Endless cycle.
/*  
#include <fxsaber\Benchmark\Benchmark.mqh> // https://www.mql5.com/ru/code/31279
#define TimeIt(A) _B(A, 1)

void OnStart()
{
  Print(TimeIt(f())); // https://www.mql5.com/en/forum/398833/page13#comment_46372879
}
*/
#include <Benchmark\Benchmark.mqh> // https://www.mql5.com/en/code/43910

void OnStart()
{
  TimeIt(f()); // https://www.mql5.com/en/forum/398833/page13#comment_46372879
}
 
fxsaber #:
Endless cycle.

Please read the instructions carefully. This is your fault!

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- if result of the expression is not used, compiler optimizes out
//--- (i.e., ignores) the function call.
   double sum = 0;

   TimeIt(sum += f());  // https://www.mql5.com/en/forum/398833/page13#comment_46372879

   Print("sum=",sum);
  }

// sum+=f() -> 1883807.000 µsec/call
// sum=5.99216352

Soon, I will add a small fix for the TimeIt() macro to guard against those non-careful calls.

 

Update 25 April 2023

Fixed the TimeIt() macro to avoid an infinite loop in rare situations.

 
I have not yet found useful scenarios for using such macros in practice.

It was important for me to find problem areas in the program to find problems in the form of program slowdowns in some situations.

So far, I can't imagine how such a library would help to solve this.
 
fxsaber #:
I have not yet found useful scenarios for using such macros in practice.

It was important for me to find problem areas in the program to find problems in the form of program slowdowns in some situations.

So far, I can't imagine how such a library would help to solve this.

Then, you need the code profiler to spot bottlenecks in your program, not the benchmarks.

Benchmarks have a specific use scenario, the same as that in the description above.

 
fxsaber #:
I have not yet found useful scenarios for using such macros in practice.

It was important for me to find problem areas in the program to find problems in the form of program slowdowns in some situations.

So far, I can't imagine how such a library would help to solve this.
You could try the macros for performance measurements in this project.





Reason: