   accessing arrays faster
  i noticed that accessingfloat test;is at least 2 time faster than accessing an element offloat test[260000]however i need to have a storage place in memory working like an array but which could be accessed as fast as a simple float.Do you know a method which could solve this performance issue ? 
  I think that you'll find that the speed of accessing an array does nothing whatsoever to affect the overall performance of your application.  My bet would be that if there are bottlenecks in your game/application they can best be fixed by changing algorithms or data structures.  i wouldn't worry about the speed of accessing an array, it's the least of your worries...  you may have done this already, but do some profiling on your code before you worry about this array thing so you can see if it's actually the bottleneck that's slowing down your app.  Otherwise you're really just wasting your time pre-optimizing something that's probably not really a problem anyway.-me   [edited by - Palidine on February 9, 2004 4:24:13 PM];  That''s not going to be a problem, even if you think it is now. Use a profiler to find the real bottlenecks. ;  I would agree with previous poster that this is unlikely to have a big effect.  I guess the time taken for an array access is because the compiler must move to the offset in the array (i.e. the index of the element).It probably also depends if the single floating point variable was created on the heap, the stack, the global memory.  If it was cached, etc.If you insist on this micro-optimization, and you''re accessing a single element in an array many times in one loop cycle, then use a pointer (or even reference) to that element and see if it''s any faster.  My guess is that most compilers would optimize this anyway:float test[260000];for(int index = 0; index < 259999; ++index) {  if(test[index] > 0 && test[index+1] < 50) {    // whatever  }  else if(test[index] >= 50 && test[index+1] < 100) {    // whatever  }  // ... lots more here...}instead of test[index] and test[index+1], do this:float test[260000];float* fp = 0;float* fpnext = 0;for(int index = 0; index < 259999; ++index) {  fp = &(test[index]);  fpnext = &(test[index+1]);  if(*fp > 0 && *fpnext < 50) {    // whatever  }  else if(*fp >= 50 && *fpnext < 100) {    // whatever  }}Note that this still requires a dereference every time but again this is probably all optimized by the compiler.Regards,Jeff ;  Array access is "slower" because it requires a multiply- which is one of the slower cpu operations.But as has already been said, this kind of optimization is almost always useless. Imagine a guy who is trying to make his car go faster by removing specks of dust from the windsheild, and you''ll have an idea of how much you are wasting your energy. ;  Just out of interest pinacolada, where does the multiply come from? You would think the address would be pushed and the offset added.   blah blah blah. ;  I have heard that the address being recorded is only the address of element #0. If you are accessing other elements (element > 0), it needs to find where it is by multiplying the index or something.I never pay attention to that kind of stuff anyway. ;  Just one thing: If your code is optimized to the point that the infinitesmal difference of time added to offset from a pointer makes a difference, it is probably already optimized as much as it''s going to get. If it is that optimized already, you are truly a god.Don''t worry about pointer offsetting. However much you may think it is a bottleneck, it is not. Beware premature, inappropriate optimization; it can be worse than no optimization at all. GolemBlender--The Gimp--Python--Lua--SDLNethack--Crawl--ADOM--Angband--Dungeondweller;quote:Original post by Chris Hare  Just out of interest pinacolada, where does the multiply come from? You would think the address would be pushed and the offset added.   The multiplication comes into play because the array might not be in bytes.  An integer takes up 4 bytes on most platforms, so accessing the 10th array element means accessing the 40th byte.;quote:Original post by rypyr  quote:Original post by Chris Hare   Just out of interest pinacolada, where does the multiply come from? You would think the address would be pushed and the offset added.   The multiplication comes into play because the array might not be in bytes.  An integer takes up 4 bytes on most platforms, so accessing the 10th array element means accessing the 40th byte. Gotcha. 