Which is faster - Floating-Point or Integer arithmetic? - page 9

 
Alain Verleyen:

@nicholishen 

So it's more complex than we thought. It depends of the hardware and the exact tests run. 

Anyway for the way you lead this discussion you deserve your ban.

Actually I can not ban me otherwise I will do...loool

So I changed my mind and removed the ban.

All this topic is a big lesson, hopefully.

On the technical aspects, we all had a part right and a part wrong, but we could not communicate quietly.

We have all a part in this clash and it's a pity. I hope every participant will learn from it. I will for sure.

 

This topic is quite important for me, and in light of recent posts, I've decide to start fresh and look at every point once again.

However, I am going to take some time to code my own tests from scratch for various situations, so that I can see for myself in detail what is going on and whether I can confirm or deny what has been stated so far.

Lets start fresh, putting aside animosities and taking a look at things once more, slowly and steadily!

 
Alain Verleyen:

Actually I can not ban me otherwise I will do...loool

So I changed my mind and removed the ban.

All this topic is a big lesson, hopefully.

On the technical aspects, we all had a part right and a part wrong, but we could not communicate quietly.

We have all a part in this clash and it's a pity. I hope every participant will learn from it. I will for sure.

@Alain Verleyen, I would like to request that you split off this thread from the original OP's query, because that got answered right at the beginning and the rest of the discussion is really one big long off-topic subject all on its own.

Maybe you can call the thread - "Which is faster - Floating-Point or Integer arithmetic?"

I ask this, because I would like to continue the discussion, but hopefully all parties will cool down and be more calm about it.

 
Fernando Carreiro:

@Alain Verleyen, I would like to request that you split off this thread from the original OP's query, because that got answered right at the beginning and the rest of the discussion is really one big long off-topic subject all on its own.

Maybe you can call the thread - "Which is faster - Floating-Point or Integer arithmetic?"

I ask this, because I would like to continue the discussion, but hopefully all parties will cool down and be more calm about it.

Done.

 

So....

 
nicholishen: So....

My intention is to start from the beginning, with fresh eyes and follow a slow, step-by-step,  more rigorous approach to analysing the issue, giving credit to each party where due, because it appears that all parties were both right and wrong in their assumptions and conclusions, contributing with both constructive and destructive information. So lets start fresh and redo things, collaborating to achieve a common goal instead of fighting among ourselves.

I am currently analysing each and every point that was discussed here so far, as well as every test code used and results shown (by all parties). So far, in my testing, some results favour Integer Arithmetic as faster while in other tests it favours Floating-point as being faster and I want to understand why that is and in what circumstances these differences occur. I don’t just want academic theoretical information, but actual usable information, that will help make EAs faster according to whatever is found to be valid for each situation.

@whroeder1 reminded us of the CPU/FPU situation, but I want to take it further and look at different hardware scenarios and what impact they have, as well as the differences between MT4 and MT5. I am also looking into known standardised benchmark tests in order to port them to MQL code for more rigorous analysis.

However, due to other prior commitments, I will not be able to focus 100% on this task with-in the next couple of days! @Alain Verleyen too is busy at the moment, so I ask that we pursue this issue more intensely from Thursday onward, so that we can all participate in a combined effort and not as separate individuals.

I also searched the Internet for similar situations in other languages, and found the following articles to be of interest which we can also discuss once we are all back here working together on this:

Should anyone find other such related articles, please post a link here so that we can use the information in analysing what impact they have on MQL.

Newsflash: Sometimes Using Float is Faster Than Using Int in Java
  • 2016.08.08
  • Stephan Rauh
  • www.beyondjava.net
Time and again, I’m surprised by the performance of modern CPUs. Actually, the speed boost astonishing me most took place a decade ago, give or take a few years. Before that, floating point operations were a lot slower than integer operations. When I learned my first assembly language, the 6502 processor was state of the art. This processor was...
 

I re-post the modified code (original code from @nicholishen), I just added some code to use the result of the addition num1+num2

      double v=Add(arrD1[i],arrD2[i]);
      sumd+=v;
      if(v>2000.)
         result++;

Then I ran it with 1E7 and 1E8 iterations and I got results which I can't explain. As said by @Fernando Carreiro, I don't have much time this week, so I will be happy if someone can provide some light about this.

2018.01.15 23:06:28.776 224626_2 (NZDUSD,H1) <int>: 98 ms for 10000000 iterations

2018.01.15 23:06:28.776 224626_2 (NZDUSD,H1) result = 9981256 sum=327648917016

2018.01.15 23:06:28.858 224626_2 (NZDUSD,H1) <double>: 81 ms for 10000000 iterations

2018.01.15 23:06:28.858 224626_2 (NZDUSD,H1) result = 9981256 sum=327648917016.000000


2018.01.15 23:07:30.459 224626_2 (NZDUSD,H1) <int>: 7078 ms for 100000000 iterations

2018.01.15 23:07:30.462 224626_2 (NZDUSD,H1) result = 99813298 sum=3276567693470

2018.01.15 23:07:43.736 224626_2 (NZDUSD,H1) <double>: 13271 ms for 100000000 iterations

2018.01.15 23:07:43.738 224626_2 (NZDUSD,H1) result = 99813298 sum=3276567693470.000000

You an also notice that the above results are quite different from the ones I posted earlier today (I just run the same unchanged script, except 1E7/1E8 iterations):

2018.01.15 10:43:44.698 224626_2 (EURUSD,M1) <int>: 3164 ms for 100000000 iterations

2018.01.15 10:43:44.702 224626_2 (EURUSD,M1) result = 99813497 sum=3276606316406

2018.01.15 10:44:24.734 224626_2 (EURUSD,M1) <double>: 40028 ms for 100000000 iterations

2018.01.15 10:44:24.736 224626_2 (EURUSD,M1) result = 99813497 sum=3276606316406.000000

It's the same computer, Windows 10 notebook (5 years-old), intel i7-2677M 1.80 GHz 4GB RAM. MT5 1745/64. Of course I have a lot of software running, so it's academic results for sure.

Files:
224626_2.mq5  3 kb
 
Alain Verleyen:

I re-post the modified code (original code from @nicholishen), I just added some code to use the result of the addition num1+num2

Then I ran it with 1E7 and 1E8 iterations and I got results which I can't explain. As said by Fernando, I don't have much time this week, so I will be happy if someone can provide some light about this.

It's the same computer, Windows 10 notebook (5 years-old), intel i7-2677M 1.80 GHz 4GB RAM. MT5 1745/64. Of course I have a lot of software running, so it's academic results for sure.

In contrast, here are the results on my machine (MTv5 build 1745 x64, Windows 10 build 16299 x64, Intel Core i7-4790T @ 2.70GHz, 16GB RAM, 960GB SSD Disk):

2018.01.16 04:26:14.747    224626_2 (EURUSD.m,H1)    <int>: 21 ms for 10000000 iterations
2018.01.16 04:26:14.747    224626_2 (EURUSD.m,H1)    result = 9981417 sum=327583648383
2018.01.16 04:26:14.767    224626_2 (EURUSD.m,H1)    <double>: 19 ms for 10000000 iterations
2018.01.16 04:26:14.767    224626_2 (EURUSD.m,H1)    result = 9981417 sum=327583648383.000000


2018.01.16 04:25:06.543    224626_2 (EURUSD.m,H1)    <int>: 209 ms for 100000000 iterations
2018.01.16 04:25:06.545    224626_2 (EURUSD.m,H1)    result = 99813581 sum=3276623426128
2018.01.16 04:25:06.745    224626_2 (EURUSD.m,H1)    <double>: 200 ms for 100000000 iterations
2018.01.16 04:25:06.745    224626_2 (EURUSD.m,H1)    result = 99813581 sum=3276623426128.000000

EDIT: from these two sets of results, @Alain Verleyen and my own, I repeat the following statement which applies to this case:

So far, in my testing, some results favour Integer Arithmetic as faster while in other tests it favours Floating-point as being faster and I want to understand why that is and in what circumstances these differences occur.

@nicholishen, can you run these same tests on your machine please and post the results you get (as well as your hardware configuration)?

EDIT2: And here are the results for the MT4 adaptation (MTv4 build 1090, same PC as above):

2018.01.16 04:48:40.313    224626_2 EURUSD.m,H1: <int>: 242 ms for 10000000 iterations
2018.01.16 04:48:40.313    224626_2 EURUSD.m,H1: result = 9981147 sum=327643353519
2018.01.16 04:48:40.563    224626_2 EURUSD.m,H1: <double>: 249 ms for 10000000 iterations
2018.01.16 04:48:40.563    224626_2 EURUSD.m,H1: result = 9981147 sum=327643353519.000000

2018.01.16 04:49:55.279    224626_2 EURUSD.m,H1: <int>: 2448 ms for 100000000 iterations
2018.01.16 04:49:55.279    224626_2 EURUSD.m,H1: result = 99813265 sum=3276538401261
2018.01.16 04:49:57.767    224626_2 EURUSD.m,H1: <double>: 2487 ms for 100000000 iterations
2018.01.16 04:49:57.767    224626_2 EURUSD.m,H1: result = 99813265 sum=3276538401261.000000
 

Can anyone shed some light on whether this subject has ever been discussed before on the Russian forum?

They tend to go much more in-depth into situations than we do here on the English one!

 

Alain, not sure how you got those results... I'm right where Fernando is on a 2.4 GHz i5. 

Fernando, thanks for also running these on MT4...that answers that for me... 

06:28:13.706 224626_2__1 (EURUSD,H1) <int>: 27 ms for 10000000 iterations

06:28:13.706 224626_2__1 (EURUSD,H1) result = 9981267 sum=327684089175

06:28:13.731 224626_2__1 (EURUSD,H1) <double>: 24 ms for 10000000 iterations

06:28:13.731 224626_2__1 (EURUSD,H1) result = 9981267 sum=327684089175.000000


Surprisingly, array read/write/sort performance is the same as well. That's where I thought <int> might have a more significant edge.  


#include <Arrays\ArrayInt.mqh>
#include <Arrays\ArrayDouble.mqh>
void OnStart()
{
//---
   srand(_RandomSeed);
   int iterations = (int)pow(10,4);
   CArrayDouble   arrD1;
   CArrayDouble   arrD2;
   CArrayInt      arrI1;
   CArrayInt      arrI2;
   arrD1.Reserve(iterations);
   arrI1.Reserve(iterations);
   arrD2.Reserve(iterations);
   arrI2.Reserve(iterations);
   
   for(int i=0;i<iterations;i++)
   {
      arrI1.Add(rand()%50000);
      arrI2.Add(rand()%50000);
   }
   for(int i=0;i<iterations;i++)
   {
      arrD1.Add((double)arrI1[i]);
      arrD2.Add((double)arrI2[i]);
   }
///////////////////
   ulong m = GetMicrosecondCount();
   for(int i=0;i<iterations;i++)
   {
      arrI1.Sort();
      arrI1.AssignArray(&arrI2);
   }  
   m=GetMicrosecondCount()-m;
   m/=1000;
   printf("<int>: %d ms for %d iterations",m,iterations);
//////////////////  
   m = GetMicrosecondCount();
   for(int i=0;i<iterations;i++)
   {
      arrD1.Sort();
      arrD1.AssignArray(&arrD2);
   } 
   m=GetMicrosecondCount()-m;
   m/=1000;
   printf("<double>: %d ms for %d iterations",m,iterations);
   
}

Reason: