From cffe3a61fec4db865e98e36545de192e6d82164b Mon Sep 17 00:00:00 2001 From: Antoine Sinton Date: Sat, 7 Dec 2024 14:43:15 +0100 Subject: [PATCH] add cython --- clean.sh | 3 ++- compile.sh | 1 + loops/cython/code.pyx | 19 +++++++++++++++++++ run.sh | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 loops/cython/code.pyx diff --git a/clean.sh b/clean.sh index 0c5e795c..b975319e 100755 --- a/clean.sh +++ b/clean.sh @@ -30,4 +30,5 @@ rm v/code rm emojicode/code emojicode/code.o rm -f chez/code.so rm -rf clojure/classes clojure/.cpcache -rm cobol/main \ No newline at end of file +rm cobol/main +rm -f cython/code.c cython/code \ No newline at end of file diff --git a/compile.sh b/compile.sh index 6bc3f903..b9217eb3 100755 --- a/compile.sh +++ b/compile.sh @@ -35,3 +35,4 @@ emojicodec emojicode/code.emojic echo '(compile-program "chez/code.ss")' | chez --optimize-level 3 -q (cd clojure && mkdir -p classes && clojure -Sdeps '{:paths ["."]}' -M -e "(compile 'code)") cobc -I /opt/homebrew/include/ -O -O2 -O3 -Os -x -o cobol/main cobol/main.cbl +cd cython && pip install cython && cython code.pyx --embed && clang $(python-config --includes) $(python-config --libs) -lpython3.13 -O3 code.c -o code \ No newline at end of file diff --git a/loops/cython/code.pyx b/loops/cython/code.pyx new file mode 100644 index 00000000..12865c6a --- /dev/null +++ b/loops/cython/code.pyx @@ -0,0 +1,19 @@ +import sys +import random + + +def main(): + cdef int u, r, i, j + cdef int a[10000] + + 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 + + +main() diff --git a/run.sh b/run.sh index 5ed14d41..955e2d7b 100755 --- a/run.sh +++ b/run.sh @@ -60,3 +60,4 @@ run "MAWK" "mawk -f ./awk/code.awk 40" run "Clojure" "java -cp clojure/classes:$(clojure -Spath) code 40" run "Babashka" "bb -cp clojure -m code 40" run "COBOL" "./cobol/main" +run "Cython" "./cython/code"