TimeUtils/perf.mq5
amrali c833329e69
2026-07-19 00:01:17 +03:00

95 lines
3.3 KiB
MQL5

//+------------------------------------------------------------------+
//| perf.mq5 |
//| Copyright © 2026, Amr Ali |
//| https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#include "TimeUtils.mqh"
#define BENCHSIZE 20000000
//ulong randUlong() { return((ulong)rand()<<60)|((ulong)rand()<<45)|((ulong)rand()<<30)|((ulong)rand()<<15)|(ulong)rand(); }
// RNG that can generate all possible 2^64 values.
struct SplitMix64 {
uint64_t x;
uint64_t next() {
uint64_t z = (x += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
long next(long min, long max) {return min + (long)(next()%(max-min+1));} // [min, max]
void seed(uint64_t seed) { x = seed; } // custom seed (e.g., for reproducibility)
SplitMix64() { x = GetTickCount64(); } // auto-seeded constructor
};
#define BENCH(expression, label) { \
ulong sum = 0; \
ulong t1 = GetMicrosecondCount(); \
for(int i=0; i<BENCHSIZE; i++) { \
(expression); \
sum += ((ulong)(mdt.year + mdt.mon + mdt.day + \
mdt.hour + mdt.min + mdt.sec + mdt.day_of_year + \
mdt.day_of_week) << mdt.day); \
} \
t1 = GetMicrosecondCount()-t1; \
PrintFormat("%5.2f ns, checksum = %llu %s", t1*1000.0/BENCHSIZE, sum, label); \
}
#define BENCH2(expression, label) { \
sum = 0; \
ulong t1 = GetMicrosecondCount(); \
for(int i=0; i<BENCHSIZE; i++) { \
(expression); \
} \
t1 = GetMicrosecondCount()-t1; \
PrintFormat("%5.2f ns, checksum = %llu %s", t1*1000.0/BENCHSIZE, sum, label); \
}
void OnStart()
{
Print("\nCompiler Version: " + (string)__MQLBUILD__ + ", " + __CPU_ARCHITECTURE__);
Print(TerminalInfoString(TERMINAL_CPU_NAME) + ", " + TerminalInfoString(TERMINAL_CPU_ARCHITECTURE));
datetime t[];
MqlDateTime dt[];
ArrayResize(t, BENCHSIZE);
ArrayResize(dt, BENCHSIZE);
SplitMix64 sm64;
for(int i = 0; i < BENCHSIZE; i++)
{
t[i] = (datetime) sm64.next(D'1970.01.01', D'3000.12.31');
TimeToStruct(t[i], dt[i]);
}
Print((string)t[ArrayMinimum(t)] + " - " + (string)t[ArrayMaximum(t)] + "\n");
// Test 1
MqlDateTime mdt;
BENCH( TimeToStruct (t[i], mdt), " // MQL's TimeToStruct()" );
BENCH( TimeToStructFast(t[i], mdt), " // TimeToStructFast (TimeUtils)" );
Print("");
// Test 2
ulong sum = 0;
BENCH2( sum += StructToTime (dt[i]), " // MQL's StructToTime()" );
BENCH2( sum += StructToTimeFast(dt[i]), " // StructToTimeFast (TimeUtils)" );
}
//+------------------------------------------------------------------+
/*
Compiler Version: 6033, AVX2 + FMA3
13th Gen Intel Core i3-1305U, AVX2 + FMA3
1970.01.01 00:39:18 - 3000.12.30 23:26:44
8.41 ns, checksum = 6016953815350270448 // MQL's TimeToStruct()
5.49 ns, checksum = 6016953815350270448 // TimeToStructFast (TimeUtils)
2.57 ns, checksum = 325314023340905291 // MQL's StructToTime()
2.61 ns, checksum = 325314023340905291 // StructToTimeFast (TimeUtils)
*/