Jump to content
  • Advertisement
Sign in to follow this  
Drythe

speed question!

This topic is 4821 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

why is: int u[420000]; for (int i = 0; i < 420000; ++i) u = i; much faster than: POINT3D Moof[420000]; for (int i = 0; i < 420000; ++i) Moof.x = i; ------------------------------- typedef struct POINT3D { float x, y, z; } POINT3D; How can I speed up the assignment of the POINT3D struct members?

Share this post


Link to post
Share on other sites
Advertisement
Its faster because it has to access the member of the struct of course. Plus, one is a float one is an integer. [grin]

Share this post


Link to post
Share on other sites
I can think of a couple possible reasons. Perhaps the most likely is that it's accessing 3 times as much memory (or rather, that it's only able to cache 1/3 as much, which should give about the same performance penalty). Also, you're doing a conversion from an int to float every time, which could also contribute. Benchmarks would show which (if either) of these is the main bottleneck.

Share this post


Link to post
Share on other sites
ok.. it seems to have been the addition memory access. I deleted the y and z floats and it has the same speed now.

Is there any speed loss in accessing a variable through a struct like that?

Share this post


Link to post
Share on other sites
Quote:

ok.. it seems to have been the addition memory access. I deleted the y and z floats and it has the same speed now.

Is there any speed loss in accessing a variable through a struct like that?


Wait a sec., I'm confused, what's the purpose of having a struct with only one variable in it? I'm assuming I'm misunderstanding you, otherwise, I would suggest using a 'typedef' if all you're looking to do is create some *new* float type. Also, you will lose minimal speed by storing multiple variables inside a STRUCT due to the fact that there is an access time the app. will have to do when utilizing those variables (like GDKnight mentioned before).

Share this post


Link to post
Share on other sites
i was just testing the suggestion that the amount of memory access was the cause of the slowdown.

if theres a time penalty of accessing struct members... im kind of confused now how I should go about storing data that needs to be accessed and changed so much.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drythe
Is there any speed loss in accessing a variable through a struct like that?


If it's the first member in struct, not in this world(unless you have 0 optimizations). It's the same asm code whatsoever.
Even if it's some other member, it's likely that it will be translated to a "lea" instruction, which is ultra-fast, and also, it's common, that accessing a variable from an array is done using "lea". So either cases lead to the same instruction anyway, because "lea" can get(implicite) 4 arguments:
- base pointer <p>
- sizeof(variable) <s>
- number of a variable <n>
- offset of a member within variable <o>
and give: p+s*n+o.

Now, if it's not the float->int conversion(I'm surprised it's so cheap), then, as @Catafriggm mentioned, it must be cache pollution. You're writing 420000 dwords after all, so it's rewriting the whole chache a few times.

Quote:
Original post by burst
I'm confused, what's the purpose of having a struct with only one variable in it?

I'm using it from time to time. It allows me to write wrappers. With methods. With constructors. With default values.

Cheers.
/def

Share this post


Link to post
Share on other sites
Your code:

-------------------------------
POINT3D Moof[420000];
for (int i = 0; i < 420000; ++i)
Moof.x = i;
-------------------------------

is equivalent to:

-------------------------------
float x[420000], y[420000], z[420000];

for (int i = 0; i < 420000; ++i)
{
x = (float) i;
y = (float) i;
z = (float) i;
}
-------------------------------

As you can see, your first code uses one variable while the second code uses three variables. Plus, you are converting the int i to a float value.

>if theres a time penalty of accessing struct members... im kind of confused now
>how I should go about storing data that needs to be accessed and changed so
>much.

Struct is fine. The compiler will take care of "expanding" your code so accessing struct members and single variables both operate at the same speed.

I suggest you don't spend much time with deciding if this is fast or not. The slowdowns in code are usually the algorithms. Structs were designed to make it easier for the programmer to program by making it easier to read.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!