Update 22 April, 2023
Updated the TimeIt() macro for more precise measurement of the execution speed.
#define MACROS \ do \ { \ int i = 0; \ } while(0); #define MACROS2 \ { \ int i = 0; \ } void OnStart() { MACROS MACROS MACROS2 MACROS2 }
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.
- 2008.09.30
- jfm3 jfm3 36.6k 10 10 gold badges 32 32 silver badges 35 35 bronze badges
- stackoverflow.com
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.
Thanks.
/* #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 }
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.
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Benchmark:
A set of macros to benchmark small code snippets for their execution speeds.
Author: amrali