LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2004, 08:14 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Why does this code go so slow?


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.


Here's the old (5 second) method:

Code:
 thing.crr->active->surf->GetPositionComponents(indexX,indexY,Wz,Wx,Wy);
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.

Last edited by BrianK; 02-13-2004 at 08:16 PM.
 
Old 02-13-2004, 10:04 PM   #2
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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.
 
Old 02-14-2004, 08:21 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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.

Thanks again
 
Old 02-14-2004, 11:24 PM   #4
wapcaplet
LQ Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
Suse 9.2 & SATA - Slow, Slow, Slow jess1975 SUSE / openSUSE 6 01-28-2007 12:17 PM
New Fedora Internet connection slow slow slow matrim Fedora 9 07-29-2005 01:39 PM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration