tests/bench/perf_bench_primi.primi

Summary

Maintainability
Test Coverage
import std.time
import std.runtime.memory

t = time.monotonic()

max_iter = 50000
tenth_iter = max_iter / 10

function decor(fn) {
    return () => {
        print("(", end: '')
        fn()
        print(")", end: '')
    }
}

function bench_function_calls() {

    adder = (x, y) => { return x + y; }

    result = -1024
    c = 0
    while (c < max_iter) {
        result = result + adder(c, 1)
        c = c + 1
        if (c.is_divisible_by(tenth_iter)) {
            print(':', end: '')
        }
    }

    return c

}

function bench_regex_matches() {

    haystack = "Když začínáme myslet, nemáme k dispozici nic jiného než myšlenku v " +
        "její čisté neurčenosti, neboť k určení již patří jedno nebo nějaké " +
        "jiné, ale na začátku ještě nemáme žádné jiné..."

    result = 0
    c = 0
    while (c < max_iter) {
        result = result + number(haystack == rx"^.*(zač).*(,)?.*?(\.)")
        c = c + 1
        if (c.is_divisible_by(tenth_iter)) {
            print(':', end: '')
        }
    }

    return c

}

function bench_dicts() {

    c = 0
    result = []

    while (c < max_iter) {

        dict = {
            'a': true,
            'b': false,
            'c': null,
            'd': 'áhojky, plantážníku!',
            'keys': ['a', 'b', 'c'],
        }

        for (name in dict['keys']) {
            result[] = dict[name]
        }

        c = c + 1
        if (c.is_divisible_by(tenth_iter)) {
            print(':', end: '')
        }

    }

    return result

}

decor(bench_function_calls)()
decor(bench_regex_matches)()
decor(bench_dicts)()

print()
print(f"{time.monotonic() - t}\n{memory.get_peak() / 1_000_000}")