Computing Power Optimization

 

Hi, guys!

This is my first thread, so let me introduce myself: I'm Jonatan from Argentina (South America). I've been a full-time trader for almost 10 years.

I'm not exactly new at general coding, but I am at MQL language (4 and 5).

My  concerns are about optimization... and not the Backteasting ones. I need a full-time coder/software engineer that get me straight on this.

 

I need to know what functions, loops, etc. are more "light execution". i.e. the loop "While" and "for" are really usefull, but does it take more computing power than nest some "if/else" statements?

Another example: is better to set a "series of variables" rather than use Arrays? It depends on the size?

The thing about all this, is to have the fastest data access/transfer possible.

 

If someone is willing to contribute, I'll be pleased.

Kind regards,

JCTrading. 

 

Hello,

good question, I can not answer much, as I am not aware of the internals of MQL and MQL compiler is pretty much basic . But as is the case in most ...cases, a good way to find out more on undocumented situations is to make your own benchmarks. In MQL you can use GetTickCount() to perform such a task. Ok, now on to what I am aware as of now

a. MQL has an optimization switch, flag, but you can use it (as far as I am aware) only in the command line, which is -o

b. Switch case can be more fast than ifs since I suspect it is executed as a series of goto statements (as MQL is C like, according to MetaQuote, and the manual states "The case keyword with a constant are just labels")

https://docs.mql4.com/basis/operators/switch and https://en.wikipedia.org/wiki/Switch_statement

c.  For is actually a while statement so you can get away with only using while; also while is rather basic

d. In general function calls are more expensive but also, in most times, library functions can be way more efficient than anything we can make our selves without using assembly. Also, Instead of beating our selves by breaking up arrays, I believe it would be better to implement traditional ways of optimisations, such as function inlining (for our own functions), loop unrolling and so on

 

best regards 

 

I would recommend you to do your own tests with a simple script like this (not tested, from scratch):

void OnStart()  {
   string strResult;
   int  l,loops = 100000;
   l=loops;
   uint end, beg = GetTickCount();
   while( l-->0  && !IsStopped() ){
      //do here your first part of measurement

   }
   end = GetTickCount()-beg;
   strResult = "First loop took: "+DoubleToString(0.001*end,3)+" sec\n";
   l=loops;
   beg = GetTickCount();
   while( l-->0  && !IsStopped() ){
      //do here your second part of measurement

   }
   end = GetTickCount()-beg;
   strResult = "Second loop took: "+DoubleToString(0.001*end,3)+" sec\n";
   Comment( strResult );
}
 
gooly:

I would recommend you to do your own tests with a simple script like this (not tested, from scratch):



You don't need all these GetTickCount(), you can profile your code with the integrated profiler (MetaEditor), it's very useful, and doesn't need to modify your actual code.
 
JCTrading:

Hi, guys!

This is my first thread, so let me introduce myself: I'm Jonatan from Argentina (South America). I've been a full-time trader for almost 10 years.

I'm not exactly new at general coding, but I am at MQL language (4 and 5).

My  concerns are about optimization... and not the Backteasting ones. I need a full-time coder/software engineer that get me straight on this.

 

I need to know what functions, loops, etc. are more "light execution". i.e. the loop "While" and "for" are really usefull, but does it take more computing power than nest some "if/else" statements?

Another example: is better to set a "series of variables" rather than use Arrays? It depends on the size?

The thing about all this, is to have the fastest data access/transfer possible.

 

If someone is willing to contribute, I'll be pleased.

Kind regards,

JCTrading. 

If you want optimization performance I suggest you to use MT5.

If your EA is properly coded, using arrays or "series of variables", while/for or if/else, etc...doesn't really matter. Check your code with the ME profiler, you will quickly find any eventual bottleneck.

 
Thank you all, guys! I was able to perform speed tests on my codes diveded by sections; sometimes I had 78hs remaining in the backtester without heavy or large parameters to check.

Problem was in some nested loops. Kept the logic with a different approach and everything went ok.
 
JCTrading:
Thank you all, guys! I was able to perform speed tests on my codes diveded by sections; sometimes I had 78hs remaining in the backtester without heavy or large parameters to check.

Problem was in some nested loops. Kept the logic with a different approach and everything went ok.

hello again,

I see you were having some problems with loops. Well, regarding that aspect I have some remarks to do, which may be usefull , even though you have resolved your issues, as you are saying. So, while "experimenting" with some C code, I came accross the following bottle-neck situation, regarding the following nested loops (here, all the code is for MQL, but it is the same, nevertheless)

    uint begin, end; 
    
    int a, b; 

    static int table[16384][16384]; /* static, because table is a local array, that goes into stack. As such, it is too large for that; static 
                                       will get it to the heap - or else it could be defined as a global variable, in which case, it should be 
                                       brought to the heap as well */ 
    begin=GetTickCount();

    for (a=0; a<16384; a++)
    {
        for (b=0; b<16384; b++)
        {
            table[b][a]=b;          /* next time, swap the order, so it should be table[a][b]=b; */
        }
    }       
    
    end=GetTickCount(); 
    
    Alert("Time passed is ",( (float)end-begin)/1000,"s");

(Normally, the above goes into OnInit())

 The bottle-neck happens in the line "table[b][a]=b;". Of course, normally, since the first for is about a, and the second about b, you would, most likely, write the loop as "table[a][b]". But, if for some reason you put b before a, the compute time goes 2 and 1/2 (not the TV series) times up (from 4 to 10 secs). What's more, while in C, it goes from about 1.5 seconds in my PC, to about 8 secs, which is 5 to 6 times more.

best regards 

Reason: