mirror of
https://github.com/thib8956/advent-of-code.git
synced 2026-11-27 00:46:34 +00:00
intcode: better benchmark setup
This commit is contained in:
1 parent
50fbc74406
commit
06956da329
2 files changed
+33
-20
No files matched your search
@@ -14,10 +14,21 @@ def sum_of_primes(n):
|
||||
return res.stdout.pop()
|
||||
|
||||
|
||||
def prime_factors(n):
|
||||
program = [3,1,109,583,108,0,1,9,1106,-1,14,4,1,99,107,0,1,19,1105,-1,27,104,-1,102,-1,1,1,21101,0,38,0,20101,0,1,1,1105,1,138,2101,1,1,41,101,596,41,45,1101,1,596,77,1101,0,1,53,101,1,77,77,101,1,53,53,7,45,77,67,1105,-1,128,108,1,1,74,1105,-1,128,1005,-1,54,1,53,77,93,7,45,93,88,1105,-1,101,1101,0,1,-1,1,53,93,93,1105,1,83,21101,0,116,0,20101,0,1,1,20101,0,53,2,1105,1,235,1205,2,54,4,53,2101,0,1,1,1105,1,101,108,1,1,133,1105,-1,137,4,1,99,22101,0,1,2,22101,0,1,1,21101,0,163,3,22101,0,1,4,22101,0,2,5,109,3,1105,1,198,109,-3,22102,-1,1,1,22201,1,4,3,22102,-1,1,1,1208,3,0,182,2105,-1,0,1208,3,1,189,2105,-1,0,22101,0,4,1,1105,1,146,1207,1,1,203,2105,-1,0,21101,0,222,3,22101,0,2,4,22101,0,1,5,109,3,1105,1,235,109,-3,22201,1,4,1,21101,0,2,2,1105,1,235,1105,0,280,101,383,236,243,1107,-1,583,247,1106,-1,276,101,383,236,256,102,1,275,-1,102,2,275,275,1007,275,0,266,1105,-1,280,101,1,236,236,1105,1,238,1,101,-1,236,236,101,383,236,286,207,1,-1,289,1106,-1,-1,22101,0,1,3,2102,1,2,363,2102,-1,2,369,22102,0,1,1,22102,0,2,2,101,1,236,320,101,-1,320,320,1107,-1,0,324,2105,-1,0,22102,2,2,2,101,383,320,336,207,3,-1,339,1105,-1,361,22101,1,2,2,22102,-1,3,3,101,383,320,354,22001,-1,3,3,22102,-1,3,3,1207,2,-1,366,1105,-1,315,22101,-1,2,2,101,383,320,377,22001,-1,1,1,1105,1,315]
|
||||
res = interpret_intcode(program, stdin=[n])
|
||||
return res.stdout
|
||||
|
||||
|
||||
def run_benchmark(name, stmt, repeat=5):
|
||||
timer = timeit.Timer(stmt)
|
||||
number, _ = timer.autorange()
|
||||
times = timer.repeat(repeat=repeat, number=number)
|
||||
best = min(times) / number
|
||||
print(f"{name}: {best:.3g} sec per loop (best of {repeat}, {number} loops)")
|
||||
|
||||
if __name__ == '__main__':
|
||||
#import timeit
|
||||
#print(timeit.timeit("test()", globals=locals()))
|
||||
print(isqrt(111))
|
||||
print(sum_of_primes(10))
|
||||
|
||||
import timeit
|
||||
run_benchmark("isqrt(111)", lambda: isqrt(111))
|
||||
run_benchmark("sum_of_primes(100_000)", lambda: sum_of_primes(100_000))
|
||||
run_benchmark("prime_factors(2147483647)", lambda: prime_factors(2147483647))
|
||||
Reference in new issue
Block a user