From a81950853e0d27f6f7b5dd3322002d2186fe45f7 Mon Sep 17 00:00:00 2001 From: PanSi21 Date: Sun, 5 Jan 2025 19:44:43 +0100 Subject: [PATCH] test.sh --- README.md | 28 +++++++++++++++++++++++++++ index.js | 5 ++++- only.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 only.js create mode 100644 test.sh diff --git a/README.md b/README.md index b0603b7..dd85c9c 100644 --- a/README.md +++ b/README.md @@ -70,3 +70,31 @@ C Execution Time: 2267.1350589999997ms TinyCC Execution Time: 10732.041204000001ms JavaScript Execution Time: 19499.030189999998ms ``` + + +## test.sh con n = 300 + +```sh +pansi21@legolas-pX:~/Scrivania/BUN-test$ bash test.sh +Bun Execution Results: +Mean Time (ms): 241.13 +Standard Deviation: 21.91 + +Node Execution Results: +Mean Time (ms): 274.54 +Standard Deviation: 12.86 + +``` + + +## test.sh con n = 500 +```sh +pansi21@legolas-pX:~/Scrivania/BUN-test$ bash test.sh +Bun Execution Results: +Mean Time (ms): 1030.54 +Standard Deviation: 36.02 + +Node Execution Results: +Mean Time (ms): 797.34 +Standard Deviation: 30.81 +``` \ No newline at end of file diff --git a/index.js b/index.js index c7d3959..332877e 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,12 @@ + +const setN = 1000; + import { cc, dlopen, FFIType } from "bun:ffi"; + // START TINYCC import source from "./matrixcc.c" with { type: "file"}; -const setN = 1000; const { diff --git a/only.js b/only.js new file mode 100644 index 0000000..2e3a89a --- /dev/null +++ b/only.js @@ -0,0 +1,54 @@ +const setN = 500; // Imposta la dimensione della matrice +// test matrix JS +function matrixjs( n = setN) { + + let a = []; + let b = []; + let c = []; + + + for (let i = 0; i < n; i++) { + a[i] = []; + b[i] = []; + c[i] = []; + + for (let j = 0; j < n; j++) { + a[i][j] = Math.floor(Math.random() * 100); + b[i][j] = Math.floor(Math.random() * 100); + c[i][j] = 0; + } + + } + + for (let i = 0; i < n; i++) { + for (let j = 0; j < n; j++) { + for (let k = 0; k < n; k++) { + c[i][j] += a[i][k] * b[k][j]; + } + } + } + + for (let i = 0; i < n; i++) { + a[i].length = 0; + b[i].length = 0; + c[i].length = 0; + } + + a.length = 0; + b.length = 0; + c.length = 0; + +} +// Funzione per misurare il tempo di esecuzione +function jsTest() { + const start = performance.now(); + + // Esegui la funzione matrixjs + matrixjs(setN); + + const end = performance.now(); + console.log(`JavaScript Execution Time: ${end - start}ms`); + } + + // Avvia il benchmark + jsTest(); \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..381d951 --- /dev/null +++ b/test.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Funzione per calcolare la media +calculate_mean() { + local sum=0 + for time in "$@"; do + sum=$(echo "$sum + $time" | bc) + done + echo "scale=2; $sum / $#" | bc +} + +# Funzione per calcolare la deviazione standard +calculate_std_dev() { + local mean=$1 + shift + local sum=0 + for time in "$@"; do + sum=$(echo "$sum + ($time - $mean)^2" | bc) + done + echo "scale=2; sqrt($sum / $#)" | bc +} + +# Esegui 100 test con Bun +bun_times=() +for i in {1..100}; do + start_time=$(date +%s%3N) # Ottieni il tempo in millisecondi + bun run only.js > /dev/null + end_time=$(date +%s%3N) + elapsed_time=$((end_time - start_time)) + bun_times+=($elapsed_time) +done + +# Calcola la media e deviazione standard per Bun +bun_mean=$(calculate_mean "${bun_times[@]}") +bun_std_dev=$(calculate_std_dev "$bun_mean" "${bun_times[@]}") + +# Esegui 100 test con Node +node_times=() +for i in {1..100}; do + start_time=$(date +%s%3N) + node only.js > /dev/null + end_time=$(date +%s%3N) + elapsed_time=$((end_time - start_time)) + node_times+=($elapsed_time) +done + +# Calcola la media e deviazione standard per Node +node_mean=$(calculate_mean "${node_times[@]}") +node_std_dev=$(calculate_std_dev "$node_mean" "${node_times[@]}") + +# Stampa i risultati +echo "Bun Execution Results:" +echo "Mean Time (ms): $bun_mean" +echo "Standard Deviation: $bun_std_dev" +echo "" +echo "Node Execution Results:" +echo "Mean Time (ms): $node_mean" +echo "Standard Deviation: $node_std_dev"