Archived

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

Endemoniada

Are Structs Faster ?

Recommended Posts

Endemoniada    430
Hi guys, In general, is it better to use structures or the individual elements when it comes to speed. I mostly mean small structures. A perfect example is this: A lot of times I store the X and Y value of a pixel location, so is it better (faster) to go: struct Pixel { int x; int y; }; or just store the X and Y as ints ? Also, when I have to calculate the X and Y value in a function will it be better to declare a struct or just the ints like so: void MyFunction() { // like this ? struct Pixel; Pixel.x=... // calculation Pixel.y=... // calculation or this: ? int x=... int y=... } And what about inside another structure like so: struct MyStruct { // like this: ? Pixel pixel; // or this: ? int pixel_x; int pixel_y; }; I''m in the optimization stage of my game and want to do everything I can to keep it efficient. Thanks.

Share this post


Link to post
Share on other sites
antareus    576
I don''t think there''s much of a difference. Even if there is, you''re talking about nano optimization, which isn''t a productive use of time.

Use a profiler when optimizing, and then concentrate on where it says the majority of the time is spent.

Share this post


Link to post
Share on other sites
Sneftel    1788
It might be slightly faster. But the real benefit of structs is their ability to associate and aggregate information, not their efficiency. None of these things will make much of a difference one way or another. Remember, micro-optimizations like this are the LAST, and most optional step in the optimization process. The first thing you should do is improve on your algorithms.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Cedric    158
quote:
Original post by Endemoniada
In general, is it better to use structures or the individual elements when it comes to speed. I mostly mean small structures. A perfect example is this:

The structure version might be slightly faster if you''re moving it around, and returning it. And besides, it''s a better programming practice to create a structure to regroup related variables.
quote:
And what about inside another structure like so:

Same comments.
quote:
I''m in the optimization stage of my game and want to do everything I can to keep it efficient.

Do you know the Three Rules of Optimization? Profile your code! Structs vs variables won''t make the difference in the end!

Cédric

Share this post


Link to post
Share on other sites