0

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.

3
  • Look at the disassembly output. And keep in mind, not all operations take the same amount of time. Commented Dec 22, 2016 at 21:28
  • Possible duplicate of How to see what parts of your code are run most often?
    – gnat
    Commented Dec 22, 2016 at 21:29
  • My code compiles down to ABC (ActionScript Byte Code) but I may be able to view the abstract syntax tree (note: there is no tag for AST on the site). Commented Dec 22, 2016 at 22:38

4 Answers 4

7

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.

2
  • Unfortunately, the compilers I work with doesn't have all the optimizations it could. I'm not sure why. The good news is that they are may be open source so it may be possible to see what it's writing it. IIRC only one part of the compiler is open. So I still want to see the operations count. Commented Dec 22, 2016 at 22:40
  • My question is partially out of curiousity. I have a getTimer() method I can use and run a million iterations on the three different code blocks but on single line statements I'd just like to see what the CPU has to deal with when I type different blocks of code. Commented Dec 22, 2016 at 22:43
4

Typically, we ignore the specifics of "how many ops" and such- why? Because it doesn't matter. Different operations likely take different amounts of time, and that time is implementation-specific.

Instead, we use Profilers- just about every language you do work in has them, and they'll tell you mostly how much time was spent in a function or piece of code. The timing is the important part, since that'll tell you where any hang-ups are in code (although you may need to manually compensate if it e.g. asks for user input).

0

I'm pretty sure you have an an objective you're not mentioning because you think it's obvious. But there are other ways to accomplish it.

I'm pretty sure you don't seek to maximize the number of cycles.
I bet you want to minimize them - i.e. find out which cycles you should get rid of. There are methods other than counting or profiling.

One is to just single-step the code. You will quickly see how time is being wasted.

Another is this method. (Some say it is just another profiling method, but no profiler tells you what it does.)

0

Much, much more important than the time taken for every statement is the time taken to complete the problem, and how it grows as the problem gets larger:

https://en.wikipedia.org/wiki/Big_O_notation

Your examples all have the same order: O(n); the differences between them are likely just a constant value. Depending on what's happening inside the loop (is it a search? summing the values?), you may be able to apply a more efficient algorithm to get better performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.