944,192 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5822
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 27th, 2007
0

code optimization ...

Expand Post »
Hi,
I have to port a code onto an ARM processor... what is the most commonly used methods for optimizing C code?
I`ve read abt limiting the number of parameters in a function, pass values by refernce in argunments rather than by value, and trying to avoid global variables...

My question is .. other than these commonly used techniques, are there any other techniques?

Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Apr 27th, 2007
0

Re: code optimization ...

Depends on what you're optimizing for. In general, it's fine to just use efficient algorithms and let the compiler do the nitty-gritty. It doesn't really matter how well you optimize something if you optimize the wrong part.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 27th, 2007
0

Re: code optimization ...

Of course.. let the machine do your job.
Compilers provide options to generate optimized for a given hardware (it optimizes using the full instruction set of that hardware). I have no idea abt the ARM processor you mentioned but check your compiler's manual if it provides any option for it.
Of course almost every compiler support the generic optimization options. So those you can anyway use (these are platform independent).
Finally just 2 words of caution:
1. When you use optimization options you loose debug information. Suggest you also test your application in optimized form (if you are doing it in debug only so far).
2. Regarding the "commonly used techniques", I suggest you first find out what has to be optimized using some tools like Quantify, there is no point in spending time/effort to optimize a function that's called only 10 times in an hour. Sometimes removing a temporary variable in a small function helps performance because it's called 10000 times. So if you're using the "commonly used techniques" use it where it's needed.
Other things you can do:
- data caching
- using unsigned vars,
- use inline functions (new compilers do this implicitly though)
- use registers.
- Finally I hope you've had a look at what everyone else has said in this thread.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Apr 27th, 2007
0

Re: code optimization ...

besides, you should consider:

-- using the right type of variable, meaning, if yyou need to use decimal values in your program, if they're not too big, consider using float rather than double...

-- (where you can) using pointers instead of arrays, since it uses less memory

-- being specific and simple in your processes, meaning you develop in 10 lines of programming what, with another process working just the same, you could use 25.

-- cleaning useless information off the buffer

-- using struct arrays instead of single variable arrays where you can

and such things that may seem simple, but can optimize your memory usage
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Apr 27th, 2007
0

Re: code optimization ...

Click to Expand / Collapse  Quote originally posted by thekashyap ...
Other things you can do:
- data caching
- using unsigned vars,
- use inline functions (new compilers do this implicitly though)
- use registers.
- Finally I hope you've had a look at what everyone else has said in this thread.
I've never heard of using unsigned being "optimized." How's that one work? The inline and register keywords are (anymore) just hints to the compiler, it'll usually do what it thinks is best unless you give it specific flags.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 27th, 2007
0

Re: code optimization ...

I`ve seen this data caching in a number of places... wat exactly does that refer to..

does it mean creating and using lookup tables instead of making the compiler calculate n generate values??
Last edited by caltiger; Apr 27th, 2007 at 2:34 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Apr 27th, 2007
0

Re: code optimization ...

Click to Expand / Collapse  Quote originally posted by Infarction ...
I've never heard of using unsigned being "optimized." How's that one work? The inline and register keywords are (anymore) just hints to the compiler, it'll usually do what it thinks is best unless you give it specific flags.
exactly.. from wat i`ve heard unsigned takes more cycles than signed ...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
caltiger is offline Offline
21 posts
since Sep 2006
Apr 27th, 2007
0

Re: code optimization ...

it's not that unsigned takes more cycles that signed 8correct me if i'm wrong), but the thing is that the space unsigned variables do not use in negative numbers is used in positive numbers... i.e.

  1. int n
goes from -32767 to 32767, so, logically
  1. unsigned int n
goes from 0 to 65524... right?
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Apr 27th, 2007
3

Re: code optimization ...

Unsigned is no more optimized than signed; they're the exact same instructions for addition, multiplication, and division.

If you really want to optimize numerical code, use Fortran.

And before you optimize your code, ask if you really need to optimize anything.

And what are you optimizing for? Memory usage? Speed?
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,480 posts
since Jun 2005
Apr 27th, 2007
0

Re: code optimization ...

Click to Expand / Collapse  Quote originally posted by caltiger ...
exactly.. from wat i`ve heard unsigned takes more cycles than signed ...
its not that it takes more cycles (correct me if i'm wrong)... its just that an unsigned variable uses the memory that is not used innegative numbers in positive numbers... i.e.:

  1. int n
goes from [-32767,32767], and
  1. unsigned int n
goes from [0,65534]... right?
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Hey, I got a very simple program here. Yet the compiler is disagreeing with me.
Next Thread in C++ Forum Timeline: String class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC