Making a Program Run Faster

closed account (9jNRX9L8)
Lately I've been quite interested in ways of making my programs run faster and I've found a few tips I'd like to share.

1
Before compiling your program and releasing the exe turn everything to one line.
For example:
1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
}

turns to:
1
2
#include <iostream>
using namespace std;int main(){cout << "Hello world!" << endl;}

Apprantly that makes large programs run much faster.

2
Remove all comments from your program (not much of a performance boost but enough to notice).

3
Avoid making tons of cpp files. Try to make one cpp file. (very big performance boost).

Please reply if anyone has some more tips?
Last edited on
I seriously doubt that any of those "tips" actually have a significant impact on performance.
(We might be able to have an interesting discussion around point 3, though.)

If you want a faster program, use a better algorithm (if you can).
And remember to turn on compiler optimizations.
Last edited on
I'm certain none of these "tips" affect run-time performance, but maybe it can reduce compile time.

What you're doing is sacrificing readability for speed (what makes it worse is that it's compile-time), and you should never do that.
Last edited on
I remember using something similar to the first two of those 'tips' years ago with an interpreted version of BASIC. But with a compiled language such as C++, they are pointless, from the perspective of run-time performance.
closed account (z05DSL3A)
How do you make a Program Run Faster?

Chase it with a pitchfork.

Well at least that is as good advice as the three points in the OP.
Smaller programs run faster too.

I find #defines to be particularly useful here.
Don't forget you can dramatically increase run-time performance by using capital letters in place of lower case letters. Capital letters have lower ASCII values and therefore are lighter and consequently faster.

For example, A corresponds to decimal 65, while a corresponds to decimal 97. This means that A is about 1.5 times lighter and therefore faster than a.

You should always avoid characters which appear near the end of the alphabet such as z, x, and y. If a variable must have a name longer than 1 character, then make the characters following the first character numbers ( optimally small numbers ).
Last edited on
closed account (N36fSL3A)
I also found that rewriting your code in assembly, then hand-optimizing it helps with run time as well.
Are you running your C++ code through an interpreter?

1) That shouldn't matter at all since it is compiled into machine code .

2) Compiler should ignore all comments when preprocessor runs.

3) This goes against everything you are taught by the top recommended programming books (including Bjarne's books from what I've seen).
closed account (3hM2Nwbp)
3) might be alluding to heavily templated code, which is generally much faster than its polymorphic equivalent.
@BHX Specter
For 2), some compilers (e.g. GCC) have an option for keeping the comments in your code. Of course, your point still stands.

@Fredbill
I'm sure you already know this, but often the compiler will do a better job of that then you ever will. The only time that is worth it is when you know something the compiler doesn't.

@htirwin
If only that were true...

---
Of course, the fastest programs are all written in Go, because it goes places quicker.
NT3 wrote:
Of course, the fastest programs are all written in Go, because it goes places quicker.

Am I the only one feeling the urge to post this: http://instantrimshot.com/
closed account (N36fSL3A)
NT3 wrote:
@Fredbill
I'm sure you already know this, but often the compiler will do a better job of that then you ever will. The only time that is worth it is when you know something the compiler doesn't
That entire post was sarcastic. It was supposed to give misinformation that wasn't supposed to be taken seriously.
closed account (9jNRX9L8)
I of course don't mean write your entire program in one line. Write it as you do then before compiling it make it one line.
closed account (9jNRX9L8)
NT3 wrote:
Of course, the fastest programs are all written in Go, because it goes places quicker.


You know there is acctually a programming language called Go?
Last edited on
Toggy, don't do that. It's simply impossible with anything larger than a 10 line homework assignment.

Toggy wrote:
You know there is acctually a programming language called Go?
I think the joke was based on that fact.
Last edited on
Toggy, most replies to this topic have been sarcastic, and from your most recent replies, it appears you do not understand the topic. So here is a serious reply for you:

Before compiling your program and releasing the exe turn everything to one line.


This will make zero difference. C++ is compiled, so all that text is parsed by the compiler and converted to binary code. Adding or removing whitespace does not result in any difference in the generated program... so making everything 1 line does absolutely nothing but waste your time.

In order to speed up your program, you will need to change what binary code is generated -- this means actual logic changes, not just changing how the code looks.

Remove all comments from your program (not much of a performance boost but enough to notice).


Again.. C++ is compiled, so all comments will be discarded by the compiler anyway, and will not exist in the binary. So manually removing comments is pointless and wastes your time.

Avoid making tons of cpp files. Try to make one cpp file. (very big performance boost).


This might have some impact, but not in the way you think. Individual C++ files will be compiled separately and then "linked" together to form the final executable. Moving all the code into one cpp file might change how the linker generates the executable, but not in any significant way. Certainly not enough to make a noticeable performance difference.




In general... C++ is fast. And you don't need to worry about "speeding it up" unless your program is noticeably slow. (What's the point in making your program run 1 ns faster when it already runs virtually instantaneously?)

When you do need to optimize your program... no quick fix like this is going to do it. Usually it involves big algorithm and logic changes. IE: ripping apart portions of your code and rewriting them to be faster.

Adding/removing whitespace or comments... shortening variable names... reducing the number of cpp files... etc... none of that matters. At best those will change compile time (note that having one big cpp file will actually make compile time significantly slower)... but the final program will be the same.




So yeah... long story short: don't worry about optimization. When you know enough C++ to understand how to do it right, you'll understand why it usually isn't necessary.
Theres always a reason to optimize to some extent at some point. Less cycles = less energy. ;)
Optimizations are worthwhile only when they significantly improve performance.

Optimizing to shave 10 cycles off a routine is likely not worth the effort, even if it saves a miliwatt.
Last edited on
...and when you are writing a routine that will be used by many people in a library, such as a sorting routine. Optimal design is significant there too...
Topic archived. No new replies allowed.