Way to see how many operations a function, block of code or a statement costs

bytecodehigh performanceoperating systemsoperatorsperformance

Let's say I have a phone that can process 1 million operations per second and a micro controller that can perform 1000.

Is there a way to tell how many operations a performed by a function or block of code is performing?

I have a function and I can write it a few different ways, an example doesn't matter because I'm talking about all code I might write going forward in the future. I'd like to know how many operations are occurring for a specific block. Is this possible?

If it matters there are cases where I can write code in a variety of ways and get the same outcome:

for (var i:int;i<number;i++) {
    // result
}

for (var property in object) {
    // result is same as previous 
}

for each (var value in object) {
    // result is same as previous
}

Again, the code I've written above doesn't matter because it's not about that code!

I want to know if there's a way to measure what each statement, block of code or function costs in terms of operations.

I'm hoping maybe there is a program that is part of the operating system or that comes with Intel or AMD CPU's that I can run when I run my program that, when I press a button, will tell me how many operations were just run on the CPU.

Duplicate question response:
My question is different than the profiling question. When a CPU, GPU or APU(?) says it does 1.4 teraflops and my application can run on that or a microcontroller that can perform 1000 flops I want to know if there is software exists that tells me the operations (not profile the time it takes to run) on a statement, or block of code. Specifically, because I can write the same code multiple ways so knowing this info is valuable to me.

Best Answer

You can kind-of put a bound on the number of CPU cycles a certain instruction takes by looking at the manual for the specific CPU you are interested in. E.g. Intel publishes instruction manuals for every single CPU they make. However, this obviously only works with instructions the CPU understands, i.e. (in the case of say a Core i7) AMD64 machine code. Agner Fog from the Technical University of Denmark maintains a set of resources on software optimization, including a table of instruction costs for popular AMD64 CPUs.

The CPU does not understand, say, C. Therefore, you have to use some program to either interpret the C code or translate the C code to AMD64 machine code. In that case, it depends much more on how the program implements this translation / interpretation than the CPU.

Or, to say in short: no, there is no way to tell just by looking at high-level code how many CPU cycles it is going to take. You have to at least know exactly how it is going to be translated to machine code (i.e. you have to know what compiler is going to be used, what version, and what exact command line switches) and the exact type, version, and revision of the CPU the code is going to run on.

However, even that will only tell you how many cycles each individual instruction takes. Thanks to the complex scheduling, superscalarity, pipelining, branch prediction and lots of other things, the CPU may take more or fewer cycles for the entire program than is the simple sum of the individual instruction cycles. So, you have to take the overall interleaving and interaction between the instructions into account.

Oh, and of course, as soon as your program accesses memory, the disk, or heaven forbid the network, all bets are off anyway.

I know you only intended the code snippets as examples, but I want to address them anyway: for the three code snippets you posted, I would assume that any optimizing compiler worth its salt would generate the same machine code for all three of them.

Actually counting cycles is only possible on very simple CPUs, such as small microcontrollers or older CPUs from the 60s and 70s. Modern general purpose high performance CPUs do so many optimizations behind the scenes and have so many heuristics that it is virtually impossible.

Related Topic