Is there a performance cost putting python imports inside functions?

后端 未结 3 1631
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 00:53

I build quite complex python apps, often with Django. To simplify inter-application interfaces I sometimes use service.py modules that abstract away from the models.

As

3条回答
  •  余生分开走
    2021-02-04 01:28

    As ray said, importing specific functions is (slightly faster) 1.62852311134 for sin() 1.89815092087 for math.sin() using the following code

    from time import time
    sin=math.sin
    t1=time()
    for i in xrange(10000000):
    x=sin(i)
    t2=time()
    for i in xrange(10000000):
        z=math.sin(i)
    t3=time()
    print (t2-t1)
    print (t3-t2)
    

提交回复
热议问题