Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tom76

Good ways to optimise/optimize code?

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

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!