Is C++ compiled or interpreted?
C++ is compiled ahead of time to machine code by a compiler such as GCC, Clang or MSVC. It does not need a virtual machine, and its speed depends on the optimization level you build with.
How it runs your code
A C++ compiler turns your source into machine code before the program runs, and a linker joins the pieces into an executable. The CPU runs that executable directly. Interpreters for C++ exist, such as Cling, but almost nobody uses one to run production programs.
Does that make C++ slow?
The optimizer works before the program starts, and you choose how hard it works with a flag: -O0 for none, then -O1, -O2 and -O3. In our test, C++ built with -O0 was slower than CPython, and -O1 made the class code about 12 times faster.
What we measured
One small program, an order with items, a coupon, a discount and tax, written as an average programmer would write it. Times are nanoseconds per call.
| Setting | Class code | Plain functions |
|---|---|---|
| -O0 (no optimization) | 2,027 | 562 |
| -O1 | 162 | 37 |
| -O2 | 106 | 25 |
| -O3 | 120 | 19 |
g++ 15.2.0 with Google Benchmark, five repetitions. The class code is slower at -O3 than at -O2, and the plain functions are faster.
Questions
Is C++ interpreted?
No. C++ is compiled ahead of time to machine code. Interpreters such as Cling exist, but production C++ does not use them.
Why is C++ fast?
Its compiler optimizes before the program runs, and the program runs directly on the CPU. Built with no optimization, our C++ program was slower than CPython.
Does C++ need a virtual machine?
No. A C++ executable runs directly on the processor with no virtual machine.
Related
Is Rust compiled? · Is Go compiled? · Is Swift compiled? · Compiled vs interpreted · Why optimization decides speed