speed question!

Started by
12 comments, last by s_cloudx 18 years, 10 months ago
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?
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]
- GDKnight
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.
buh?
ok, ill try both of those things
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?
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).
"One must learn to follow before one can lead."
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.
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
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.
--------------------------Sony "Mr. Valdez" ValdezPinoyforum.net Technical EditorPinoyforum.net - the breeding ground of the Filipino Computer Talents

This topic is closed to new replies.

Advertisement