Suppose that I want to know the number of the cycles of cpu clock that uses to run a specific code written with a specific programming language, like Python or others.

Can I do it?

I mean, do you have any access to count the CPU's clock cycles for your code technically?

Does this code run on any system (any minicomputer or microprocessor) with the same number of cycles of CPU clock when you're using the exactly same code, programming language and library?

Actually, if the answer to the last question is yes, if I have the number of cycles, I can calculate the code-runtime for every systems according to the clock frequency of the CPU. Then, I can find the suitable system that run the task fast enough and choose it for my project.

If you have the answer, I'll appreciate if you can provide references.

  • 2
    $\begingroup$ The number of cpu clocks depends on the compiled code, the other currently running programs (such as the OS and other user programs), the cpu itself, the RAM, and many other things... Clearly, it will not run the same amount of clock cycles on different computers - let alone it can vary even on a single computer! $\endgroup$
    – nir shahar
    Dec 7, 2021 at 9:05
  • $\begingroup$ do you have any access to count the cpu's clock cycles for your code technically? Depends. $\endgroup$
    – greybeard
    Dec 7, 2021 at 9:16

1 Answer 1


Assume, I want to know the number of the cycles of cpu clock that uses to run a specific code written with a specific Programming language like python or others. Can I do it?

Short answer: Not currently.

Longer answer: It is at least conceptually possible for a restricted subset of programs -- e.g., restricted to loop-free non-interactive code -- and it may be possible for more programs in the future.

Long answer: This is a very difficult task, and an open research question. To my knowledge, it not currently possible. There are three main barriers to accomplishing this:

  1. Predicting the compiler. The compiled code (say, in X86 assembly) will look considerably different than the piece of code itself. Predicting this would essentially require re-implementing the compiler ourselves. One way to get around that we could just pre-compile the piece of code individually and link it to other parts of the program.

  2. Predicting loops. The second challenge is more of a fundamental impossibility: whether looking at the source program or the compiled code, if the piece of code has loops (i.e. in assembly, Jump instructions), then it is very difficult to infer how many times the loops will run. It is undecidable in general, though in specific cases we might hope to use static analysis to determine the exact number of loop iterations based on context.

  3. Predicting the architecture. Finally, there is a great deal of difference between the assembly code and the number of cycles it will take to actually run on the hardware, for a number of reasons which are only getting more complex, including for example basic architectural features like caching and branch prediction, as well as more complicated modern hardware optimizations. As you point out, this also means that the number of cycles will depend on the target architecture and target hardware.

Despite these barriers, I think this is a feasible goal and something that we could conceivably achieve in the future (and I know at least some researchers working on it). For a starters, we could look at code that is:

  • Already compiled to assembly (avoiding barrier 1);

  • Loop-free or consisting only of simple loops, e.g. for loops where the number of iterations is a known variable $n$ (avoiding barrier 2); and

  • Does not contain any interactive operations difficult to predict at the hardware level, specifically memory accesses, system calls, and I/O.

With these three restrictions, which are admittedly rather severe, it would be conceptually completely possible to predict the exact number of clock cycles of a program.


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.