Nested loop comparison in Python,Java and C -
the following code in python takes long run. (i couldn't wait until program ended, though friend told me him took 20 minutes.)
but equivalent code in java runs in approximately 8 seconds , in c takes 45 seconds.
i expected python slow not much, , in case of c expected faster java slower. jvm using loop unrolling technique achieve speed? there reason python being slow?
import time st=time.time() in xrange(0,100000): j in xrange(0,100000): continue; print "time taken : ",time.time()-st
gcc 4.2 -o1 flag or higher optimize away loop , program takes 1 milli second execute. benchmark not representative far real world use. you're doing nested loop reason, , never leave empty. python doesn't optimize away loop, although see no technical reason why couldn't.
python slower c because it's further machine language. xrange nice abstraction adds heavy layer of machine code compared simple c loop.
c source:
int main( void ){ int i, j; (i=0;i<100000;i++){ (j=0;j<100000;j++){ continue; } } return 0; }
Comments
Post a Comment