ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm having some weirdness with the speed of this call - it takes about 5 seconds to execute using the old method and about 15 seconds using the new method. Now, I expect there to be a little bit of a slow down with the new method, but this seems odd, maybe I'm missing something.
it's a direct call, so it should be pretty quick. My test case (where the timing came from) makes 612284 calls to this.
Here's the new (15 second) method:
Code:
worker->getSurfacePositionComponents(indexX,indexY,Wz,Wx,Wy);
// which calls this function:
void
blah::getSurfacePositionComponents(double indexX, double indexY,
float& Wz, float& Wx, float& Wy)
{
if (liquidArray[currentIndex]->getSurface())
liquidArray[currentIndex]->getSurface()->GetPositionComponents(indexX, indexY, Wz, Wx, Wy);
}
/// FYI: getSurface should be really quick:
someClass* getSurface() { return surface; }
Does it seem like it should be *that* much slower? Both cases make 612284 calls (to the first line). Do you have any general suggestions to make it quicker? (I understand you can't say much without seeing the rest of the code). Maybe inlining will help (I thought the compiler would inline anything it could automagically)? Note that "GetPositionComponents" does the same thing in both cases, and gets executed the same number of times in both cases.
Well, in the second example you're making three function calls instead of one (first getSurface() in the 'if' statement, then getSurface() again inside the 'if' clause, then the call to GetPositionComponents(). Function calls require some overhead, so it doesn't surprise me that the second example is a lot slower.
You could cut some of it down by assigning a temporary variable the result of (liquidArray[currentIndex]->getSurface()) within the 'if' statement, then using that variable again inside the 'if' clause. Your best bet for tuning is probably to optimize GetPositionComponents() as much as possible, especially if it's being called thousands of times.
Thanks for the reply. "GetPositionComponents" is part of a library, so it can't be optimized (by me, anyway). I know that I'm making more calls in the second example, it just doesn't seem like it should be that much slower - being that all of the calls really don't do anything whereas GetPositionComponents does a good bit of math.
You may be able to speed up the function calls by declaring them 'inline', though I am not sure whether that works with accessing private data. Good compilers tend to inline stuff like that anyway, so it may not make a difference. Another thing that may help is to make 'surface' a public, rather than private, class data member - that way you don't have to do any function calls, but can just access it directly (or you could leave it private, and make the class which calls it a friend class). Maybe not the best programming practice, but it could gain you some efficiency.
If GetPositionComponents is in a library that is open-source, you could optimize it
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.