aboutsummaryrefslogtreecommitdiff
path: root/optim.sh
blob: 04accf97fd24c71e72ea604329564e22deaedd40 (plain) (blame)
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
53
#!/bin/bash

TRIALS=100
SUCCESSES=10

echo "if this takes too long, either choose a random timeout from the trials, adapt the script accordingly, or just use smaller numbers hehe"

set -e

make c
REFERENCE=$(./csort)

function test() {
	make asm
	for i in $(seq $TRIALS); do
		output=$(stdbuf -i0 -o0 -e0 ./sort)
		if [ "$output" != "$REFERENCE" ]; then
			return 1
		fi
	done
	return 0
}

function substitute() {
	sed -ie "/^%define TIMEOUT/s/ [0-9]\+/ $1/" config.asm
}

# TODO: Multithreading
minimum=4294967295
successes=0
timeout=1
while true; do
	substitute $timeout
	if test; then
		successes=$((successes + 1))
		echo "$successes/$SUCCESSES with $timeout"

		if [ $timeout -le $minimum ]; then
			minimum=$timeout
		fi

		if [ $successes -eq $SUCCESSES ]; then
			break
		fi

		timeout=$((timeout - timeout / 3))
	else
		timeout=$((timeout * 2))
	fi
done

substitute $minimum
echo "Done. Timeout $minimum will probably work most of the time."