Click here to Skip to main content
15,888,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
{
  int *z = new int[100];
  z[1]=1;
  z[2]=4;
  z[3]=5;
  ...
  z[100]=20;
}

in the above code, the assignment sequence is from z[0] to z[100],
if I change the order as below,will it become slow
C++
{
  int *z = new int[100];
  z[10]=4;
  z[3]=5;
  z[100]=20;
  ...
  z[2]=4;
}
Posted

Memory operations are executed in the sub nano micro second range, so the order of the indexing makes no difference to your program.
 
Share this answer
 
v2
Comments
Angela2012 27-Jun-12 9:23am    
Thank you for your help.
Aescleal 28-Jun-12 4:08am    
They're not sub ns by any stretch of the imagination - even if something's in the level 1 cache then it's going to take several nanoseconds to read. Writing will be faster if you've got a writeback cache although multiple cores/processors slow that down. If it's not in any cache (which is very likely if you've just allocated a block of it) then it's going to take at least 40ns to read.

Sequential access is at least 10 times as fast as random access as it uses cache lines that are already filled.
Mehdi Gholam 28-Jun-12 4:21am    
I stand corrected nano -> micro, thanks. Obviously the size of the array will effect the timings if it is over the L1 cache size etc. for 100 items it will probably be the same as it will be in the cache already.

The point should be made to the OP that this level of optimization will make little difference to them unless they are doing really advanced stuff, normally a better algorithm will offer better performance.
Aescleal 28-Jun-12 6:19am    
No probs, have a 5 for the revised answer
Mehdi Gholam 28-Jun-12 6:27am    
Cheers :)
The sequence in which you execute the assignments influences the performance of the processor cache. That's probably the reason why variant 1 is working faster than variant 2. That should however only be significant if you execute that code many times. A hundred integer assignments just take a very tiny fraction of time by themselves on today's processors.
 
Share this answer
 
Comments
Angela2012 27-Jun-12 9:38am    
thank you for your answer, the above code is just an example , in my algorithm, the value of polynomial with different variables is assigned to z[i],like this:

{
for(int k=0;k<600;k++)
{
x=x_array[k];

y=y_array[k];
z[1]=polynomial1(x,y);
z[2]=polynomial2(x,y);

...
z[100]=polynomial100(x,y);
}
}

in this case, will it become slow if the sequence changed
nv3 27-Jun-12 9:43am    
It will be just slightly slower, but probably unnoticeable. The time for a function call of youe polynomial functions is probably much longer than the assignment of the value to z[i] plus any effects a cache-miss might have.
Angela2012 27-Jun-12 10:08am    
OK,thank you very much.
Aescleal 28-Jun-12 6:20am    
The amount of data you're using will exceed the amount of level 1 cache on most processors. In this case you want to access variables sequentially. It'll be faster and it's a more natural way of doing it for both the programmer and the processor. It also avoids the cache wrecking effects of thread switches as well.

Having said all that, if performance bothers you then measure it, that's the only way to know for sure.
By the way... if you generate a dynamic array like yours:
int *z = new int[100];

The valid indexes are from 0-99, not from 1-100 (array indexes are zero based).

z[100] = 20; //out of bounds, likely crash
z[99] = 20; //last valid index
 
Share this answer
 
Comments
Angela2012 27-Jun-12 10:36am    
thank you for reminding ,I always make this kind of mistakes.
Albert Holguin 27-Jun-12 10:40am    
Practice makes perfect... zero based indexing is really only natural to programmers and probably not others. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900