简体   繁体   中英

How to determine complexity (as in CPU cycles) of C++ code with Visual Studio 2008

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 (eg 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.

Your "classic example" of ++i vs i++ is usually irrelevant. Optimizing compilers are good enough to prevent that from being an issue. In fact they're really good at making code that looks slow fast.

Look at algorithmic complexity: often if code is unexpectedly slow, there's a hidden O(n) in an inner loop somewhere.

It's been said before, profile, profile, profile. Counting cycles is much less relevant now, because of the importance of the cache. Microbenchmarks are sometimes ok for small chunks of code, but often aren't representative for their performance in an application.

Visual Studio has a built in profiler, described here: http://msdn.microsoft.com/en-us/magazine/cc337887.aspx which is really what you need.

Don't choose the code that's more efficient. Choose the code that's more readable .

I know you said you do not want to pay for performance tools, but I highly suggests you to take a look at AQTime .

I know it can be expensive, but it is worth every penny invested. It is capable of doing a very good analyze of your code, such as allocation, performance and many, many others.

I can not imagine myself working without this tool, really. And I do not work for Smartbear . I am just a big fan.

What I think is: why should anyone bother reading and debugging Assembly when we have great tools to do that? Your time can be more productive if you have the right tools and focus on business.

Just my 2 cents.

Using the Visual Studio prompt you can invoke cl.exe (the VC++) compiler and produce assembly listings with the option /FA[c|s|u] .

cl.exe /FA mycode.c

Generates a file named mycode.asm , containing the listings, looking something like:

; Line 16
    push    ebp
    mov ebp, esp
; Line 17
    cmp DWORD PTR _argc$[ebp], 2
    jl  SHORT $LN2@main
    cmp DWORD PTR _argc$[ebp], 2
    jle SHORT $LN3@main
$LN2@main:
; Line 19
    push    OFFSET $SG2660
    call    _puts
    add esp, 4

... 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM