Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cargo build --manifest-path rust/Cargo.toml --release
kotlinc -include-runtime kotlin/code.kt -d kotlin/code.jar
kotlinc-native kotlin/code.kt -o kotlin/code -opt
dart compile exe dart/code.dart -o dart/code --target-os=macos
cd inko && inko build --opt=aggressive code.inko -o code && cd ..
cd inko; inko build --opt=aggressive code.inko -o code; cd ..
nim c -d:danger --opt:speed -d:passC -x:off -a:off nim/code.nim
nim -d:release --threads:off --stackTrace:off --lineTrace:off --opt:speed -x:off -o:nim/code c nim/code.nim
sbcl --noinform --non-interactive --load "common-lisp/code.lisp" --build
Expand All @@ -36,4 +36,6 @@ lake build --dir lean4
#dotnet publish fsharp -o fsharp/code-aot /p:PublishAot=true /p:OptimizationPreference=Speed
# haxe --class-path haxe -main Code --jvm haxe/code.jar # was getting errors running `haxelib install hxjava`
#dotnet publish csharp -o csharp/code-aot /p:PublishAot=true /p:OptimizationPreference=Speed
#gnatmake -O3 -gnat2022 -gnatp -flto ada/code.adb -D ada -o ada/code
#gnatmake -O3 -gnat2022 -gnatp -flto ada/code.adb -D ada -o ada/code
cd py; pip install cython setuptools; CFLAGS=-O3 cythonize -3 -i cythonized.py; cd ..

3 changes: 2 additions & 1 deletion fibonacci/py/code.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from functools import cache


@cache
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a cache is "cheating" in this case. It allows the program to skip huge amounts of the work relative to other implementations. I mean, it's cool that python supports this, but a similar cache could be implemented in other languages giving them huge boosts also.

Copy link
Author

@miraculixx miraculixx Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, in this case all JIT and compiler optimizations should not be allowed either, because they are essentially "cheating" too, right? Otherwise the benchmark is comparing compilers, not languages.

def fibonacci(n):
if n == 0:
return 0
Expand Down
30 changes: 21 additions & 9 deletions loops/py/code.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
from functools import cache
import sys
import random

@cache
def compute(a, u, r):
# memoized function - don't recompute if the same values are passed
for j in range(100000): # 100k inner loop iterations, per outer loop iteration (for same values a, u, r)
a += j % u # Simple sum
return a + r # Add a random value to each element in array

def main():
u = int(sys.argv[1]) # Get an input number from the command line
r = random.randint(0, 10000) # Get a random number 0 <= r < 10k
a = [0] * 10000 # Array of 10k elements initialized to 0
for i in range(10000): # 10k outer loop iterations
for j in range(100000): # 100k inner loop iterations, per outer loop iteration
a[i] += j % u # Simple sum
a[i] += r # Add a random value to each element in array
print(a[r]) # Print out a single element from the array
u = int(sys.argv[1]) # Get an input number from the command line
r = random.randint(0, 10000) # Get a random number 0 <= r < 10k
a = [0] * 10000 # Create an array of 10k elements
# 10k outer loop iterations, in parallel and memoized
tasks = ((a[i], u, r) for i in range(10000))
a = list(parallel(compute, tasks))
print(a[r]) # Print out a single element from the arra

def parallel(fn, iterable):
# helper for parallel processing as a transparent fallback in case memoization is not effective
from multiprocessing import Pool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multi threading and multi processing are not allowed for this particular benchmark. Though, if you have an idea for a multi-process-specific benchmark, feel free to make a PR to add a new one!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thougt the objective is to benchmark 1 billion operations? I get that you want some ground rules, but why does it matter how that is achieved?

with Pool() as p:
return p.starmap(fn, iterable)

main()
if __name__ == '__main__':
main()
24 changes: 24 additions & 0 deletions loops/py/cythonized.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
import random
import cython

# cython.boundscheck(False) disables bounds checking for array access
# cython.cdivision(True) disables validity checking for division by zero
# cython.locals() declares the types of variables as native
@cython.boundscheck(False)
@cython.cdivision(True)
@cython.locals(u=cython.int, r=cython.int, a=cython.int[10000], j=cython.int, i=cython.int)
def main():
u = int(sys.argv[1]) # Get an input number from the command line
r = random.randint(0, 10000) # Get a random number 0 <= r < 10k
a = [0] * 10000 # Array of 10k elements initialized to 0
for i in range(10000): # 10k outer loop iterations
for j in range(100000): # 100k inner loop iterations, per outer loop iteration
a[i] += j % u # Simple sum
a[i] += r # Add a random value to each element in array
print(a[r]) # Print out a single element from the arra
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already merged a PR for cython?

Copy link
Author

@miraculixx miraculixx Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is PR #222, however that is treating Cython as a separate language (because it is using Cython-specific syntax). This PR however uses Cython's pure python mode, i.e. same syntax as original code.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I already merged a PR for cython?

Actually, PR #222 hasn't been merged yet :)


if __name__ == '__main__':
# force import of cythonized module
from cythonized import main
main()
5 changes: 3 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ run "Node" "node" "./js/code.js"
run "Bun" "bun" "./js/code.js"
run "Bun (Compiled)" "" "./js/bun"
run "Deno" "deno" "./js/code.js"
run "PyPy" "pypy" "./py/code.py"
run "CPP" "" "./cpp/code"
run "Go" "" "./go/code"
run "Node (jitless)" "node --jitless" "./js/code.js"
Expand All @@ -32,6 +31,8 @@ run "PHP JIT" "php -dopcache.enable_cli=1 -dopcache.jit=on -dopcache.jit_buffer_
run "PHP" "php" "./php/code.php"
run "R" "Rscript" "./r/code.R"
run "Python" "python3.13" "./py/code.py"
run "Python (cython)" "python3.313 ./py/cythonized.py 40"
run "Python (pypy)" "pypy" "./py/code.py"
run "Common Lisp" "" "common-lisp/code"
run "Inko" "" "./inko/code"
run "Nim" "" "./nim/code"
Expand Down Expand Up @@ -63,4 +64,4 @@ run "Babashka" "bb" "bb/code.clj"
#run "Haxe JVM" "java -jar haxe/code.jar" # was getting errors running `haxelib install hxjava`
#run "Ada" "./ada/code"
#run "D" "./d/code" # Seems to not have an arm / M1 version
#run "Java GraalVM" "./jvm.code"
#run "Java GraalVM" "./jvm.code"