C optimizations

I have written a lot of C code, in fact C was probably the first ‘real’ computer language I learned but it opened the doors to a lot of other languages because it’s syntax is what I would call a ‘root’ syntax (which was copied by a lot of other languages.)

I wanted to focus on optimizations in C. There are some good posts out there on C optimization already which are well worth reading and digesting. But I wanted to add some more information to the pot based on my own experiences.

First off you need to understand the processor you are programming to. My experience is with AMD and Intel, but there are lots of processors out there and they all have difference performance characteristics. So it is usually a very good idea to read any documentation put out by the chip designers, for example AMD publishes a useful guide called “Software Optimization Guide for the AMD64 Processors”.

Second, different compilers optimize in different ways. GCC does a good job, but Intel’s compilers do an even better job on Intel processors (duh!).

Third, you need to check which compiler settings to use to get the best out of your application. Defaults are rarely the best settings and you will need to run benchmarks to see what works best. I have found that setting the right machine architecture flag on GCC can yield 10%-15% improvements (setting ‘-march=opteron’ on opteron for example,) the flip side is that your binaries are less transportable.

Fourth, writing simple code is usually the best thing. By that I mean that complex or convoluted code could make it more difficult for the compiler to optimize. This post has some good information about that, as does this one, but there is a lot of overlap between the two.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: