#!/bin/bash # Run all pi calculation programs and measure performance # Run each program 3 times and take the average set -e SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" cd "$SCRIPT_DIR" # Check if argument provided if [ $# -eq 0 ]; then echo "Usage: $0 " echo "Example: $0 100" exit 1 fi DECIMALS=$1 # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' # No Color # Function to verify result against facit verify() { local result="$1" local decimals="$2" local expected=$(head -c $((decimals + 2)) facit.txt) if [ "$result" = "$expected" ]; then return 0 else return 1 fi } echo "=== Pi-beräkning med $DECIMALS decimaler (4 körningar, genomsnitt av 3 efter warmup) ===" echo "" # Array to store results declare -a results # Run each program 4 times, discard first run (warmup), take average of last 3 run_program() { local name="$1" shift printf "%-12s " "$name" local total_time=0 local success_count=0 local result # Run 4 times, discard first run (warmup) for i in 1 2 3 4; do local start=$(date +%s%N) if result=$("$@" 2>/dev/null); then local end=$(date +%s%N) local elapsed=$(( (end - start) / 1000000 )) # Skip first run (warmup) if [ $i -gt 1 ]; then total_time=$((total_time + elapsed)) if verify "$result" "$DECIMALS"; then ((success_count++)) fi fi else # If any run fails, mark as error echo -e "${RED}ERROR${NC}" results+=("999999 $name ERROR") return fi done # Calculate average local avg_time=$((total_time / 3)) if [ $success_count -eq 3 ]; then echo -e "${GREEN}SUCCESS${NC} $avg_time ms (avg)" results+=("$avg_time $name SUCCESS") else echo -e "${RED}FAILED${NC} $avg_time ms (avg)" results+=("$avg_time $name FAILED") fi } # Run all programs (no timeouts) run_program Bash bash bash/bin/print_hej "$DECIMALS" run_program Brainfuck brainfuck/bin/print_hej "$DECIMALS" run_program C c/bin/print_hej "$DECIMALS" run_program C++ cpp/bin/print_hej "$DECIMALS" run_program Crystal crystal/bin/print_hej "$DECIMALS" run_program CSharp csharp/bin/print_hej "$DECIMALS" run_program D d/bin/print_hej "$DECIMALS" run_program Dart dart/bin/print_hej "$DECIMALS" run_program Elixir elixir/bin/print_hej "$DECIMALS" run_program Erlang erlang/bin/print_hej "$DECIMALS" run_program Fortran fortran/bin/print_hej "$DECIMALS" run_program Go go/bin/print_hej "$DECIMALS" run_program Haskell haskell/bin/print_hej "$DECIMALS" run_program Java java/bin/print_hej "$DECIMALS" run_program JavaScript javascript/bin/print_hej "$DECIMALS" run_program Julia julia/bin/print_hej "$DECIMALS" run_program Kotlin kotlin/bin/print_hej "$DECIMALS" run_program Objective-C objective-c/bin/print_hej "$DECIMALS" run_program Scala scala/bin/print_hej "$DECIMALS" run_program TypeScript typescript/bin/print_hej "$DECIMALS" run_program Lua lua/bin/print_hej "$DECIMALS" run_program Nim nim/bin/print_hej "$DECIMALS" run_program Odin odin/bin/print_hej "$DECIMALS" run_program Perl perl/bin/print_hej "$DECIMALS" run_program PHP php/bin/print_hej "$DECIMALS" run_program Python python/bin/print_hej "$DECIMALS" run_program R r/bin/print_hej "$DECIMALS" run_program Ruby ruby/bin/print_hej "$DECIMALS" run_program Rust rust/bin/print_hej "$DECIMALS" run_program Swift swift/bin/print_hej "$DECIMALS" run_program Zig zig/bin/print_hej "$DECIMALS" run_program Assembly assembly/bin/print_hej "$DECIMALS" run_program Wolfram wolfram/bin/print_hej "$DECIMALS" run_program Vimscript vimscript/bin/print_hej "$DECIMALS" echo "" echo "=== Sammanfattning (sorterad efter prestanda) ===" echo "" # Sort and print results IFS=$'\n' sorted=($(printf '%s\n' "${results[@]}" | sort -n)) unset IFS for entry in "${sorted[@]}"; do time_ms=$(echo "$entry" | awk '{print $1}') name=$(echo "$entry" | awk '{print $2}') status=$(echo "$entry" | awk '{print $3}') printf "%-12s " "$name" if [ "$status" = "SUCCESS" ]; then echo -e "${GREEN}$status${NC} $time_ms ms" else echo -e "${RED}$status${NC} $time_ms ms" fi done echo ""