Features of the mql5 language, subtleties and tricks - page 339

 

A forum on trading, automated trading systems and the testing of trading strategies

Features of the MQL5 language, nuances and techniques

fxsaber, 19 June 2022 16:17

For the specific case where a numeric field is used for sorting (the most common scenario), I’ve written a version that is several orders of magnitude faster than the previous one. However, it uses twice as much memory. This is relevant for large arrays.


Result.

Alert: Bench_Stack = 0, 100 <= Time[Test9.mq5 129 in OnStart: SortOnField_Num::SORT::Sort(Array)] = 34574468 mcs.

Alert: Bench_Stack = 0, 100 <= Time[Test9.mq5 130 in OnStart: SortOnField_Num::SORT2::Sort(Array)] = 10586 mcs.
It seems the reason for this result is that moving large structures between one another is much more costly during sorting than operating on indices.
 
amrali #:
Please have a look in the English section:

The English section at https://www.mql5.com/en/code/53970 also mentions 2024.

Vitaly Muzichenko #:
It would be great to do something with iBarShift(...) – it’s a frequently used function.

Are there any examples of how it could be made faster? And whilst we’re at it, iTime, iHigh and so on.

We need to suggest some options to the developers.

 
Renat Fatkhullin #:

Implemented in build 6002, available on MetaQuotes-Demo

We even managed to beat it in the first instance.

Thanks for the example!

Forum on trading, automated trading systems and testing trading strategies

Features of the mql5 language, subtleties and tricks

amrali, 2026.07.03 01:23

Benchmarks:

StructToTimeFast (EURUSD,H1)    Compiler Version: 5836, AVX2 + FMA3
StructToTimeFast (EURUSD,H1)    13th Gen Intel Core i7-13700KF, AVX2 + FMA3
StructToTimeFast (EURUSD,H1)    30.27 ns, checksum = 323450257960585376  /// MQL's StructToTime()
StructToTimeFast (EURUSD,H1)     1.95 ns, checksum = 323450257960585376   // StructToTimeFast
TimeToStructFast (EURUSD,H1)    Compiler Version: 5836, AVX2 + FMA3
TimeToStructFast (EURUSD,H1)    13th Gen Intel Core i7-13700KF, AVX2 + FMA3
TimeToStructFast (EURUSD,H1)    1970.01.01 01:02:44 - 2999.12.31 23:23:34
TimeToStructFast (EURUSD,H1)    21.34 ns, checksum = 6246603919299185333  /// MQL's TimeToStruct()
TimeToStructFast (EURUSD,H1)     2.79 ns, checksum = 6246603919299185333   // TimeToStructFast
                            

Compiler Version: 6013:

StructToTimeFast (EURUSD,H1)    Compiler Version: 6013, AVX2 + FMA3
StructToTimeFast (EURUSD,H1)    13th Gen Intel Core i3-1305U, AVX2 + FMA3
StructToTimeFast (EURUSD,H1)     6.81 ns, checksum = 323495722718210653  /// MQL's StructToTime()
StructToTimeFast (EURUSD,H1)     6.88 ns, checksum = 323495722718210653   // StructToTimeFast
TimeToStructFast (EURUSD,H1)    Compiler Version: 6013, AVX2 + FMA3
TimeToStructFast (EURUSD,H1)    13th Gen Intel Core i3-1305U, AVX2 + FMA3
TimeToStructFast (EURUSD,H1)    1970.01.01 00:17:35 - 2999.12.31 23:59:58
TimeToStructFast (EURUSD,H1)    32.50 ns, checksum = 6236137421645416417  /// MQL's TimeToStruct()
TimeToStructFast (EURUSD,H1)    13.79 ns, checksum = 6236137421645416417   // TimeToStructFast

Thank you.

 

If you are unable to compile the code because there are errors in an unused function, the problem can be quickly resolved by adding the following line.

template <typename T>
void WrongFunc()
{
  return(0); // A 'void' function returns a value
}
 

I revisited the previous benchmark results and noticed something that may explain the large performance gap.

I suspect that the current implementation in compiler version 6013 uses signed arithmetic, whereas the original Neri-Schneider reference implementation is based on unsigned arithmetic.

To make the benchmark fairer, I modified TimeToStructFast() to perform the same input validation (here) as the built-in TimeToStruct() :

   if(time<0 || time>=91674063532800)
      return(false);

This adds the same range check before the conversion, making the comparison more direct.

With the additional validation, the updated results (in the attched script) are:

TimeToStructFast (XAUUSD,H1)    Compiler Version: 6013, AVX2 + FMA3
TimeToStructFast (XAUUSD,H1)    13th Gen Intel Core i3-1305U, AVX2 + FMA3
TimeToStructFast (XAUUSD,H1)    1970.01.01 00:43:39 - 2999.12.31 23:54:43
TimeToStructFast (XAUUSD,H1)    33.96 ns, checksum = 6234931204052642718  /// MQL's TimeToStruct()
TimeToStructFast (XAUUSD,H1)    22.03 ns, checksum = 6234931204052642718   // TimeToStructFast
TimeToStructFast (XAUUSD,H1)    34.92 ns, checksum = 6234931204052642718   // TimeToStruct_Signed

Even after adding the extra range check, TimeToStructFast() remains approximately 35% faster than the built-in TimeToStruct() while producing identical checksums.

TimeToStruct_Signed() has a comparable performance to the built-in  TimeToStruct().

If my assumption about the signed vs. unsigned arithmetic is correct, there may still be room for further optimization in the built-in implementation.

Files:
 

Forum on trading, automated trading systems and testing trading strategies

Libraries: High-Performance Time Functions (TimeUtils)

amrali, 2026.07.18 17:47

Update 18 July 2026 - version 2.50

  • Added AddPeriod() and SubPeriod() for calendar period arithmetic.
  • Added WithXxx() functions for replacing individual date/time fields.
  • Added IsInSession() overloads for checking recurring daily sessions.
The library codebase page: https://www.mql5.com/en/code/53970
High-Performance Time Functions (TimeUtils)
High-Performance Time Functions (TimeUtils)
  • 2024.12.03
  • www.mql5.com
High-performmance functions for dealing with time.
 
amrali #:
Page containing the library’s source code: https://www.mql5.com/en/code/53970
Updates are only available in the English version at https://www.mql5.com/en/code/53970
High-Performance Time Functions (TimeUtils)
High-Performance Time Functions (TimeUtils)
  • 2024.12.03
  • www.mql5.com
High-performmance functions for dealing with time.
 
Edgar Akhmadeev #:
Updates are only available in the English version at https://www.mql5.com/en/code/53970
This issue is reported to MetaQuotes and should be fixed soon.
 
Amir Yacoby # :
It seems the issue has been resolved (as a test reboot to version 6002)
I’m still experiencing the same delay in version 6033.
 
TheXpert the ternary operator.
enum eHours { _0, _4, _6, _8, _12, _16, _24, _32, _48, _64, _96, _128 };

input eHours TrendHours = _8;

int
eHours2Hours(eHours e) {
	switch (e) {
	case _0:	return 0;
	case _4:	return 4;
	case _6:	return 6;
	case _8:	return 8;
	case _12:	return 12;
	case _16:	return 16;
	case _24:	return 24;
	case _32:	return 32;
	case _48:	return 48;
	case _64:	return 64;
	case _96:	return 96;
	case _128:	return 128;
	default:	return -1;
	}
}