Does StringConcatenate speed up string concatenation?

 

I personally had never used StringConcatenate because it would have made my code less readable and probably wouldn't have save much time anyway.

https://docs.mql4.com/strings/stringconcatenate

"The StringConcatenate() works faster and more memory-saving than when strings are concatenated using addition operations (+)."

But since others evidently use this function I thought I would test it.

#define LOOPTILL 9000000

int start(){

   string str1 = "This is ";
   string str2 = "part of a ";
   string str3 = "much longer string";
   string result="";
   
   int n=0;
   int start=0;
   int delta=0;
   
   // Since MT4 does not have an optimizing compiler we are safe to make this test.
   // An optimizing compiler might realise that nothing is happening and simplify the code block
   
   start= GetTickCount();
   for( n=0; n<LOOPTILL; n++ ){
      result = str1 + str2 + str3;
   }
   
   delta = GetTickCount() - start;
   Print("String addition " + LOOPTILL + " times in " + delta + " ms");
   
   start= GetTickCount();
   for( n=0; n<LOOPTILL; n++ ){
      result = StringConcatenate(str1,str2,str3);
   }
   
   delta = GetTickCount() - start;
   Print("String concatenation " + LOOPTILL + " times in " + delta + " ms");  

   return(0);
}

Result:


Addition -> 4110ms

Concatenation ->5484ms

Conclusion: StringConcatenate is worse than useless (in terms of speed at least)


And to be doubly sure of the test I swapped the test order so that the StringConcatenate was done first. Same sort of result, although there is variation from test to test due to the finite timer resolution. Any of you guys with faster machines might need to increase LOOPTILL, depending on your system timer resolution.

 
dabbler:

I personally had never used StringConcatenate because it would have made my code less readable and probably wouldn't have save much time anyway.

https://docs.mql4.com/strings/stringconcatenate

"The StringConcatenate() works faster and more memory-saving than when strings are concatenated using addition operations (+)."

But since others evidently use this function I thought I would test it.

Result:


Addition -> 4110ms

Concatenation ->5484ms

Conclusion: StringConcatenate is worse than useless (in terms of speed at least)


And to be doubly sure of the test I swapped the test order so that the StringConcatenate was done first. Same sort of result, although there is variation from test to test due to the finite timer resolution. Any of you guys with faster machines might need to increase LOOPTILL, depending on your system timer resolution.

It might be faster when adding string literals together (rather than string variables as in your example) but I doubt it would make any significant difference in 'everyday' code
 
You might try
double str1 = 1.1;
double str2 = 2.22;
double str3 = 3.333;
:
    result = DoubleToStr(str1,Digits) + DoubleToStr(str2,Digits) + DoubleToStr(str3,Digits);
:
    result = StringConcatenate(str1,str2,str3);
as concatenate does conversions just like Print/Comment.
 
paul_hughes:
It might be faster when adding string literals together (rather than string variables as in your example) but I doubt it would make any significant difference in 'everyday' code
Exactly. This justifies my not using it :-)
 
I like doing these kinds of speed tests too, I was recently suprised to discover it takes 4 times longer to assign a value to an array index than it does to assign it to an integer
 

No surprise there, as

  1. Every access is checked for limits (read returns zero and GetLastError returns ERR_ARRAY_INDEX_OUT_OF_RANGE 4002)
  2. arrays can be forward or backwards (asSeries)
 

Hello,

  

these are my results: 

Original test  3 strings Addition = 2636

Original test 3 strings Concatenation = 2871 

 

Quite in-line with the first topic... but if I start to concatenate 4 strings instead of 3:  

4 strings Addition = 4556

4 strings Concatenation = 3962 

 

With 6 strings: 

6 strings Addition = 9891

6 strings Concatenation = 7519

 

With 9 strings: 

9 strings Addition = 20155

9 strings Concatenation = 12854 

 

It seems clear that StringConcatenate() start to outperform the simple string addition when we start to merge more than 3 strings together 

Reason: