Created
November 15, 2024 18:30
-
-
Save dceddia/fd92406dbb5bc4cc3e25983716921933 to your computer and use it in GitHub Desktop.
Language benchmarks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function to measure execution time | |
measure_time() { | |
start_time=$(date +%s.%N) | |
"$@" | |
end_time=$(date +%s.%N) | |
execution_time=$(echo "$end_time - $start_time" | bc) | |
echo "Execution time: $execution_time seconds" | |
} | |
# JavaScript benchmark | |
echo "Running JavaScript benchmark..." | |
cat << EOF > benchmark.js | |
let array = new Array(10000); | |
for (let i = 0; i < 10000; i++) { | |
for (let j = 0; j < 100000; j++) { | |
array[i] = array[i] + j; | |
} | |
} | |
EOF | |
measure_time node benchmark.js | |
# Python benchmark | |
echo -e "\nRunning Python benchmark..." | |
cat << EOF > benchmark.py | |
array = [0] * 10000 | |
for i in range(10000): | |
for j in range(100000): | |
array[i] = array[i] + j | |
EOF | |
measure_time python3 benchmark.py | |
# Go benchmark | |
echo -e "\nRunning Go benchmark..." | |
cat << EOF > benchmark.go | |
package main | |
func main() { | |
var array [10000]int | |
for i := 0; i < 10000; i++ { | |
for j := 0; j < 100000; j++ { | |
array[i] = array[i] + j | |
} | |
} | |
} | |
EOF | |
go build benchmark.go | |
measure_time ./benchmark | |
# C benchmark | |
echo -e "\nRunning C benchmark..." | |
cat << EOF > benchmark.c | |
#include <stdio.h> | |
int main() { | |
long array[10000]; | |
for (int i = 0; i < 10000; i++) { | |
for (int j = 0; j < 100000; j++) { | |
array[i] = array[i] + j + i; | |
} | |
} | |
return 0; | |
} | |
EOF | |
gcc -O3 benchmark.c -o benchmark_c | |
measure_time ./benchmark_c | |
# Clean up | |
rm benchmark.js benchmark.py benchmark.go benchmark.c benchmark benchmark_c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment