Good ways to optimise/optimize code?

Started by
31 comments, last by tom76 22 years, 5 months ago
I know about making functions inline if they''re to be accessed frequently, but are there some good tips for speeding up code? Mine currently has a lot of if statements which I suspect slows it down. "I envy you, who has seen it all" "And I, young sir, envy you, who have yet to see it for the first time..." - Daniel Glenfield 1st October 2001
if (witisism == !witty) return to_hole
Advertisement
quote:Original post by tom76
I know about making functions inline if they''re to be accessed frequently, but are there some good tips for speeding up code?

Mine currently has a lot of if statements which I suspect slows it down.



Not really. If your if statements usually go the same way they''ll be branch predicted anyway.

General code optimizing tips: Always use an optimizing compiler. (MSVC Professional, not Standard or Student.)

Try to access data structures the way they''re layed out in memory. (Fill 2d arrays by row and not by column, etc)

Precalculate as much as possible outside of loops.

Run a profiler against your code to find out where your bottlenecks really are. (Studies show that programmers almost always optimize in the places that do the least good.)
Another thing that can help is assembly. It makes your code longer but it will speed it up (as long as you write your code well).
I don''t know assembly, just C and C++

"I envy you, who has seen it all"
"And I, young sir, envy you, who have yet to see it for the first time..."
- Daniel Glenfield
1st October 2001
if (witisism == !witty) return to_hole
I agree. Profile.

Create test data and take timings. Profile, optimise, re-time on same test data, now check your FPS (or whatever metric you use).

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie
I think the most obvious optimization is to use the right data structure for the right job.

If you have a bad algorithm or data structure your program will perform badly. You could put all the effort in the world to optimize it but you could''t not compete against a better algorithm (and it doesn''t necessarily mean more complicated).

So the morale is, keep it simple stupid (aka KISS) and straight to the point.

Patrick Lafleur

System Engineer
Creation Objet Inc.
Has anyone an opinion on AoS vs SoA? (array of structures and reverse)The AoS seems like the C++ way to do things, but the SoA can be considerably faster if the program needs to go through the whole data and perform operations only on some (or just one) of the fields in the structure because of the cache speed-up.

Edited by - Diodor on November 6, 2001 11:54:08 PM
Lots of tricks to do this, but one thing you will want to remember is the 80 - 20 rule. That comes up alot in life btw. Anyway, 80% of the time the user will run 20% of the code. So optimize that.

You can unroll loops, that takes some serious typing to get any significant increase in preformance though. You could also tell the compiler to keep certin variables in the CPU register. This helps some, but if you use an optimize compiler it might do this for you. I''m not sure.

Best thing to do has already been told though, profile.

Glandalf
Just a thought

quote:Original post by TheFez
Another thing that can help is assembly. It makes your code longer but it will speed it up (as long as you write your code well).


Unless you really know what you''re doing, assembly isn''t going to speed things up, but it might actually slow your program down. An optimizing compiler today does a much better job than a poor assemblyly programmer.

==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
MadKeithV wrote an article for gamedev about C++ optimisation technics. Check it in the "Articles" section.

This topic is closed to new replies.

Advertisement