Skip to main content
grammar errors and a slight re-wording + format added
Source Link
user3666197
  • 1
  • 6
  • 54
  • 102

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:

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 time interval.

I wander the reason and a possible solution. I guess it's related to cpu cache or some mechanism of cPython vm.

result:

without sleep in loop, time consume: 50us with sleep in loop, time consume: >150us 

some wrongs about the sleep releases cpu, i provide the exactly same version with c code below:

another experiment:(we remove the for loop)

result:

execution time decrease from 150us -> 50us gradually. and keep stable in 50us. 

to find out whether this is problem of cup cache, I write a c counterpart. And find out that no this kind of phenomenon happens.

result:

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 wonder the reason and a possible solution. 

I guess it's related to CPU-cache or some mechanism of cPython VM.

result:

without sleep in loop, time consumed:  50us with  a sleep in loop, time consumed: >150us 

some disadvantage of the .sleep() is, that it releases CPU, thus I provide the exactly same version with a C code below:

another experiment: (we remove the for loop)

result:

execution time decreased from 150us -> 50us gradually.  and keep stable in 50us. 

to find out whether this is problem of CPU-cache, I wrote a C counterpart. And have found out that this kind of phenomenon does not happen.

result:

added 558 characters in body
Source Link
doge
  • 11
  • 3

some wrongs about the sleep releases cpu, i provide the exactly same version with c code below:

''' Created on Aug 22, 2015 @author: doge ''' import numpy as np import time import gc gc.disable() t = np.arange(100000) count = 0 for i in xrange(100): count += 1 if ( count % 1000000 != 0 ): continue #--> these three lines make the following lines much slower st = time.time() np.sum(t) print (time.time() - st)*1e6 

another experiment:(we remove the for loop)

another experiment:(we remove the for loop)

some wrongs about the sleep releases cpu, i provide the exactly same version with c code below:

''' Created on Aug 22, 2015 @author: doge ''' import numpy as np import time import gc gc.disable() t = np.arange(100000) count = 0 for i in xrange(100): count += 1 if ( count % 1000000 != 0 ): continue #--> these three lines make the following lines much slower st = time.time() np.sum(t) print (time.time() - st)*1e6 

another experiment:(we remove the for loop)

edited title
Link
doge
  • 11
  • 3

python function(or a code block) runs much slower with ana time interval in a loop

added 448 characters in body
Source Link
doge
  • 11
  • 3
Loading
added 168 characters in body
Source Link
doge
  • 11
  • 3
Loading
added 168 characters in body
Source Link
doge
  • 11
  • 3
Loading
added 20 characters in body; edited title
Source Link
doge
  • 11
  • 3
Loading
Source Link
doge
  • 11
  • 3
Loading