Best way to measure performance

Started by
26 comments, last by DevFred 15 years, 3 months ago
Quote:Original post by AshleysBrain
For example, this would allow i++ in a for loop to not generate an unused temporary return if i is an iterator.

Maybe, but here is the important question: WHY write for loops with i++ in the first place? What is the benefit of i++ over ++i? I find the semantics of ++i a lot simpler to understand and explain to others.
Advertisement
Quote:Original post by AshleysBrain
Quote:Original post by swiftcoder
What if ++i and i++ are overloaded to do entirely different things? Despite the expectation that they represent pre and post-increment, the programmer is allowed to overload them to do whatever he wants: ++i could create a database, and i++ reformat your hard drive, for all the compiler knows.


You'd be crazy if you made those operators do different things!


Good point. A better example would be an iterator class that does a lot of extra leg-work behind the scenes. Even if the compiler still could make the same optimization, it might not take that chance. Because, for all it knows, ++i could create a database, and i++ reformat your hard drive.
Getting into ASM at the moment, and decompiled a few C sources. How would I find out how many cycles it takes for one command? And is this CPU specific? Or is there another way to measure the cycles of a program? And yes, it's mostly curiosity :-)

PS: I'm using the latest NASM assembler on WinXP
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
Quote:Original post by Decrius
Getting into ASM at the moment, and decompiled a few C sources. How would I find out how many cycles it takes for one command? And is this CPU specific? Or is there another way to measure the cycles of a program? And yes, it's mostly curiosity :-)
You need to download the manuals for your particular model of CPU (it will be different for each). However, as has been said before, cycle counts are entirely useless as a measure of application performance on a modern CPU.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thanks guys :)

Also, about the post and pre increment debate, this is what http://www.agner.org/optimize/optimizing_cpp.pdf (page 29) says:
Quote:Increment and decrement operators

The pre-increment operator ++i and the post-increment operator i++ are as fast as additions. When used simply to increment a variable, it makes no difference whether you use pre-increment or post-increment. The effect is simply identical. For example, for (i=0; i<n; i++) is the same as for (i=0; i<n; ++i). But when the result of the expression is used, then there may be a difference in efficiency. For example, x = array[i++] is more efficient than x = array[++i] because in the latter case, the calculation of the address of the array element has to wait for the new value of i which will delay the availability of x for approximately two clock cycles. Obviously, the initial value of i must be adjusted if you change pre-increment to post-increment.

There are also situations where pre-increment is more efficient than post-increment. For example, in the case a = ++b; the compiler will recognize that the values of a and b are the same after this statement so that it can use the same register for both, while the expression a = b++; will make the values of a and b different so that they cannot use the same register.

Everything that is said here about increment operators also applies to decrement operators.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
I guess this was all said already, but:

Quote:Original post by swiftcoder
Quote:Original post by DevFred
Furthermore, i++ can NEVER be faster than ++i. I ALWAYS write ++i.
That isn't entirely true - if i is an integral type, there is really no reason why the compiler cannot compile both expressions to the identical assembly. For aggregate types you are of course correct.


Er, I have to wonder how quickly you can spot the bug in the following code, then:

int foo[10];for (int i = 0; i <= 10; ++i) { foo = 42; }


:) Faster != at least as fast.

Modern C++ developers write ++i by instinct. The only reason not to do so is because i++ yields correct logic and ++i doesn't; and in these cases, you should almost certainly prefer something else entirely.
Quote:Original post by Zahlman
int foo[10];for (int i = 0; i <= 10; ++i) { foo = 42; }


:) Faster != at least as fast.


Correct me if I'm wrong, but doesn't the ++i and i++ have nothing to do with this code? I see a bug, but that ain't it.
Quote:Original post by Zahlman
I have to wonder how quickly you can spot the bug in the following code, then:
int foo[10];for (int i = 0; i <= 10; ++i) { foo = 42; }


The bug is you are using <= instead of <. What does this have to do with the issue? ++i or i++ doesn't make any difference here, since it's not part of a larger expression where the value is processed further.

This topic is closed to new replies.

Advertisement