I am trying to measure the execution time of a dot product, but I am finding difference depending on the variable used for storing the final result, i.e., when using an integer the result is 0ms but when using an element of array the time is much higher.
Could it be related with the compiler, when using an integer variable, is able to perform the vectorization of the loop?
Here is my code
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;
void main(int argc, char* argv[])
{
int* a = new int[2000000000];
for (unsigned long long i = 0; i < 2000000000; i++)
a[i] = 1;
clock_t t1 = clock();
int nResult = 0;
for (unsigned long long i = 0; i < 2000000000; i++)
nResult += a[i] * a[i];
clock_t t2 = clock();
cout << "Execution time = " << (int)(1000 * ((t2 - t1) / (double)CLOCKS_PER_SEC)) << " ms" << endl;
t1 = clock();
int b[1] = {0};
for (unsigned long long i = 0; i < 2000000000; i++)
b[0] += a[i] * a[i];
t2 = clock();
cout << "Execution time = " << (int)(1000 * ((t2 - t1) / (double)CLOCKS_PER_SEC)) << " ms" << endl;
delete[] a;
getchar();
return;
}
And here is the output
Execution time = 0 ms
Execution time = 702 ms
Thanks in advance for your help