#!/usr/bin/env python
from sys import argv
from pylab import plot, xlabel, ylabel, show, savefig, ylim
from scipy import polyfit, polyval

# Collect data
x = []
y = []
results = open('bench.txt')
for line in results.readlines():
    n, t = line[:-1].split(' ')

    # Use the minimum of all measurements
    if len(x) and x[-1] == int(n):
        if y[-1] > float(t):
            y[-1] = float(t)
        continue

    x.append(int(n))
    y.append(float(t))

seq = [x[0], y[0]]
x = x[1:]
y = y[1:]

# Least squares data fit
c = tuple(polyfit(x, y, 2).tolist())
fit = polyval(c, x)

# Plot and optionally save data
plot(x, y, 'x')
plot(x, fit, '-')
plot(seq[0], seq[1], 'x')
ylim(ymin=0)
xlabel('Aantal processen')
ylabel('Tijd (s)')
if len(argv) > 1:
    savefig(argv[1])
show()
