import sys
import re
import numpy as np
import matplotlib.pyplot as plt

bitonic_times = []
stl_times     = []
qsort_times   = []

pattern = re.compile(
    r'(bitonic|stl|qsort)\s+time\s*=\s*([\d.]+)\s*ms',
    re.IGNORECASE
)

for line in sys.stdin:
    index = -1
    if "Repeating all tests" in line:
        index += 1
        bitonic_times.append([])
        stl_times.append([])
        qsort_times.append([])
        continue
    match = pattern.search(line)
    if match:
        test_type = match.group(1).lower()
        time = float(match.group(2))

        if test_type == 'bitonic':
            bitonic_times[index].append(time)
        elif test_type == 'stl':
            stl_times[index].append(time)
        elif test_type == 'qsort':
            qsort_times[index].append(time)

def getmean(array):
    ret = []
    for x in range(len(array[0])):
        sum = 0
        for y in range(len(array)):
            sum += array[y][x]
        ret.append(sum / len(array))
    return np.array(ret)

def reshape(array):
    arr = np.array(array)
    arr = arr.reshape(9, 16, order='F')
    return arr

def plot1(bs, stl, qs):
    markers = ['o', 's', '^', 'D', 'v', '>', '<', 'p', '*', 'd']
    cmap = plt.cm.get_cmap('tab10')

    fig, ax = plt.subplots(figsize=(12,8))
    arr = reshape(getmean(bs))
    xaxes = [pow(2, i)*np.log(pow(2, i)) for i in range(10, 10+len(arr[0]))]
    xkes  = [pow(2, i)                   for i in range(10, 10+len(arr[0]))]
    for x in range(len(arr)):
        ax.plot(xkes, arr[x]/xaxes * 1e6,
                color=cmap(x/8),
                marker=markers[x],
                linestyle='-',
                ms=5,
                label=f"bitonic {pow(2, x)} trd")
    ax.plot(xkes, getmean(reshape(stl[0]))/xaxes * 1e6, 'bx-', label="std::sort", ms=5)
    ax.plot(xkes, getmean(reshape(qs[0]))/xaxes * 1e6, 'ch-', label="qsort", ms=5)
    ax.legend()
    ax.set(ylabel=r'$ T(n) / (n\cdot \log{n}) $', xlabel=r'$ n $')
    ax.set_title(r'$ \frac{T(n)}{ (n\cdot \log{n})} (n) $')
    ax.grid(which='major')
    ax.minorticks_on()
    ax.grid(which='minor', linestyle=':')
    ax.set_xscale('log', base=2)
    plt.show()

plot1(bitonic_times, stl_times, qsort_times)

def plot2(bs, stl, qs):
    fig, ax = plt.subplots(figsize=(12,8))

    markers = ['o', 's', '^', 'D', 'v', '>', '<', 'p', '*', 'd']
    cmap = plt.cm.get_cmap('tab10')

    arr = reshape(getmean(bs))
    xkes  = [pow(2, i) for i in range(10, 10+len(arr[0]))]

    for x in range(len(arr)):
        ax.plot(xkes, arr[x],
                color=cmap(x/8),
                marker=markers[x],
                linestyle='-',
                ms=5,
                label=f"bitonic {pow(2, x)} trd")

    ax.plot(xkes, getmean(reshape(stl[0])), 'bx-', label="std::sort", ms=4)
    ax.plot(xkes, getmean(reshape(qs[0])),  'ch-', label="qsort",     ms=4)

    ax.legend()
    ax.set(ylabel=r'$ T(n), ms $', xlabel=r'$ n $')
    ax.set_title(r'Среднее время сортировки от количества элементов')
    ax.grid(which='major')
    ax.grid(which='minor', linestyle=':')
    ax.minorticks_on()
    plt.show()

plot2(bitonic_times, stl_times, qsort_times)
