Python function to convert seconds into minutes, hours, and days

前端 未结 16 1380
不思量自难忘°
不思量自难忘° 2020-11-28 07:27

Question: Write a program that asks the user to enter a number of seconds, and works as follows:

  • There are 60 seconds in a minute. If the number of seconds

16条回答
  •  粉色の甜心
    2020-11-28 07:52

    The "timeit" answer above that declares divmod to be slower has seriously flawed logic.

    Test1 calls operators.

    Test2 calls the function divmod, and calling a function has overhead.

    A more accurate way to test would be:

    import timeit
    
    def moddiv(a,b):
      q= a/b
      r= a%b
      return q,r
    
    a=10
    b=3
    md=0
    dm=0
    for i in range(1,10):
      c=a*i
      md+= timeit.timeit( lambda: moddiv(c,b))
      dm+=timeit.timeit( lambda: divmod(c,b))
    
    print("moddiv ", md)
    print("divmod ", dm)
    
    
    
    
    moddiv  5.806157339000492
    
    divmod  4.322451676005585
    

    divmod is faster.

提交回复
热议问题