PDA

View Full Version : How to reduce C program execution time



stamatiou
February 26th, 2012, 08:03 PM
Do you have any tips on how to reduce the execution time of a c program? For example using const vars when necessary...

gardnan
February 26th, 2012, 08:16 PM
Well, I would say that there are no general rules for increasing performance, but there are two pieces of advice that are pretty universal.

If you are concerned with performance, make sure you are compiling using optimization options. This the -OX flag, where X is 2 or 3. 2 is pretty standard for software projects, and 3 is a little unstable. Also there are nearly hundreds of different optimization options within gcc to look through. I would recommend selecting those that suit your needs.

The second piece of advice is try and use a profiler, like gprof, to find spots of your code that may need tuning. It makes no sense to fine tune a function that is only called once for a small time delay!

trent.josephsen
February 26th, 2012, 08:39 PM
Do you have any tips on how to reduce the execution time of a c program? For example using const vars when necessary...

You know the two rules of optimization, right?

Rule 1. Don't do it.
Rule 2. (for experts only) Don't do it yet.

If marking a variable const is your idea of reducing execution time, you're not ready for rule 2.

stamatiou
February 26th, 2012, 08:45 PM
You know the two rules of optimization, right?

Rule 1. Don't do it.
Rule 2. (for experts only) Don't do it yet.

If marking a variable const is your idea of reducing execution time, you're not ready for rule 2.
Maybe you are right :/ But then, what type of int should I use for a counter variable?

Bachstelze
February 26th, 2012, 08:51 PM
If you think the "type of int" you use for a counter variable makes any difference in the execution time of a program, you are not ready for rule 2.

Get a book on algorithms.

MG&TL
February 26th, 2012, 09:00 PM
Bachstelze will probably tell me off/correct me for this, as I'm not ready for rule 2 either, but I pass large objects as pointers, use the -O flag, and according to The C programming language, if you have a commonly-used variable, you should declare it as "register" to make accessing it faster.

schauerlich
February 26th, 2012, 09:09 PM
Bachstelze will probably tell me off/correct me for this, as I'm not ready for rule 2 either, but I pass large objects as pointers, use the -O flag, and according to The C programming language, if you have a commonly-used variable, you should declare it as "register" to make accessing it faster.

Those are all low-level optimizations. Chances are if you're having performance problems the best way to solve it is to change the way you solve the problem. Better algorithms trump better memory layout or parameter passing schemes 9 times out of 10.

Bachstelze
February 26th, 2012, 09:26 PM
Also, K&R is old. Anything in it that is not purely concerned with C (mostly, anything that concerns the hardware or OS) is obsolete. register nowadays is mostly a no-op, because compliers know when to keep a variable in a register. If you need to have that kind of hardware control, you write assembly, not C.

MG&TL
February 26th, 2012, 09:37 PM
Also, K&R is old. Anything in it that is not purely concerned with C (mostly, anything that concerns the hardware or OS) is obsolete. register nowadays is mostly a no-op, because compliers know when to keep a variable in a register. If you need to have that kind of hardware control, you write assembly, not C.

Huh. And I thought I bought the most recent edition for a reason. ;)

Okay, off to remove register from my for-loops.

Barrucadu
February 26th, 2012, 10:11 PM
If for some reason you are unable to switch to a better implementation, you can run a profiler over your program and see where it is spending its time.

gardnan
February 26th, 2012, 11:19 PM
Maybe you are right :/ But then, what type of int should I use for a counter variable?

Well, I know that choosing the type of int for a counter variable doesn't really change much in terms of performance, but you may see a fair amount of code using size_t rather than int to iterate through an array.

Also, I don't remember where exactly, but I do remember reading something about optimization regarding integer values on 64-bit machines, something about machine word alignment. However, I am sure this kind of thing really is only of interest of people implementing compiler optimizations or people doing hand optimizations. If anyone had information relating to that, I would be interested.