From ba1ef13a39f89a015a76cd349194decf0c633db0 Mon Sep 17 00:00:00 2001 From: miraculixx Date: Fri, 13 Dec 2024 00:37:37 +0100 Subject: [PATCH] update python code - code.py is native python - cythonized.py is compiled to c using cython, a python compiler --- compile.sh | 6 ++++-- fibonacci/py/code.py | 3 ++- loops/py/code.py | 30 +++++++++++++++++++++--------- loops/py/cythonized.py | 24 ++++++++++++++++++++++++ run.sh | 5 +++-- 5 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 loops/py/cythonized.py diff --git a/compile.sh b/compile.sh index a8ff9537..99139d49 100755 --- a/compile.sh +++ b/compile.sh @@ -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 @@ -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 \ No newline at end of file +#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 .. + diff --git a/fibonacci/py/code.py b/fibonacci/py/code.py index da0c19d9..2ecccb79 100644 --- a/fibonacci/py/code.py +++ b/fibonacci/py/code.py @@ -1,6 +1,7 @@ import sys +from functools import cache - +@cache def fibonacci(n): if n == 0: return 0 diff --git a/loops/py/code.py b/loops/py/code.py index 119c0060..f627ec23 100644 --- a/loops/py/code.py +++ b/loops/py/code.py @@ -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 + with Pool() as p: + return p.starmap(fn, iterable) -main() +if __name__ == '__main__': + main() diff --git a/loops/py/cythonized.py b/loops/py/cythonized.py new file mode 100644 index 00000000..b3ff0aab --- /dev/null +++ b/loops/py/cythonized.py @@ -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 + +if __name__ == '__main__': + # force import of cythonized module + from cythonized import main + main() \ No newline at end of file diff --git a/run.sh b/run.sh index 3731cfd4..e0d74bbb 100755 --- a/run.sh +++ b/run.sh @@ -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" @@ -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" @@ -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" \ No newline at end of file +#run "Java GraalVM" "./jvm.code"