mercredi 24 avril 2019

Why float division is faster than integer division in c++?

Consider the following code snippet in C++ :

First Block

const int size = 500000000;
int sum =0;
int *num1 = new int[size];
int *num2 = new int[size];
for (int i = 0; i < size; i++)
{
    sum +=(num1[i] / num2[i]);
}

Second Block

const int size = 500000000;
float *num1 = new float [size];  
float *num2 = new float [size];
for (int i = 0; i < size; i++)
{
    sum +=(num1[i] / num2[i]);
}

I expected that first block runs faster because it is integer operation . But the Second block is considerably faster , although it is floating point operation . here is results of my bench mark : Division:

Type    Time
uint8   879.5ms
uint16  885.284ms
int     982.195ms
float   654.654ms

My system spec: CPU core i7-7700 ,Ram 64GB

Aucun commentaire:

Enregistrer un commentaire