I notice a case in python., when a block of code, nested in a loop, runs continuously, it is much faster than running with some .sleep() time interval.
I wander the reasonwonder the reason and a possible solutionpossible solution.
I guess it's related to cpu cacheCPU-cache or some mechanism of cPython vmVM.
result:result:
without sleep in loop, time consumeconsumed: 50us with a sleep in loop, time consumeconsumed: >150us some wrongs aboutdisadvantage of the sleep.sleep() is, that it releases cpuCPU, ithus I provide the exactly same version with ca C code below:
another experiment:(we remove the for loop)another experiment: (we remove the for loop)
result:result:
execution time decreasedecreased from 150us -> 50us gradually. and keep stable in 50us. to find out whether this is problem of cup cacheCPU-cache, I writewrote a cC counterpart. And findhave found out that no this kind of phenomenon happensdoes not happen.
result:result: