Advertisement

How to add arrays?

Started by September 16, 2003 03:04 PM
21 comments, last by Keermalec 21 years, 5 months ago
Oops, right Brother Bob, I did mix the two around, but I also mixed them back the other way in the final line (vecTime/arrTime) therefore the comparison between xyz classes and arrays still goes the way I initially stated. Here is the corected code, with some other small corrections.

Get it here

Proving that arrays are faster than classes,
By Keermalec, 30 september 2003
----------------------------------
1000000 random vectors and arrays created

Adding 1000000 arrays: 0.160000 seconds
Adding 1000000 vectors: 0.210000 seconds

Calculating 1000000 additions of random vectors took:

0.210 seconds using the xyz vector class
0.160 seconds using arrays

Therefore arrays are 1.313 x faster than xyz classes.

Thanks to Brother Bob and _DarkWing_ for nagging me until I wrote this
----------------------------------


Please try it out to confirm.


Sneftel: Like I told Fruny, you may be right about my unoptimised ititialisation but this has no effect on the problem at hand, and to prove it I have implemented your comment in the code above, with no visible effect on the result.

DarkWing, maybe your compiler does not handle the rand() function the same way mine does and all your vectors are the same. That may explain why they all get optimised out and you get 0 duration. Maybe you would like to implement anothe random float generation function.



[edited by - Keermalec on October 2, 2003 3:10:15 PM]
I give up. I don''t have the time or the enegry to presuade you to use vectors instead of arrays... Do what you will. Maybe you''ll learn in time.

And to anwser your question: my rand() works just fine (as simple rand can ). And compiler did not remove for loop. But you might want to replace clock() with something actualy usefull for timing (like QueryPerformanceCounter).

You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Advertisement
Darkwing, apart from the fact that arrays are faster than vectors, which has just been proven, OpenGL was designed to use arrays, and not vectors. Why else would there be functions such as glNormal3fv, glVertex3fv, or glTexCord2fv which require arrays?

Classes and structs are great for organising complex data, but come with a performance hit, which is not a problem in most applications where 0.4 microseconds lost per operation does not amount to much. In game programming, however, the 20-30% speed gain in using arrays can give you just what you need to make your scene just a little more complex or allow useage of better visual effects.

If you have no time or energy to prove the contrary (provided you can of course), then you have no time or energy to write a good game engine.

This topic is closed to new replies.

Advertisement