Sign in to follow this  

Function calls

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

Hi! I'm working on my own 3D math libs. And I've fount a thing, that I cannot understand: If I call the D3DXVec3Normalize function (with valid parameters) x-times it takes for example 0.3 seconds. Here is the other function { }; Yes, yes it's empty. But the execution of this functions x-times takes about 0.4 second. BUT IT IS EMPTY! Does nothing. How can this be? Please HELP! I tried inline functions too.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Post some code (besides the empty function), also I wouldnt worry too much about profiling empty functions unless you plan on using them.

Share this post


Link to post
Share on other sites
The function was probably not inlined, unlike the vector-based one, because the compiler didn't feel like it. The resulting call overhead could have caused lower performance.

Are you using all optimization flags on your compiler? This would simply eliminate any calls to the function.

Even then, without a definite knowledge of x, of the testing code and generated listing, and of the repeatability of the observation, we can't tell for sure.

Share this post


Link to post
Share on other sites
The code:

DWORD T1,T2,TD;
for(int j=0;j<5;j++)
{
std::cout << "TEST #" << j << "\n";
T1 = GetTickCount();
for(DWORD i=0;i<10000000;i++)
{
D3DXVec3Normalize(&A,&A);
}
T2 = GetTickCount();
TD = T2 - T1;
std::cout << "D3DX time: " << TD << "\t" << Out.x << "\t" << Out.y << "\t" << Out.z << std::endl;
T1 = GetTickCount();
for(DWORD i=0;i<10000000;i++)
{
// Ax.Normailze(); <- My normalize function is Vector3D class,
/* This function is defined if the .h file. This is mutch slower than D3DX
void inline Normailze()
{
float d = sqrtf(Data[0]*Data[0]+Data[1]*Data[1]+Data[2]*Data[2]);

Data[0] /= d;
Data[1] /= d;
Data[2] /= d;
};
*/
Cd();// <- this is the empty function
}
T2 = GetTickCount();
TD = T2 - T1;
std::cout << "LMath time: " << TD << "\t" << Outx.x << "\t" << Outx.y << "\t" << Outx.z << std::endl<< "\n\n";
}

Oh, another Q: How can I use the Performance counter? I have some problem with LARGE_INTEGER type.

Share this post


Link to post
Share on other sites
Your benchmark is flawed.

Benchmarking rule #1: Do not do timing on debug builds! Use release build with reasonable optimisation settings.

Benchmarking rule #2: Compilers are smart. If they can leave out something because they notice that it wont have any effect on the program then they might leave that thing out.
That second loop with an empty function would probably take almost zero time at all. The compiler might even remove the loop. I would expect T2 to be the same as T1 in a release build.

Your Normalize function is quite inefficient. Division is very slow, much slower than even 2 multiplications. You can make it faster by calculating the inverse square root and multiplying the x, y, and z by that.
You should read this thread for a few interesting fast inverse methods etc.

Share this post


Link to post
Share on other sites
Function calling invokes an overhead even if the body is empty. At an absolute minimum, the return address has to be pushed, the EIP changed, the return address popped back and the EIP reset. Most compilers also perform some work, by default, within a function body - potentially creating a stack frame, checking for stack corruption, marking out a critical section (in certain multithreaded applications) and setting up an exception handler. An empty function compiled in debug mode can actually be pretty huge.

Regards
Admiral

Share this post


Link to post
Share on other sites

This topic is 4108 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this