Is Ruby compiled or interpreted?
Ruby is compiled to bytecode, then run by a virtual machine. Ruby 1.8 was a real interpreter that walked the syntax tree. Ruby 1.9 replaced it with YARV, which compiles your source to bytecode first.
How it runs your code
Ruby 3.3 runs the bytecode on the YARV virtual machine. Ruby also has a JIT called YJIT, which you switch on. The Ruby in our test image comes from a Debian build that leaves YJIT out, so we have no JIT numbers for Ruby.
Does that make Ruby slow?
We cannot show what the JIT does for Ruby. Our numbers show Ruby without a JIT: slower than CPython and slower than PHP, on both the class code and the plain functions.
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 |
|---|---|---|
| Ruby 3.3.8, no JIT | 2,260 | 1,150 |
Measured with benchmark-ips. The class-code runs gave 2.26 and 2.36 microseconds per call, and the plain-function runs gave 1.15 and 1.16.
Questions
Is Ruby interpreted?
Not in the old sense. Ruby 1.8 walked a syntax tree. Since Ruby 1.9, YARV compiles source to bytecode and a virtual machine runs it.
Does Ruby have a JIT?
Yes. YJIT is an optional JIT in the standard implementation. JRuby and TruffleRuby have JITs too.
How fast is Ruby without a JIT?
On our program, Ruby 3.3.8 took 2,260 ns for the class code and 1,150 ns for plain functions.
Related
Is Python compiled? · Is PHP compiled? · Is Lua compiled? · Compiled vs interpreted · Why optimization decides speed