Slowdown!

Started by
21 comments, last by Drythe 18 years, 10 months ago
I'm having some trouble with slowdown and tried to pinpoint what's going wrong by testing it with the following code. Here are 2 different versions of a loop statement that do the same thing. With the first one, I'm getting about 130,000 program cycles/sec and the second one, 18:P As you can see, the only difference is in one I'm writing the math out manually, and the second I'm using a function that does the same thing. Good: POINT3D LightPos = POINT3D(34, 546, 56); float u; for (int i = 0; i < 13976; i++) { for (int k = 0; k < 3; k++) { //this is pasted 16 times in the real code u = pow((LightPos.x * LightPos.x) + (LightPos.y * LightPos.y) +(LightPos.z * LightPos.z), 0.5); } } Bad: POINT3D LightPos = POINT3D(34, 546, 56); float u; for (int i = 0; i < 13976; i++) { for (int k = 0; k < 3; k++) { //this is pasted 16 times in the real code u = GetLength( LightPos); } } Here are the definitions used: //////////////////////////////////////////////////////////////////////////// inline const float GetLength(const POINT3D& Vector) { return pow((Vector.x * Vector.x) + (Vector.y * Vector.y) + (Vector.z * Vector.z), 0.5); } //////////////////////////////////////////////////////////////////////////// typedef struct POINT3D { public: POINT3D() {}; POINT3D(float xf, float yf, float zf) {x = xf; y = yf; z = zf;} float x, y, z; }POINT3D; *A couple notes: the inline seems to be working I guess.. because when I remove it, the 18 program cycles/sec goes down to 9. Also, when I move the 'float u;' into the header file, the 18 goes to 32. I don't know if this is a hint of any kind. Also, this data isn't being used anywhere in the program. in fact, this is about all the program is, since I commented all of the main stuff out. Everything's still being compiled though... P.S. sorry about the formatting... how do you do those little code boxes on this forum?
Advertisement
Why do you have two fors() if you don't use the value for k or i?
well, I do use the double fors in the final code, but I commented just about everything out to test the slowdown problem.
GetLength( LightPos);
Does it has the exact same code as that line where you do the calculations?
yes

////////////////////////////////////////////////////////////////////////////
inline const float GetLength(const POINT3D& Vector)
{

return pow((Vector.x * Vector.x) +
(Vector.y * Vector.y) +
(Vector.z * Vector.z), 0.5);

}
I mean.. it does the same thing, but there must be a key difference on the compiler level or something. Possibly something to do with memory? i dunno:)
I have no idea...
What compiler are you using?
VC++6
Can you take a look at the assembly code the compiler generates in both cases?
You can post it here.
My 2 cents: This conundrum is whack.
I think the compiler output is necessary to help solve this one

This topic is closed to new replies.

Advertisement