Get timer ticks in Python

后端 未结 6 712
一整个雨季
一整个雨季 2020-12-05 01:46

I\'m just trying to time a piece of code. The pseudocode looks like:

start = get_ticks()
do_long_code()
print \"It took \" + (get_ticks() - start) + \" secon         


        
6条回答
  •  有刺的猬
    2020-12-05 02:38

    import datetime
    
    start = datetime.datetime.now()
    do_long_code()
    finish = datetime.datetime.now()
    delta = finish - start
    print delta.seconds
    

    From midnight:

    import datetime
    
    midnight = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
    now = datetime.datetime.now()
    delta = now - midnight
    print delta.seconds
    

提交回复
热议问题