-
Why is this faster
Hi everyone, I'm working on a program here and came across this discovery
Code:
for(i=0;i<Nli++){
while(arr[i] * h[index[i]] <= h[num-1]) index[i]+=1;
}
AND
Code:
for(i=0;i<Nli++){
index=0;
while(arr[i] * h[index] <= h[num-1]) index++;
}
When i run both in my program, the compile time for the first code is 0 seconds however for the second code it takes 5 second.
Can someone explain to me why is it that using index as an array is faster?
Thanks
-
Do you mean compile time or run time?
Two different things!!
-
You do realize these two code are entirely different.
1st case, index is array. index[i]+=1;
2nd , index is just an integer. perhaps? show the declaration!
-
Ok I think in the second one index is the address of the array?
Or is it?
It would help if you printed the values of the variables ie
arr[i], h[index] and [num-1]
then you would have an idea of what is going on, maybe.
I think I am agreeing with Bayint, 'index' is probably a huge 64 bit number such as 9347593475972
-
I'm not surprised they take differing amounts of time. It looks to me like you've made a 'floptimisation'. It's not just slower, it doesn't actually do the same thing any more, and would clearly give different results.
Learn the difference between compile-time and run-time.