performanceoperators

How do basic operations (arithmetic, element-selection, max & comparison) compare speedwise, in programming languages?


[I'm not sure whether this is the right SE community for this question. Apologies if not, please redirect me to the right one]

If we consider some basic operations/operators common to most programming languages:

how do they compare in terms of speed, in general?

I understand that a precise answer may depend on the specific programming language, but I'd be happy to hear about general considerations and examples for such a comparison. Cheers!


Solution

  • As Eugene Sh. pointed out, this question is extremely broad, so I can't provide a complete answer here, but I'll try to give you enough context to orient you in the right direction for further exploration.

    Low-level engineers measure the time of an operation using CPU clock cycles. Depending on the processor that you're using you can expect anywhere between one million and five billion clock cycles per second.

    In most compiled languages, such as C, +, -, *, /, ==, <, min, and max are all generally going to take one CPU clock cycle when applied to numeric arguments. Exponentiation is generally going to take a dozen clock cycles or so.

    Reading values from a complex data structure such as a vector or matrix will usually require fetching data from RAM which typically takes around the order of 200 clock cycles on a modern x86-64 processor.

    Notes about the above

    In most interpreted languages, such as Python, each arithmetic operation or data fetch can take hundreds or thousands of clock cycles. The exception to this are interpreted languages that compile parts of the interpreted code. For example, modern JavaScript interpreters try to compile interpreted JavaScript into machine binary, allowing the JavaScript code do most arithmetic in a single clock cycle.

    Edit: This post glosses over an extraordinary amount of nuance. This post is limited in scope to SISD integer arithmetic, there are other kinds of arithmetic that CPUs can do that I do not discuss at all in this post. Even within SISD integer arithmetic, I gloss over an enormous amount of detail. Commenters specifically point out the following: