I implemented an algorithm in rust for speed, which is then built into a python module. Running the function is indeed much faster than the python implementation. But I noticed an interesting quirk: Running the function a lot of times (say, 1 million) is on average much faster than just running the function once or a few times.
print(timeit.timeit(lambda: blob(9999, 16, (3, 5), (4, 11), (2, 4)), number=1))
print(timeit.timeit(lambda: blob(9999, 16, (3, 5), (4, 11), (2, 4)), number=1000000) / 1000000)
Output:
1.5100000382517464e-05
2.1137116999998398e-06
As you can see, the function being executed 1000000 times is on average, is about 7 times faster than only running it once.
Any idea why this is happening? Any help would be appreciated.
If the code of the rust function is needed to pinpoint the problem, just send a comment and I'll put it here :)