I often have to write code that I would like to optimize for performance, and I often have several solutions to a particular problem.
Is there a simple way to determine the number of CPU cycles a particular statement/function would take? I’m not talking about complex code that access the file system, Windows APIs or the network, I’m talking about comparing half a dozen lines of C++ code to determine which code would be more efficient.
The classic example would be comparing ++i with i++. The former is faster, but without knowing that, how would I be able to determine this myself?
I’d rather not install costly performance tools (e.g. Intel’s tools), but find a simple way to get to the bottom. Is there a way to see the Assembler code that is generated by C++ code – without debugging?
Any other suggestions and/or approaches are of course welcome.
Using the Visual Studio prompt you can invoke
cl.exe(the VC++) compiler and produce assembly listings with the option/FA[c|s|u].Generates a file named
mycode.asm, containing the listings, looking something like:… and so forth.
Similarly if you put a breakpoint inside VS and open the disassembly, you will see the assembly listings (provided the circumstances are right, debug mode should probably be on.)
This is probably of interest as well: How many CPU cycles are needed for each assembly instruction?