Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mageziyx

Which is faster?

This topic is 5985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

will there be a difference in speed between these two way of coding a vector class? class V { float xyz[3]; // operations }; class V { float x,y,z; // operations };

Share this post


Link to post
Share on other sites
Advertisement
Depends on what you''re doing with them, but you can send the first one as a pointer to a graphics API, as opposed to as three separate values (i.e. glVertex3fv vs. glVertex3f). I''d wager that the separate floats would be faster in operators such as addition and multiplication, etc. In the end, the difference is marginal, and convenience is quickly becoming a luxury we can afford. You can always optimize later if it makes enough of a difference to bother you.

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

Share this post


Link to post
Share on other sites
i think

float x,y,z;

is faster since you dont have to use the indexing operator [] each time you want to access a data member.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ahh, but he wouldn''t necessarily have to use the indexing operator each time to access a value, he could simply use pointers to access them.

I venture to guess that there is no speed difference at all. The only value of using the seperate x,y,z values is clarity of code.

Share this post


Link to post
Share on other sites
Well it depends, first - how good your compiler is at optimizing. Suppose you have an array of 3 variables, that memory is static in the application and has a definate address. So if you have a reference to the third variable like this:

array[2] = 0xDEADBEEF;

Then array[2] would just be reduced to the address by the compiler, although the address could still be calculated by:

base+2*(sizeof(long))

In that situation, the speeds are the same. But if you use a variable to index the array as oppsed to accessing a variable directly then the speeds differ.

array[position] = 0;

Then the code must first calculate the address by using:

base+position*sizeof(long)

and there is no way around that! However, if you were indexing with a variable then to do it with three seperate variables you wouldn''t beable to use a loop to do it, or, you could use a loop like this:

for(long index = 0; index < 3; index++)
{
if(index == 0)
var1 = 0;
if(index == 1)
var2 = 0;
...
}

And that is very inefficient and in which case I would use an array.

So there are speed differances...but it all depends on how you wirte your code.

--Andrew

Share this post


Link to post
Share on other sites
Thanks everyone, guess i will stick to x,y,z since i find it easier to read that way. btw, i''m not sure why the x,y,z class can''t be sent to glVertex3fv, i thought the two classes have the same memory layout?

Share this post


Link to post
Share on other sites
They can both be sent to glVertex3v. In the first case you''d pass "v.xyz", and in the second case you''d pass "&v.x".

The assembler code for both would look the same with pretty much any compiler.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!