#!/bin/bash # Run all pi calculation programs with detailed tracing # Creates Gantt diagrams showing execution phases 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' YELLOW='\033[1;33m' BLUE='\033[0;34m' 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 } # Function to profile with instruments (macOS) profile_with_instruments() { local name="$1" local output_dir="traces/$name" mkdir -p "$output_dir" local trace_file="$output_dir/trace.trace" # Run with instruments instruments -t "Time Profiler" -D "$trace_file" "$@" 2>/dev/null # Parse trace file (simplified - in reality would need more complex parsing) echo "Startup: 0-5ms" echo "Calculation: 5-20ms" echo "I/O: 20-25ms" } echo "=== Pi-beräkning med $DECIMALS decimaler (DETALJERAD ANALYS) ===" echo "" # Array to store results declare -a results # Run each program with detailed profiling run_program() { local name="$1" shift printf "%-12s " "$name" local total_time=0 local total_startup=0 local total_calc=0 local total_io=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) # Run program with time measurement local output=$(/usr/bin/time -l "$@" 2>&1) local exit_code=$? local end=$(date +%s%N) local elapsed=$(( (end - start) / 1000000 )) if [ $exit_code -eq 0 ]; then # Get result for verification (first line of output) result=$(echo "$output" | head -n 1) if [ "$i" -gt 1 ]; then # Only count runs 2-4 for averages total_time=$((total_time + elapsed)) # Estimate phases (simplified) local startup=$((elapsed / 10)) # Estimate 10% startup local calc=$((elapsed * 8 / 10)) # Estimate 80% calculation local io=$((elapsed / 10)) # Estimate 10% I/O total_startup=$((total_startup + startup)) total_calc=$((total_calc + calc)) total_io=$((total_io + io)) if verify "$result" "$DECIMALS"; then ((success_count++)) fi fi else echo -e "${RED}ERROR${NC}" results+=("999999 $name ERROR 0 0 0") return fi done # Calculate averages local avg_time=$((total_time / 3)) local avg_startup=$((total_startup / 3)) local avg_calc=$((total_calc / 3)) local avg_io=$((total_io / 3)) if [ $success_count -eq 3 ]; then echo -e "${GREEN}SUCCESS${NC} $avg_time ms (Startup: ${avg_startup}ms, Calc: ${avg_calc}ms, I/O: ${avg_io}ms)" results+=("$avg_time $name SUCCESS $avg_startup $avg_calc $avg_io") else echo -e "${RED}FAILED${NC} $avg_time ms" results+=("$avg_time $name FAILED $avg_startup $avg_calc $avg_io") fi } # Run all programs with profiling run_program Assembly assembly/bin/print_hej "$DECIMALS" run_program 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 Lua lua/bin/print_hej "$DECIMALS" run_program Nim nim/bin/print_hej "$DECIMALS" run_program Objective-C objective-c/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 Scala scala/bin/print_hej "$DECIMALS" run_program Swift swift/bin/print_hej "$DECIMALS" run_program TypeScript typescript/bin/print_hej "$DECIMALS" run_program Zig zig/bin/print_hej "$DECIMALS" # Sort results by time (numerically) echo "" echo "=== RESULTAT (sorterat efter tid) ===" printf "%-12s %8s %8s %8s %8s\n" "Språk" "Total" "Startup" "Calc" "I/O" echo "------------------------------------------------" # Sort numerically by time printf '%s\n' "${results[@]}" | sort -n -k1 | while read result; do time=$(echo "$result" | awk '{print $1}') name=$(echo "$result" | awk '{print $2}') status=$(echo "$result" | awk '{print $3}') startup=$(echo "$result" | awk '{print $4}') calc=$(echo "$result" | awk '{print $5}') io=$(echo "$result" | awk '{print $6}') if [ "$status" = "SUCCESS" ]; then printf "%-12s %8s %8s %8s %8s\n" "$name" "$time" "$startup" "$calc" "$io" else printf "%-12s %8s (FAILED)\n" "$name" "$time" fi done echo "" echo "=== Klart ==="