Quality and efficiency of mql coding.
An interesting study! I myself am a bit obsessed with trying to find the shortest and most efficient code. Thank you very much for sharing your findings and thoughts with the community :)
add method (https://www.mql5.com/ru/forum/40117/page30#comment_1399820):
void OnStart() { //--- string s="1 M1 5 M5 15 M15 30 M30 60 H1 240 H4 1440 D1 10080 W1 43200 MN1 "; int pos=StringFind(s,string(_Period)); if(pos>=0)Print(StringSubstr(s,pos+6,3)); } //+---------- проходов 11000000 на каждой распечатке 2015.03.01 17:48:55.229 Tf EURUSD,Daily: 1359 2015.03.01 18:20:55.765 PeriodToString2 EURUSD,Daily: METHOD 7 : using arrays. Executed 11000000 times in 875 ms. 2015.03.01 18:20:54.890 PeriodToString2 EURUSD,Daily: METHOD 6 : using switch. Executed 11000000 times in 344 ms. 2015.03.01 18:20:54.546 PeriodToString2 EURUSD,Daily: METHOD 5 : using if(s) improved. Executed 11000000 times in 406 ms. 2015.03.01 18:20:54.140 PeriodToString2 EURUSD,Daily: METHOD 4 : using EnumToString(macro). Executed 11000000 times in 1672 ms. 2015.03.01 18:20:52.468 PeriodToString2 EURUSD,Daily: METHOD 3 : using if(s) original. Executed 11000000 times in 406 ms. 2015.03.01 18:20:52.062 PeriodToString2 EURUSD,Daily: METHOD 2 : using EnumToString. Executed 11000000 times in 4687 ms. 2015.03.01 18:20:47.374 PeriodToString2 EURUSD,Daily: METHOD 1 : using Static Arrays. Executed 11000000 times in 1172 ms.
)
string Period_[43200]; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- Period_[0]="M1"; Period_[4]="M5"; Period_[14]="M15"; Period_[29]="M30"; Period_[59]="H1"; Period_[239]="H4"; Period_[1439]="D1"; Period_[10079]="W1"; Period_[43199]="MN"; Print( Period_[_Period-1]); }
There was recently an interesting topic on Russian forum, where the discussion started from a poll on the quality of code produced by freelancer to finally shift to a discussion on the best way to retrieve the name of a timeframe (as s string) from its mql4 value.
The big divergence start from the following code, posted as a "proof" of bad quality code :
Unfortunately, as it's often the case on Russian forum
, there was not any useful conclusion. So I decide to check by myself.
I created a script to benchmark all solutions I could find on this topic, on the forums in general, including my own solution. There was 8 proposed methods, one being discarded as not working, remains 7 methods. These methods are presented "as is", the author doesn't necessarily had efficiency in mind when posting the code.
The methods :
The results :
2015.03.01 13:50:06.514 METHOD 1 : using Static Arrays. Executed 11000000 times in 2109 ms.
2015.03.01 13:50:13.735 METHOD 2 : using EnumToString. Executed 11000000 times in 7219 ms.
2015.03.01 13:50:15.282 METHOD 3 : using if(s) original. Executed 11000000 times in 1547 ms.
2015.03.01 13:50:17.580 METHOD 4 : using EnumToString(macro). Executed 11000000 times in 2297 ms.
2015.03.01 13:50:19.017 METHOD 5 : using if(s) improved. Executed 11000000 times in 1437 ms.
2015.03.01 13:50:20.377 METHOD 6 : using switch. Executed 11000000 times in 1360 ms.
2015.03.01 13:50:22.112 METHOD 7 : using arrays. Executed 11000000 times in 1734 ms.
As you can see the more efficient is the "switch" one.
Personally I am using method 4, with only 1 line of code, efficiency doesn't matter, as most of the time you can use that only 1 once during initialization, and saved the result in a global variable. Of course, all depends of the context.
Make your choice.
Attached the full script, you can change the #define NO_TESTING to TESTING, to check the results of each method.