2. Faster access times that were achieved here are never due to an object or a structure, it is merely due to compiler's optimization for the "switch" statement.
3. Optimization of switch depends on the number of cases + optimization mode. Large number or complex switches do not get this jump table optimization and will remain as multiple if-else.
4. Lookup table using an array will always be the standard and the most reliable method not depending on the number of cases, their complexity or the compiler's optimization mode.
int Lookup_Table(int x)
{
staticint table[16] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150};
return table[x];
}
// When compiling an small to medium switch with consecutive cases (0 to n), under// optimization mode, the compiler converts the series of these multiple if-else// statements O(n) into a jump table, where an array of pointers to code blocks// is indexed using the cases number at runtime, giving O(1) direct access similar// to the lookup table using an array.int Switch(int x)
{
switch(x)
{
case0: return0;
case1: return10;
case2: return20;
case3: return30;
case4: return40;
case5: return50;
case6: return60;
case7: return70;
case8: return80;
case9: return90;
case10: return100;
case11: return110;
case12: return120;
case13: return130;
case14: return140;
case15: return150;
}
return -1;
}
voidOnStart()
{
long sum1 = 0;
long sum2 = 0;
int slots = 16;
int test_count = 1e8;
int seed = MathRand();
ulong time1 = GetMicrosecondCount();
for(int i = 0; i < test_count; i++)
for(int x = 0; x < slots; x++) {
//int j = (x * 7 + seed) % slots; // uncomment two lines to test lookups in a random order (Miller Shuffle algorithm)//sum1 += Lookup_Table(j); // in a real situation, the lookups are not sequential
sum1 += Lookup_Table(x);
}
time1 = GetMicrosecondCount() - time1;
PrintFormat("%6ull microsec, sum:%ull %s", time1, sum1, " // Lookup table");
ulong time2 = GetMicrosecondCount();
for(int i = 0; i < test_count; i++)
for(int x = 0; x < slots; x++) {
//int j = (x * 7 + seed) % slots;//sum2 += Switch(j);
sum2 += Switch(x);
}
time2 = GetMicrosecondCount() - time2;
PrintFormat("%6ull microsec, sum:%ull %s", time2, sum2, " // Switch statement");
}
No proof given, but printf() to the expert-journal is again truncating strings to be printed, abitrarily.
Obviously the limit of printing strings beyond size of 16K is again truncated, as it was quite some builds ago, and now came back. - Only possible fix is round about this:
Above code actually needs more complex wrapping with templated calls, such that you can pass through StringFormat() before sending your string to above function.
Examples for this Bug have been given before, please search the forum, if you are in doubt of my findings and reporting.
2025.08.2411:18:40.544 Test shablon (AUDCAD,M5) 3 Years 6 Months
2025.08.2411:18:40.544 Test shablon (AUDCAD,M5) 1 Days
2025.08.2411:18:40.544 Test shablon (AUDCAD,M5) 1 Years 2 Days
2025.08.2411:18:40.544 Test shablon (AUDCAD,M5) 1 Years 1 Days
To be clear, the operation 'TYPE NAME = INITIALIZER' will no longer be split into two operations DECL + ASSIGNMENT, but will remain a single 'DECL [has initializer]' If you need to declare a variable and call the = operator for it, split the operation into two parts manually: DECL; ASSIGNMENT; (yes, you will have to part with the variable constant).
struct A
{
int Tmp;
// A() {}// A( const A& ) { Print(__FUNCSIG__); } voidoperator =( const A& ) { Print(__FUNCSIG__); }
};
voidOnStart ()
{
A a1;
// const - allowed./*const */A a2 = a1;// The implicit copy constructor A::A(const A&) is called// const - not allowed./*const */A a3; a3 = a1;// The copy operator A::operator=(const A&) is called
}
Similarly.
class B
{
A a;
public:
B( const A &Tmp) : a(Tmp)// The implicit copy constructor A::A(const A&) is called
{
a = Tmp;// The copy operator A::operator=(const A&) is called
}
};
voidOnStart()
{
A a;
B b(a);
}
If there is a need to initialise a static object through a copy operator, it can be done by this technique.
class B
{
static A a1;
static A a2;
static A a3;
staticbool Init;
template <typename T>
staticbool Operator( T &Dest, const T& Source )
{
Dest = Source; // The copy operator T::operator=(const T&) is calledreturn(true);
}
};
static A B::a1;
static A B::a2; // Need B::a2 = B::a1 via A::operator=(const A&)static A B::a3; // Need B::a3 = B::a1 via A::operator=(const A&)staticbool B::Init = B::Operator(B::a2, B::a1) && // B::a2 = B::a1 via A::operator=(const A&)
B::Operator(B::a3, B::a1); // B::a3 = B::a1 via A::operator=(const A&)
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Wrong!
I write in the Russian-language thread, which is automatically translated into the English-language thread that you read.
Unfortunately, deleting the erroneous post in the Russian-language thread had no effect on the English-language thread.
Maximum Optimization:
Faster access times are due to the generated jump table under the full optimization mode.
No Optimization:
A simple and reliable piece of code to generate a full 32-bit unsigned random number in [0, UINT_MAX]:
we can also use it as functions
Old bug coming back again.
No proof given, but printf() to the expert-journal is again truncating strings to be printed, abitrarily.
Obviously the limit of printing strings beyond size of 16K is again truncated, as it was quite some builds ago, and now came back. - Only possible fix is round about this:
Above code actually needs more complex wrapping with templated calls, such that you can pass through StringFormat() before sending your string to above function.
Examples for this Bug have been given before, please search the forum, if you are in doubt of my findings and reporting.
I needed to determine the length of the test interval.
I couldn't find it on the forum, so I wrote such a function.
It seems to show correctly.
Couldn't find it on the forum, so I wrote such a function.
We needed to determine the length of the testing interval.
I couldn't find it on the forum, so I wrote such a function.
It seems to show correctly.
What is the point of such contrivances?
The result is the same
Why all the fancy stuff?
The result is the same
Forum on trading, automated trading systems and testing trading strategies
New version of MetaTrader 5 build 5200: OpenBLAS extension and increased control in MQL5
Ilyas, 2025.08.04 14:08
To be clear, the operation 'TYPE NAME = INITIALIZER' will no longer be split into two operations DECL + ASSIGNMENT, but will remain a single 'DECL [has initializer]'
If you need to declare a variable and call the = operator for it, split the operation into two parts manually: DECL; ASSIGNMENT; (yes, you will have to part with the variable constant).
Similarly.
If there is a need to initialise a static object through a copy operator, it can be done by this technique.
Forum on trading, automated trading systems and testing trading strategies
Peculiarities of mql5 language, subtleties and techniques of work
fxsaber, 2025.08.26 08:32