Errors, bugs, questions - page 2315

 
fxsaber:

Why is by_ref slower than by_val?

Where does it follow from?

If the code is the same

//Test.mqh
        int by_ref(string& var) { return int(var) % 100; }
        int by_val(string  var) { return int(var) % 100; }
//Test.mq5
#include "Test.mqh"
#define  ITERATIONS 1000000
#define  MACRO( X ) \
        { \
                ulong time = GetMicrosecondCount(); \
                for(int i=0; i<ITERATIONS; i++) \
                { \
                        string r = string( rand ); \
                        sum_##X += by_##X(r); \
                } \
                time_##X = GetMicrosecondCount() - time; \
        }
void OnStart()
{
        for ( int i = 0; i < 10; i++ )
                OnStart2( rand());
}
void OnStart2( int rand )
{
        long time_ref, sum_ref = 0;
        long time_val, sum_val = 0;
        MACRO( ref )
        MACRO( val )
        printf( "%+10.3f:%d", (time_ref-time_val)/1000.0, sum_ref-sum_val );
}

so does the result (execution time difference):

+12.221:0
+5.099:0
+0.149:0
-13.729:0
+14.531:0
-27.429:0
+26.405:0
-0.839:0
+5.400:0
-4.882:0

alternating (+\-) within insignificant limits

 
A100:

Where does it come from?

#define  ITERATIONS 1000000

void OnStart()
{
   string Str = "1";
   
   for (int i = 0; i < 10; i++)
     Str += Str;
   
   for (int j = 0; j < 5; j++)
   {
     {
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++){
           string r = string(rand()) + Str;
           sum += by_ref(r);
        }
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_ref", time/1000.0, sum);
     }{
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++)
           sum += by_val(string(rand()) + Str);
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_val", time/1000.0, sum);
     }
     
     Print("");
   }
}
//+------------------------------------------------------------------+

int by_ref(string &var){ return int(var) % 100; }
int by_val(string  var){ return int(var) % 100; }


by_ref took 15119.806 milliseconds: sum=-1000000 ll
by_val took 13872.479 milliseconds: sum=-1000000 ll

by_ref took 14433.781 milliseconds: sum=-1000000 ll
by_val took 13817.533 milliseconds: sum=-1000000 ll

by_ref took 13889.424 milliseconds: sum=-1000000 ll
by_val took 14135.603 milliseconds: sum=-1000000 ll

by_ref took 16047.643 milliseconds: sum=-1000000 ll
by_val took 14494.432 milliseconds: sum=-1000000 ll

by_ref took 15542.276 milliseconds: sum=-1000000 ll
by_val took 13843.121 milliseconds: sum=-1000000 ll
 
Nikolai Semko:

I wanted to put a grade on the KB and it didn't work. And judging by the fact that recent publications have no grades at all, it looks like I'm not the only one with this problem.

Yeah, it's not working.

 
fxsaber:
My ref and val loops have the same code (the comparison is more or less correct), but yours is different
 
A100:
In my loops ref and val have the same code (comparison is more or less correct), while yours is different

Yes, different. But the question remains valid. Why is val-variant noticeably faster than ref?

 
fxsaber:

Yes, different. But the question remains valid. Why is the val-variant noticeably faster than ref?

Maybe the ref variant is less optimized by the compiler - who knows.

To my mind, both variants should compile nearly the same code or even completely the same code but the compiler handles things differently

 
TheXpert:

Maybe the ref variant is worse optimized by the compiler - who knows.

Seems to me both should compile roughly the same code or even exactly the same, but the compiler thinks differently

The question was actually meant for developers, so that there is a basis for discrepancies in the reasons for discrepancies.

Usually I try to pass everything by reference for speed. And this was justified in some builds. But it seems to me now.

 
Alexey Kozitsyn:

+1. Something seems to be broken. Bild 1881 x64. Win 10. On startup it loads CPU by 20+% (i5 8600k) and RAM by 650-700mb (no increase).

Task manager shows "Not responding" status.

And the other terminal 1881 (not Opening) runs fine.

Added:

Eventually it did boot up. However it took extremely long to boot - that's not normal. Then I closed terminal and opened it again. Opened instantly. Apparently there was some problem with data loading.

The solution was to reinstall the terminal. I accidentally took out the folder with all the settings and graphs) I drew everything again, but it works like clockwork now.
 
reztor:
The solution was to reinstall the terminal. I also accidentally deleted the folder with all the settings and graphs) I drew everything again, but it works like clockwork now.

This was not necessary. You could simply delete the news.dat file.

 
TheXpert:

maybe the ref variant is worse optimised by the compiler - who knows?

To my mind, both options should compile approximately the same code or even completely the same code, but the compiler counts in a different way

What's the connection between the final code and its execution time?

For example, let's change . .. so that the loops become absolutely identical

#define  ITERATIONS 1000000
void OnStart()
{
   string Str = "1";
   for (int i = 0; i < 10; i++)
     Str += Str;
   for (int j = 0; j < 5; j++)
   {
     {
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++)
           sum += by_ref(string(rand()) + Str);
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_ref", time/1000.0, sum);
     }
     {
        ulong time = GetMicrosecondCount();
        ulong sum = 0;
        for(int i=0; i<ITERATIONS; i++)
           sum += by_val(string(rand()) + Str);
        time = GetMicrosecondCount() - time;
        printf("%s took %.3f milliseconds: sum=%dll", "by_val", time/1000.0, sum);
     }    
     Print("");
   }
}
int by_ref(string var){ return int(var) % 100; }
int by_val(string var){ return int(var) % 100; }

The result..:

by_ref took 18354.547 milliseconds: sum=1865600ll
by_val took 18318.319 milliseconds: sum=1861628ll

by_ref took 18416.747 milliseconds: sum=1904488ll
by_val took 18221.978 milliseconds: sum=1907860ll

by_ref took 18301.009 milliseconds: sum=1757988ll
by_val took 18545.258 milliseconds: sum=1949720ll

by_ref took 18373.648 milliseconds: sum=1867160ll
by_val took 17972.432 milliseconds: sum=1760308ll

by_ref took 19426.076 milliseconds: sum=1795564ll
by_val took 19177.485 milliseconds: sum=1826360ll

is about the same... Or do you think that the compiler has created different code in the same loops?

Let me remind you - there was something like this

by_ref took 13889.424 milliseconds: sum=-1000000ll
by_val took 14135.603 milliseconds: sum=-1000000ll

and different code... and there's about the same difference in time, but the code and the functions themselves are exactly the same

Reason: