Why?
Because I wanted to have some fun. I know it's been a while since my last post (and I still have not completed my machine learning with common lisp), but I wanted to post something new here, I saw a random video on YouTube of something similar, got curious, and decided to do my own.
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.26 x86_64
# `:/: `.----//----.` :/- Host: Strix GL704GV_GL704GV 1.0
# .:. `---` `--.` .:` Kernel: 5.15.0-52-generic
# .:` `--` .:- `:. Uptime: 15 hours, 14 mins
# `/ `:. `.-::-.` -:` `/` Packages: 2892 (dpkg), 12 (snap)
# /. /. `:++++++++:` .: .: Shell: bash 5.1.16
# `/ .: `+++++++++++/ /` `+` Resolution: 1920x1080, 1920x1080
# /+` -- .++++++++++++` :. .+: DE: Plasma 5.26.2
# `/ .: `+++++++++++/ /` `+` WM: KWin
# /` /. `:++++++++:` .: .: Theme: Breeze Dark [Plasma], Breeze [GTK2/3]
# ./ `:. `.:::-.` -:` `/` Icons: Tela-dark [Plasma], Tela-dark [GTK2/3]
# .:` `--` .:- `:. Terminal: yakuake
# .:. `---` `--.` .:` CPU: Intel i7-8750H (12) @ 4.100GHz
# `:/: `.----//----.` :/- GPU: Intel CoffeeLake-H GT2 [UHD Graphics 630]
# .-:.` `` `-:-. GPU: NVIDIA GeForce RTX 2060 Mobile
# `---.`` `` `.---.` Memory: 5617MiB / 15849MiB
# `..---+/---..`
Also, I'll be using the following:
Language | Version |
---|---|
Python | 3.10.6 |
Java (openJDK) | 11.10.16 |
R | 4.2.1 |
Bash | 5.1.16(1)-release |
Julia | snap 1.8.2 |
Common Lisp | SBCL 2.1.11.debian |
C | gcc (Ubuntu 11.3.0-1ubuntu1~22.04) |
I know, Bash? Yes, Bash. Why? Yes, why.
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.6 Elapsed time (mm:ss:ms) 1:57.13
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 11.0.17 2022-10-18 Elapsed time (mm:ss:ms) 0:00.31
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:05.57
Bash
Code:
#!/bin/bash
n=0
while [ $n -lt 1000000000 ]
do
if [ $n -eq 0 ]
then
echo "First"
fi
if [ $n -eq 999999999 ]
then
echo "Last"
fi
n=$(( $n + 1 ))
done
Execution:
./tester bash counter.sh "Bash"
^C # I gave up...
DNF
Julia
Code:
n = 0
while n < 1000000000
if n == 0
println("First...")
end
if n == 999999999
println("...Last")
end
global n += 1
end
Execution:
./tester julia counter.jl "Julia"
Testing… Julia = julia version 1.8.2 Elapsed time (mm:ss:ms) 1:33.08
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.64
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.3.0-1ubuntu1~22.04) 11.3.0 Elapsed time (mm:ss:ms) 0:02.00
Results
And the winner is…
Pos. | Language | Time (mm:ss.mm) |
---|---|---|
1 | Java | 0:00.31 |
2 | Common Lisp (SBCL) | 0:00.64 |
3 | C | 0:02.00 |
4 | R | 1:05.57 |
5 | Julia (snap pkg) | 1:33.08 |
6 | Python | 1:57.13 |
7 | Bash | DNF |