vector class question
1) is there any overhead in using a vector class to represent the coordinates of a particle in a particle class, as lots of particles have to be updated very quickly. For example would it be quicker this way:
Class CParticle
{
CVector position;
}
in a loop: particle.position.x
or the way i''m currently doing it:
Class CParticle
{
int x,y,z;
}
in a loop: particle.x
and sometimes they''ll need to be substracted etc.. to get the distance between two particles so using a vector class would make it easier to understand.
2)Why do all vector classes I see use
class CVector
{
float v[4] //or v[3]
}
instead of an easier to understand
class CVector
{
float x,y,z
}
As to your first question, "quicker" is very dependant on circumstances. What compiler do you use, what processor, what libraries, how did you implement your classes/templates/inlinining/operators/etc/etc/etc, how do you actually use the code?
In the case of a particle system, where you''re talking about lots and lots of particles, one example concern is data cache coherency. Let''s say, for whatever reason, you decide that it''s important to modify all the X coords first, then Y coords, then Z. Then neither of your structures will be good for data cache: rather, you''d want three separate arrays (one each for X, Y and Z).
Sometimes the best way is to try both ways, time them in real-use circumstances, and pick the better.
On your second question as to why an array rather than x,y,z. My guess quite often is personal preference. I suppose you could loop through an array with slightly more readability than looping over individual members.
As to why 4 instead of 3, one possible reason is padding. More likely, it''s for homogenous coordinates.
In the case of a particle system, where you''re talking about lots and lots of particles, one example concern is data cache coherency. Let''s say, for whatever reason, you decide that it''s important to modify all the X coords first, then Y coords, then Z. Then neither of your structures will be good for data cache: rather, you''d want three separate arrays (one each for X, Y and Z).
Sometimes the best way is to try both ways, time them in real-use circumstances, and pick the better.
On your second question as to why an array rather than x,y,z. My guess quite often is personal preference. I suppose you could loop through an array with slightly more readability than looping over individual members.
As to why 4 instead of 3, one possible reason is padding. More likely, it''s for homogenous coordinates.
Accessing fields directly produces _no_ overhead. You get overhead when you make a function call or dereference a pointer, or the like. But accessing object.vector.x is no faster or slower than accessing object.x
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement