Files
TDT4200/exercise2/bench.py
2025-09-10 18:01:34 +02:00

25 lines
591 B
Python
Executable File

#!/usr/bin/env python3
from statistics import mean
from subprocess import DEVNULL, run
from sys import argv, exit
from time import perf_counter
def benchmark(cmd, N=10):
times = []
for _ in range(N):
start = perf_counter()
run(cmd, shell=True, stdout=DEVNULL, stderr=DEVNULL)
times.append(perf_counter() - start)
return mean(times)
if __name__ == "__main__":
if len(argv) < 2:
print("usage: python3 bench.py 'cmd' [N]")
exit(1)
cmd = argv[1]
N = int(argv[2]) if len(argv) > 2 else 10
print(f"{benchmark(cmd, N):.4f}")