-
Notifications
You must be signed in to change notification settings - Fork 0
/
precision-time.sh
52 lines (40 loc) · 1.16 KB
/
precision-time.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/bash
# Check for correct number of arguments
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <count> <program> [args...]"
exit 1
fi
# Read arguments
count=$1
shift
program="$@"
# Initialize timing variables
total_time=0
min_time=999999999
max_time=0
# Run the program the specified number of times
for ((i = 1; i <= count; i++)); do
start=$(date +%s.%N)
$program >/dev/null 2>&1
end=$(date +%s.%N)
elapsed=$(echo "$end - $start" | bc)
# Accumulate total time
total_time=$(echo "$total_time + $elapsed" | bc)
# Update min and max times
if (( $(echo "$elapsed < $min_time" | bc -l) )); then
min_time=$elapsed
fi
if (( $(echo "$elapsed > $max_time" | bc -l) )); then
max_time=$elapsed
fi
done
# Calculate average time
average_time=$(echo "$total_time / $count" | bc -l)
# Format all times with leading zero and 9 decimal places
average_time=$(echo "$average_time" | awk '{printf "%.9f", $1}')
min_time=$(echo "$min_time" | awk '{printf "%.9f", $1}')
max_time=$(echo "$max_time" | awk '{printf "%.9f", $1}')
# Output results
echo "Average Time: $average_time seconds"
echo "Minimum Time: $min_time seconds"
echo "Maximum Time: $max_time seconds"