class FruitClass (SOLVED) - page 3

 
Alain Verleyen:

So you said robust but it's not robust ? I don't understand your metaphor sorry.

Maybe, maybe not, not sure where you got this information ?

Also notice that MT5 is faster by more than 25% for such a simple thing.

Try using stack based objects (just create a stack based object in the for loop)

The malloc/free overhead with your new/deletes will be far greater than the time to assign the variable

You might have to multiply your iterations by 10


Either way the code genrated by the compiler shouldn't be any different for your two ctor types. Sadly we can't see the generated assembly which we could in C++

 
James Cater:

Try using stack based objects (just create a stack based object in the for loop)

The malloc/free overhead with your new/deletes will be far greater than the time to assign the variable

You might have to multiply your iterations by 10


Either way the code genrated by the compiler shouldn't be any different for your two ctor types. Sadly we can't see the generated assembly which we could in C++

A little more than 2 times faster :

2017.09.19 00:45:05.061 Efficient EURUSD,M5: 1: standard initialization, 10000000 iterations done in 471842 µs
2017.09.19 00:45:04.588 Efficient EURUSD,M5: 2: initilization list, 10000000 iterations done in 479828 µs

2017.09.19 00:45:04.109 Efficient EURUSD,M5: 2: initilization list, 10000000 iterations done in 1069796 µs
2017.09.19 00:45:03.039 Efficient EURUSD,M5: 1: standard initialization, 10000000 iterations done in 1030818 µs

 
Alain Verleyen:

A little more than 2 times faster :

What is that ? stack vs heap or MQL4 vs MQL5
 
James Cater:
What is that ? stack vs heap or MQL4 vs MQL5

mql4 only.

   for(int i=0;i<ITERATION;i++)
     {
      a1 obj1;
     }

against initial version.

I reduced the iteration as I have no patience to wait for 1 billion iterations.

 
Alain Verleyen:

mql4 only.

against initial version.

I reduced the iteration as I have no patience to wait for 1 billion iterations.

2017.09.18 23:59:56.481 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 220262 µs
2017.09.18 23:59:56.260 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 220182 µs

2017.09.18 23:59:56.040 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 220647 µs
2017.09.18 23:59:55.820 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 221681 µs


I converted them all to stack based and I sometimes get faster standard, and other times faster init list ( the above run was faster init lists, but others were reversed)


2017.09.19 00:03:26.529 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 226471 µs
2017.09.19 00:03:26.303 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 231402 µs
2017.09.19 00:03:26.072 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 225707 µs
2017.09.19 00:03:25.846 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 236558 µs

This run has a mixture of init  and standard being the fastest


I suspect they have very similar overhead and the timing differences are random

 
James Cater:


I'm sorry to tell you but this is not true. C++ and mql4 do not generate code to initialise member variables that are simple types.

It is only imperative to use initialiser lists when the member variables are other classes.


Also I would like to clear up another misunderstanding in this thread.

In MQL4 there's a difference between heap based objects and stack based objects.

  - MQL4 classes that are created on the heap with the new operator seem to use "calloc" rather than "malloc" under the covers which zero initialises the buffer before use.

  - MQL4 classes that are created on the stack (local variables within functions) do not do this and just map the contents of the current stack onto their structure which are random bytes.

Stack based objects will produce "uninitialized memory reads" (UMR) if the simple instance variables are not set in the ctors.


For performance reasons C++ uses malloc and not calloc so UMR are common with heap as well as stack based objects.


That's good to know, I just assumed it was the same as C++. Where did you learn about the inner workings of the MQL compiler?

 
James Cater:

2017.09.18 23:59:56.481 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 220262 µs
2017.09.18 23:59:56.260 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 220182 µs

2017.09.18 23:59:56.040 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 220647 µs
2017.09.18 23:59:55.820 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 221681 µs


I converted them all to stack based and I sometimes get faster standard, and other times faster init list ( the above run was faster init lists, but others were reversed)


2017.09.19 00:03:26.529 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 226471 µs
2017.09.19 00:03:26.303 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 231402 µs
2017.09.19 00:03:26.072 Efficient AUDUSD,M30: 2: initilization list, 10000000 iterations done in 225707 µs
2017.09.19 00:03:25.846 Efficient AUDUSD,M30: 1: standard initialization, 10000000 iterations done in 236558 µs

This run has a mixture of init  and standard being the fastest


I conclude that they are both he same overhead and the timing differences are random

Yes agree.That's what I thought from the start, but as you said we have no way to check the assembly code produced, so we can only check indirectly.

I also noticed your computer is faster than mine


 
Alain Verleyen:

Yes agree.That's what I thought from the start, but as you said we have no way to check the assembly code produced, so we can only check indirectly.

I also noticed your computer is faster than mine


My desktop computer is a i7 3770K (Ivy Bridge) from Jan 2013.

Just what era is your computer from ?

 
James Cater:

My desktop computer is a i7 3770K (Ivy Bridge) from Jan 2013.

Just what era is your computer from ?

i7 860 from 2010, start to become old.

 
nicholishen:

That's good to know, I just assumed it was the same as C++. Where did you learn about the inner workings of the MQL compiler?


I learned how several C++ compilers work from Bjarne Stroustrup's Annotated C++ Reference Manuals. Also inspecting generated assembly code and tracking down compiler bugs over the years helped a lot.

Once you understand how C++ works under the covers for many use cases then spotting MQL4 differences is a little easier.

I worked out the difference between MQL4 stack and heap objects by testing the values after creation (there is no way to see the generated assembly code from MQL4)