What is the difference between int() and floor() in Python 3?

后端 未结 2 2101
死守一世寂寞
死守一世寂寞 2020-11-30 02:39

In Python 2, floor() returned a float value. Although not obvious to me, I found a few explanations clarifying why it may be useful to have floor()

2条回答
  •  一向
    一向 (楼主)
    2020-11-30 03:26

    I test time complexity of both method they are the same

    from time import time
    import math
    import random
    
    r = 10000000
    def floorTimeFunction():
      for i in range(r):
        math.floor(random.randint(-100,100))
    
    def intTimeFunction():
      for i in range(r):
        int(random.randint(-100,100))
    
    t0 = time()
    floorTimeFunction()
    t1 = time()
    intTimeFunction()
    t2 = time()
    
    print('function floor takes %f' %(t1-t0))
    print('function int   takes %f' %(t2-t1))
    

    output is:

    # function floor takes 11.841985
    # function int   takes 11.841325
    

提交回复
热议问题