Why?
Because I wanted to have some more fun.
Yes, this is exclusively for fun, I have no intention of leading any one to believe that one programming language is better or worse than any other. I'm not following any particular scientific method, and I've not used that many languages. So, if you're curious and like to see how all this languages performed in my computer, read on. You can also download all of this to your own machine and try it yourself. You can also check results, details on the hardware used, and even add your own results.
Where?
I'm not a scientist but I don't want to play this game without any type of control. So, here's the setup that will be running the speed test (Ubuntu 22.04, BTW):
neofetch
# `..---+/---..` christian@chris-GL704GV-Neon
# `---.`` `` `.---.` ----------------------------
# .--.` `` `-:-. OS: KDE neon 5.27 x86_64
# `:/: `.----//----.` :/- Host: Strix GL704GV_GL704GV 1.0
# .:. `---` `--.` .:` Kernel: 5.19.0-32-generic
# .:` `--` .:- `:. Uptime: 13 hours, 36 mins
# `/ `:. `.-::-.` -:` `/` Packages: 3043 (dpkg), 16 (snap)
# /. /. `:++++++++:` .: .: Shell: bash 5.1.16
# `/ .: `+++++++++++/ /` `+` Resolution: 1920x1080, 1920x1080
# /+` -- .++++++++++++` :. .+: DE: Plasma 5.27.10
# `/ .: `+++++++++++/ /` `+` WM: KWin
# /` /. `:++++++++:` .: .: WM Theme: Layan
# ./ `:. `.:::-.` -:` `/` Theme: Breeze Dark [Plasma], Breeze [GTK2/3]
# .:` `--` .:- `:. Icons: kora [Plasma], kora [GTK2/3]
# .:. `---` `--.` .:` Terminal: konsole
# `:/: `.----//----.` :/- Terminal Font: Hack 12
# .-:.` `` `-:-. CPU: Intel i7-8750H (12) @ 4.100GHz
# `---.`` `` `.---.` GPU: Intel CoffeeLake-H GT2 [UHD Graphics 630]
# `..---+/---..` GPU: NVIDIA GeForce RTX 2060 Mobile
# Memory: 8729MiB / 15848MiB
Also, I'll be using the following:
Language | Version |
---|---|
Python | 3.10.12 |
Java (openJDK) | 17.0.9 |
R | 4.2.1 |
Common Lisp | SBCL 2.1.11.debian |
C | gcc (Ubuntu 11.4.0-1ubuntu1~22.04) |
Scala | 3.3.1 |
Rust | 1.73.0 |
Maybe in the future I'll add more to this list.
What?
So the idea is very simple. How fast can a program execute only a while loop, with a control variable from 0 to 1,000,000,000 (1 billion, or 1e9, or 1 times 10 to the power of 9).
It's only doing that calculation. Nothing else. No printing, no logging, no multi-threading, no networking, no passing or returning values. Also, the computer running the code is completely idle (except for the code running) during the time trial.
All files are executed directly from the command line, from a fish shell, and timed with the use of the command time
(the GNU time
, not Bash).
Now that's clear, 3, 2, 1, go! (no, go is not included).
Time trials
Python
Code:
n = 0
while (n < 1000000000):
if (n == 0):
print("First...")
if (n == 999999999):
print("...Last")
n += 1
Execution:
./tester python3 counter.py "Python"
Testing… Python = Python 3.10.12 Elapsed time (mm:ss:ms) 1:58.54
Java
Code:
public class counter{
public static void main(String args[]){
int n = 0;
while(n < 1000000000) {
if (n == 0) System.out.println("First...");
if (n == 999999999) System.out.println("...Last");
n++;
}
}
}
Execution:
javac counter.java
./tester java counter "Java"
Testing… Java = openjdk 17.0.9 2023-10-17 Elapsed time (mm:ss:ms) 0:00.23
R
Code:
n <- 0
while (n < 1000000000) {
if (n == 0) {
print("First...")
}
if (n == 999999999) {
print("...Last")
}
n <- n + 1
}
Execution:
./tester Rscript --vanilla counter.R "R"
Testing… R = Rscript (R) version 4.2.1 (2022-06-23) Elapsed time (mm:ss:ms) 1:04.62
Common Lisp
Code:
(dotimes (n 1000000000)
(if (= n 0) (format T "First...~%"))
(if (= n 999999999) (format T "...Last~%")))
Execution:
./tester sbcl --script counter.lisp "Common Lisp"
Testing… Lisp = SBCL 2.1.11.debian Elapsed time (mm:ss:ms) 0:00.54
C
Code:
#include <stdio.h>
int main() {
int n = 0;
while(n < 1000000000) {
if (n == 0) {
printf("First...\n");
}
if (n == 999999999) {
printf("...Last\n");
}
n++;
}
return 0;
}
Execution:
gcc -o counter counter.c
./tester ./counter "C"
Testing… C = gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 Elapsed time (mm:ss:ms) 0:02.07
Scala
Code:
object Counter extends App {
Range(0, 1000000000).foreach(x => {
if x == 0 then println("First...")
if x == 999999999 then println("...Last")
})
}
Execution:
scalac counter.scala
run_test scala counter "Scala"
Testing… Scala = Scala code runner version 3.3.1 – Copyright 2002-2023, LAMP/EPFL Elapsed time (mm:ss:ms) 0:00.48
Rust
Code:
fn main() {
for i in 0..=999999999 {
match i {
0 => println!("First..."),
999999999 => println!("...Last"),
_ => (),
}
}
}
Execution:
rustc counter.rs
run_test ./counter "Rust"
Testing… Rust = rustc 1.73.0 (cc66ad468 2023-10-03) (built from a source tarball) Elapsed time (mm:ss:ms) 0:05.54
Results
And the winner is…
Pos. | Language | Time (mm:ss.mm) |
---|---|---|
1 | Java | 0:00.23 |
2 | Scala | 0:00.48 |
3 | Common Lisp (SBCL) | 0:00.54 |
4 | C | 0:02.07 |
5 | Rust | 0:05.54 |
6 | R | 1:02.07 |
7 | Python | 1:58.54 |