How to add arrays?
Hi, I''m using 3-float arrays for my vectors like so:
typedef float vector[3];
Instead of the usual class:
class vector
{
public:
float x, y, z;
};
Question: how can I implement operators, such as:
vector a, b, c;
a = b + c;
This is easy to do in a class but how do I do it with a 3-float array?
The old C method I find cumbersome:
void AddVectors ( vector a, vector b, vector &result )
{
result[0] = a[0] + b[0];
result[1] = a[1] + b[1];
result[2] = a[2] + b[2];
}
I would like something like:
vector operator+(vector& a, vector& b)
{
vector temp;
temp[0] = a[0] + b[0];
temp[1] = a[1] + b[1];
temp[2] = a[2] + b[2];
return temp;
}
But a function cannot return an array...
Is there any way out there to add two arrays together with a simple operator?
Thanks for your help.
I doubt this can be even done in C++.
Is there any good reason why are you not using classes?
You should never let your fears become the boundaries of your dreams.
Is there any good reason why are you not using classes?
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
September 16, 2003 03:18 PM
Why don''t you try just creating a wrapper around your array
like so:
class Vector3f {
// Define constructors
// If you need the array itself
const float* data() const { return m_data; }
// Define whatever ops you need
private:
float m_data[3];
};
like so:
class Vector3f {
// Define constructors
// If you need the array itself
const float* data() const { return m_data; }
// Define whatever ops you need
private:
float m_data[3];
};
Good suggestion, I was thinking along the same line and did a test. Here is my vector class:
class vector
{
protected:
float data[3];
public:
vector();
vector(float a[]);
vector& operator=(vector& s);
virtual float& operator[](int index);
vector operator+(vector& s);
bool vector::operator==(vector& s);
};
So I speed tested this against a straightforward array and also the xyz vector class previously mentionned, and sure enough:
For 10''000''000 additions on my Celeron 900 MHz:
array classes: 1.92 seconds
xyz classes: 0.811 seconds
arrays: 0.421 seconds
Which answers the first question, why use arrays? But I still need to use operators, as the speed gained is often lost because my code is rendered more complicated.
class vector
{
protected:
float data[3];
public:
vector();
vector(float a[]);
vector& operator=(vector& s);
virtual float& operator[](int index);
vector operator+(vector& s);
bool vector::operator==(vector& s);
};
So I speed tested this against a straightforward array and also the xyz vector class previously mentionned, and sure enough:
For 10''000''000 additions on my Celeron 900 MHz:
array classes: 1.92 seconds
xyz classes: 0.811 seconds
arrays: 0.421 seconds
Which answers the first question, why use arrays? But I still need to use operators, as the speed gained is often lost because my code is rendered more complicated.
quote:
Original post by Keermalec
For 10''000''000 additions on my Celeron 900 MHz:
array classes: 1.92 seconds
xyz classes: 0.811 seconds
arrays: 0.421 seconds
Which answers the first question, why use arrays?
A benchmark can only give you an answer to the question you ask. That means, your benchmack shows you that plain arrays are faster than xyz/array classes when doing 10 million additions in (I assume) a for loop. On top of that, did you use a propely implemented class and compile your code in release mode with optimization on? If not, then your benchmark does not tell you how the class behaves in your engine/game, using a properly implemented class and when compiling a release build.
Could you post xyz class + operator code. And the test program you used for testing.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
Brother Bob, yes this was tested with a release version. What do you mean by "optimisation on"?
Darkwing: here are the classes and the speed testing code (the array class was taken from Emile C. Chi October 1997 http://domanski.cs.csi.cuny.edu/chi/330/arrays.htm). Hope you find this useful:
[edited by - Keermalec on September 29, 2003 1:24:10 PM]
Darkwing: here are the classes and the speed testing code (the array class was taken from Emile C. Chi October 1997 http://domanski.cs.csi.cuny.edu/chi/330/arrays.htm). Hope you find this useful:
#include <stdio.h>#include <windows.h> #include <mmsystem.h>#include <math.h>#include <iostream.h>#include <time.h>#define MC_NUMVALUES 10000000 // Number of tests to run// Regular arraytypedef float M3float[3];// Adds arraysvoid mveAddVec( M3float v1, M3float v2, M3float &result ){ result[0] = v1[0] + v2[0]; result[1] = v1[1] + v2[1]; result[2] = v1[2] + v2[2];}// xyz classclass Mvector{ public: float x, y, z; Mvector (float X, float Y, float Z) { x = X; y = Y; z = Z; } Mvector inline operator+(Mvector &a) { return Mvector(x + a.x, y + a.y, z + a.z); }; Mvector () {} ~Mvector () {} };class array // base class for arrays{ protected: float data[3]; // pointer to data public: array(); // constructor M array(float a[]); // constructor array& operator=(array& s); // overloaded assignment virtual float& operator[](int index); // virtual overloaded [] can be lvalue array operator+(array& s); // overloaded + bool array::operator==(array& s); // overloaded ==};array array::operator+(array& op2) // overloaded +{ array temp; for (int i=0; i<3; i++) temp.data[i] = data[i] + op2.data[i]; return temp;}int main (void){ // Adding class arrays float startTime, endTime, totalTime; startTime = (float)clock()/CLOCKS_PER_SEC; for ( int a = 0; a < M_NUMVALUES; a++ ) ac3 = ac1 + ac2; endTime = (float)clock()/CLOCKS_PER_SEC; totalTime = endTime - startTime; printf("Adding %i class arrays: % 5.6f seconds\n\n", M_NUMVALUES, totalTime); // Adding arrays startTime = (float)clock()/CLOCKS_PER_SEC; for ( a = 0; a < M_NUMVALUES; a++ ) mveAddVec(a1,a2,a3); endTime = (float)clock()/CLOCKS_PER_SEC; totalTime = endTime - startTime; printf("Adding %i arrays: % 5.6f seconds\n\n", M_NUMVALUES, totalTime); // Adding xyz classes startTime = (float)clock()/CLOCKS_PER_SEC; for ( a = 0; a < M_NUMVALUES; a++ ) mvc1 = mvc2 + mvc3; endTime = (float)clock()/CLOCKS_PER_SEC; totalTime = endTime - startTime; printf("Adding %i xyz vectors: % 5.6f seconds\n\n", M_NUMVALUES, totalTime); return 0;}
[edited by - Keermalec on September 29, 2003 1:24:10 PM]
That test it totaly bogus. Any half-decent compiler will optimize for-loop away.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
You should never let your fears become the boundaries of your dreams.
What I meant with "optimization on" is that any decent compiler have the ability to optimize the resulting code, and not just do a blind line-by-line translation of the sourcecode into machine code, and you should use that feature. I tried the code you posted (slightly modified version, since the code as it is won''t compile) with my compiler (Intel 6), and the result wasn''t unexepcted at all; 0 for all tests. Looking at the assembly output, all loops was removed becuase they don''t do anything, and the compiler noticed that and removed them.
As Darkwing said, that test really doesn''t tell you anything usefull. I mean, how often do you perform 10 million vector additions, using the same vectors for all 10 million additions? If you at least use different vectors there may be situations, but then you also have other issues, like the memory. If you want a usefull result, perform the benchmark in a REAL program.
It''s always easy to get some numbers, but getting the right numbers and interpreting them correct can be difficult.
As Darkwing said, that test really doesn''t tell you anything usefull. I mean, how often do you perform 10 million vector additions, using the same vectors for all 10 million additions? If you at least use different vectors there may be situations, but then you also have other issues, like the memory. If you want a usefull result, perform the benchmark in a REAL program.
It''s always easy to get some numbers, but getting the right numbers and interpreting them correct can be difficult.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement