#!/bin/bash

set -eu
#set -o pipefail

max_duration=3000

function pad
{
	echo "$1                              " | sed 's/^\(...................................\).*$/\1/'
}

function timer
{
	n="$1"
	shift
	basename="$1"
	name="$1.dat"
	shift
    cmd="$*"
	t0=$(python3 -c 'import time; print(time.time())')
	# shellcheck disable=SC2086
	if maxtime "$max_duration" $cmd
    then
        t1=$(python3 -c 'import time; print(time.time())')
        duration=$( (echo scale=2; echo "$t1-$t0") | bc)
        echo "$n" "$duration" >> "$name"
        echo "$(pad "$basename")$duration" 1>&2
    else
        echo "$(pad "$basename")timeout" 1>&2
    fi
}

rm -f ./*-n.dat

top=12

for exponent in $(seq 0 $top)
do
	n=$((10**exponent))
    m=1000
	echo "$exponent (going to $top) m=$m n=$n $(date)"
	echo Starting timings...
	./random-numbers | timer "$n" "highest-treap-n" ./highest --use-treap -n "$m" > /dev/null
	./random-numbers | timer "$n" "highest-bisect-n" ./highest --use-bisect -n "$m" > /dev/null
	./random-numbers | timer "$n" "highest-heap-n" ./highest --use-heap -n "$m" > /dev/null
	./random-numbers | timer "$n" "highest-rbtree-n" ./highest --use-rbtree -n "$m" > /dev/null
	./random-numbers | timer "$n" "highest-fibonacci-heap-n" ./highest --use-fibonacci-heap -n "$m" > /dev/null
	./random-numbers | timer "$n" "sort-n" ./'sort-head' -n "$m" > /dev/null
	./random-numbers | timer "$n" "highest-treap-n-no-dups" ./highest --use-treap -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$n" "highest-bisect-n-no-dups" ./highest --use-bisect -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$n" "highest-rbtree-n-no-dups" ./highest --use-rbtree -n "$m" --disallow-duplicates > /dev/null
	./random-numbers | timer "$n" "sort-n-no-dups" ./'sort-head' -n "$m" --disallow-duplicates > /dev/null
	echo
done

rm -f temp.bz2
rm -f temp.xz