sorry,could you explain the "all loops don''t do anything" ?
are you talking about loops in assembly or inside the C++ code? because in the code they ARE doing something...
and how can it optimize it removing loops?
(i''m not really fond in assembly...)
There aren''''t problems that can''''t be solved with a gun...
How to add arrays?
It has nothing to do with assembly...
Look at this piece of code
this loop will be thrown out by compiler becouse results of loop are never used. Or in worst case compiler will just replace loop with simple "x=3" since that can be evaluated at compile-time.
If you want to build a valid test loop you have to: a) use random start point and b) use result of one iteration in the next one. c) actualy use result of loop.
example :
You should never let your fears become the boundaries of your dreams.
Look at this piece of code
int x, y = 1, z = 2;for ( int i=0; i<1000000; ++i ) x = y + z;
this loop will be thrown out by compiler becouse results of loop are never used. Or in worst case compiler will just replace loop with simple "x=3" since that can be evaluated at compile-time.
If you want to build a valid test loop you have to: a) use random start point and b) use result of one iteration in the next one. c) actualy use result of loop.
example :
int x = rand(500), y = 1;for ( int i=0; i<100000; ++i ) x = f( x, y );cout << x;
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
quote:
Original post by Keermalec
Mvector (float X, float Y, float Z) { x = X; y = Y; z = Z;}
Ugh...
Mvector (float X, float Y, float Z) : x(X), y(Y), z(Z){}
Since you're complaining about performance, you'd better make sure you don't do unnecessary initializations.
Also, acquaint yourself with the Named Return Value Optimization, and how to help your compiler apply it.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[edited by - Fruny on September 29, 2003 6:51:38 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
OK, latest first:
Fruny: thanks for the tip on optimising initialisation. However, please note the performance gain there does not affect the speed of vector operations, which is what I was measuring.
_Darkwing_: My compiler has not "optimised the for loop away" as you say. Here are the results:
class arrays: 1.92 seconds
arrays: 0.421 seconds
xyz vectors: 0.811 seconds
I did initially create 100''000 random vectors but the memory requirements and time taken were too high for 10''000''000 vectors, plus I noticed my compiler was not optimising the for loop (bty how do you set such optimisation on in VC++6?) so I simplified the code to use always the same vectors.
Darkwing, if this really is bogus, why don''t you write a working proof of the contrary and post it here?
m_e_my_self: I do not want to use classes because until someone can prove they are just as fast as arrays, I do not see the use in dividing my frame rate by two just to make my code look nicer.
Brother Bob: You are right, this code will not compile as it is a patchwork of code snippets, the whole app was way too long to post here. Note I do not get 0 seconds like you do as my compiler does not seem to optimise the loop out like yours.Btw, here is the missing initialisation code:
Fruny: thanks for the tip on optimising initialisation. However, please note the performance gain there does not affect the speed of vector operations, which is what I was measuring.
_Darkwing_: My compiler has not "optimised the for loop away" as you say. Here are the results:
class arrays: 1.92 seconds
arrays: 0.421 seconds
xyz vectors: 0.811 seconds
I did initially create 100''000 random vectors but the memory requirements and time taken were too high for 10''000''000 vectors, plus I noticed my compiler was not optimising the for loop (bty how do you set such optimisation on in VC++6?) so I simplified the code to use always the same vectors.
Darkwing, if this really is bogus, why don''t you write a working proof of the contrary and post it here?
m_e_my_self: I do not want to use classes because until someone can prove they are just as fast as arrays, I do not see the use in dividing my frame rate by two just to make my code look nicer.
Brother Bob: You are right, this code will not compile as it is a patchwork of code snippets, the whole app was way too long to post here. Note I do not get 0 seconds like you do as my compiler does not seem to optimise the loop out like yours.Btw, here is the missing initialisation code:
float a1[3] = { 23, 54, 67 }; float a2[3] = { 65, 34, 98 }; float a3[3] = { 121, 454, 78 }; array ac1(a1), ac2(a2), ac3(a3); Mvector mvc1 = mcLoadVector( 23, 54, 67 ); Mvector mvc2 = mcLoadVector( 65, 34, 98 ); Mvector mvc3 = mcLoadVector( 121, 454, 78 );
OK results :
I removed "class array" since its useless.
And next time like Brother Bob said use a real program to test this on. The point is that even if adding arrays was faster it wouldn''t matter. Have you evenused a profiler to see if this is even a bottleneck...
You should never let your fears become the boundaries of your dreams.
I removed "class array" since its useless.
Compiled in debug mode : adding 10000000 arrays : 0.802adding 10000000 vectors : 1.882Compiled in release mode : adding 10000000 arrays : -1.49012e-008adding 10000000 vectors : -1.49012e-008
And next time like Brother Bob said use a real program to test this on. The point is that even if adding arrays was faster it wouldn''t matter. Have you evenused a profiler to see if this is even a bottleneck...
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
quote:
Original post by Keermalec
I did initially create 100''000 random vectors but the memory requirements and time taken were too high for 10''000''000 vectors, plus I noticed my compiler was not optimising the for loop (bty how do you set such optimisation on in VC++6?) so I simplified the code to use always the same vectors.
You see, when you simplified the code, you also simplified the situation you benchmarked. When you use the same vectors, the memory used will not affect the time that much as it will likely fit in the cache for fast access. But in a real situation, the memory of 10 million vectors WILL affect the result, and that is why you CAN''T make a simplification like that.
quote:
Original post by Keermalec
I do not want to use classes because until someone can prove they are just as fast as arrays, I do not see the use in dividing my frame rate by two just to make my code look nicer.
I don''t know if "dividing my frame rate by two" was taken just like that, or if you really based it on the fact that your benchmark showed that arrays was twice as fast as xyz vectors. If it was becuase of the benchmark, then I have to ask you: Does your program do anything else that adding vectors? If it''s doing other things aswell, that part of the program will be unaffected, causing the performance hit to be lower. The less the part doing vector additions, the less the actual performance hit will be.
OK you made me do it, here is the test prog. I've set it to "only" 1'000'000 operations because at 10 mill I was getting a lot of disk swapping and performance dropped significantly due to this.
Here is the VC++6 project if you want it:
http://www.christov.com/ogl/test.zip
And here is the code, I get a performance bonus of about 25% using arrays instead of classes. Other operations seem equally as fast, though I leave it up to you to implement them:
[edited by - Keermalec on September 30, 2003 6:22:26 PM]
[edited by - Keermalec on September 30, 2003 6:28:15 PM]
Here is the VC++6 project if you want it:
http://www.christov.com/ogl/test.zip
And here is the code, I get a performance bonus of about 25% using arrays instead of classes. Other operations seem equally as fast, though I leave it up to you to implement them:
// Test programme to verify that array operations are faster than classes// Keermalec, 30 September 2003#include <stdio.h> // Standard C/C++ Input-Output, FILE, fopen(), sprintf(), fwrite(), fclose()#include <windows.h> // Standard Header For MSWindows Applications#include <mmsystem.h> // For timeGetTime()#include <math.h> // Math functions such as sqrt(), sin(), cos()#include <iostream.h>#include <time.h> // for clock()#define M_NUMVALUES 1000000 // Number of tests to run// Our standard array, simple isn't it?typedef float M3float[3];// Our xyz vector classclass Mvector{ public: float x, y, z; Mvector (float a, float b, float c) { x = a; y = b; z = c; } Mvector inline operator+(Mvector &a) { return Mvector(x + a.x, y + a.y, z + a.z); }; Mvector () {} ~Mvector () {} };// Array addiionvoid arrAdd(M3float a, M3float b, M3float &c){ c[0] = a[0] + b[0]; c[1] = a[1] + b[1]; c[2] = a[2] + b[2];}// The following funtion generates a random float from rangelow to rangehigh// uses rand() function, which generates a random int from 1 to 32768// Tested and confirmed on 010927float mRandGen(unsigned int seed, float rangelow, float rangehigh){ srand(seed); // defines seed to be used in rand() return ((rand()*(rangehigh-rangelow))/32768)+rangelow;}int main (void){ printf("\nProving that arrays are faster than classes,\n"); printf("\nBy Keermalec, 30 september 2003\n"); printf("\n----------------------------------\n"); // Some useful variables unsigned int RandSeed, a, b; float startTime, endTime, vecTime, arrTime; // 3 sets of arrays and 3 sets of vectors M3float *array_1; array_1 = (M3float*) calloc (M_NUMVALUES, sizeof M3float); M3float *array_2; array_2 = (M3float*) calloc (M_NUMVALUES, sizeof M3float); M3float *array_3; array_3 = (M3float*) calloc (M_NUMVALUES, sizeof M3float); Mvector *vector_1; vector_1 = (Mvector*) calloc (M_NUMVALUES, sizeof Mvector); Mvector *vector_2; vector_2 = (Mvector*) calloc (M_NUMVALUES, sizeof Mvector); Mvector *vector_3; vector_3 = (Mvector*) calloc (M_NUMVALUES, sizeof Mvector); // Create the random seed RandSeed = mRandGen(((unsigned)time( NULL )),1,32768); // Load random values for ( a= 0; a < M_NUMVALUES; a++ ) { for ( b = 0; b < 3; b++ ) { array_1[a][b] = mRandGen(RandSeed*rand(),-100.0f,100.0f); array_2[a][b] = mRandGen(RandSeed*rand(),-100.0f,100.0f); } vector_1[a].x = array_1[a][0]; vector_1[a].y = array_1[a][1]; vector_1[a].z = array_1[a][2]; vector_2[a].x = array_2[a][0]; vector_2[a].y = array_2[a][1]; vector_2[a].z = array_2[a][2]; } printf("\n%i random vectors and arrays created\n", M_NUMVALUES); // Now do some additions // Array addition startTime = (float)clock()/CLOCKS_PER_SEC; for ( b = 0; b < M_NUMVALUES; b++ ) { arrAdd(array_1[b], array_2[b], array_3[b]); } endTime = (float)clock()/CLOCKS_PER_SEC; vecTime = endTime - startTime; printf("\nAdding %i arrays: % 5.6f seconds\n", M_NUMVALUES, vecTime); // Mvector addition startTime = (float)clock()/CLOCKS_PER_SEC; for ( b = 0; b < M_NUMVALUES; b++ ) { vector_3[b] = vector_1[b] + vector_2[b]; } endTime = (float)clock()/CLOCKS_PER_SEC; arrTime = endTime - startTime; printf("Adding %i vectors: % 5.6f seconds\n", M_NUMVALUES, arrTime); printf("\nCalculating %i additions of random vectors took:", M_NUMVALUES); printf("\n\n% 5.3f seconds using the xyz vector class", vecTime); printf("\n% 5.3f seconds using arrays", arrTime); printf("\n\nTherefore arrays are % 5.3f x faster than xyz classes.", arrTime/vecTime); printf("\n\nThanks to Brother Bob and _DarkWing_ for nagging me until I wrote this\n"); printf("\n----------------------------------\n"); return 0;}
[edited by - Keermalec on September 30, 2003 6:22:26 PM]
[edited by - Keermalec on September 30, 2003 6:28:15 PM]
Kermaleec, your vector code is slower because you don''t understand the proper use of initializer lists. By changing the vector constructor to:
The two methods even out. I suggest taking a look at initializer lists; failure to use them is a common source of inefficiency for programmers.
How appropriate. You fight like a cow.
Mvector (float a, float b, float c) : x(a), y(b), z(c) {}
The two methods even out. I suggest taking a look at initializer lists; failure to use them is a common source of inefficiency for programmers.
How appropriate. You fight like a cow.
E:\CppProjects\whatever\Release>whatever.exe
Proving that arrays are faster than classes,
By Keermalec, 30 september 2003
----------------------------------
1000000 random vectors and arrays created
Adding 1000000 arrays: 0.080000 seconds
Adding 1000000 vectors: 0.080000 seconds
Calculating 1000000 additions of random vectors took:
0.080 seconds using the xyz vector class
0.080 seconds using arrays
Therefore arrays are 1.000 x faster than xyz classes.
Thanks to Brother Bob and _DarkWing_ for nagging me until I wrote this
----------------------------------<br> </pre>
You should never let your fears become the boundaries of your dreams.
Nice piece of proof you got there 
By the way, you have mixed the two variables the second time you print them. The correct output should be.
edit: A possible source of the mix up could be that you use vecTime for the array time, and arrTime for the vector time.
And here's some additional results.
[edited by - Brother Bob on October 1, 2003 7:39:33 AM]

1000000 random vectors and arrays createdAdding 1000000 arrays: 0.141000 secondsAdding 1000000 vectors: 0.125000 secondsCalculating 1000000 additions of random vectors took: 0.141 seconds using the xyz vector class 0.125 seconds using arraysTherefore arrays are 0.887 x faster than xyz classes.
By the way, you have mixed the two variables the second time you print them. The correct output should be.
edit: A possible source of the mix up could be that you use vecTime for the array time, and arrTime for the vector time.
0.125 seconds using the xyz vector class 0.141 seconds using arrays
And here's some additional results.
Adding 10000000 arrays: 1.593000 secondsAdding 10000000 vectors: 1.359000 secondsAdding 100000 arrays: 0.016000 secondsAdding 100000 vectors: 0.015000 seconds
[edited by - Brother Bob on October 1, 2003 7:39:33 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement