Python打印时间|Python统计脚本运行时间

1、python 打印时间

import time 

print('my name:%s'%time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 

print('my name2:%s'%time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))

from time import strftime, localtime

# 打印当前时间

def printTime():

    print(strftime("%Y-%m-%d %H:%M:%S", localtime()))

    return

2、程序运行时间

import time 

import datetime

#print('my name:%s'%time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 

#print('my name2:%s'%time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))

def run_fun(num):

    i=0

    cycle = 10000000*num

    while i<cycle:

          i+=1

    print("run process")

starttime = datetime.datetime.now()

run_fun(5)

endtime = datetime.datetime.now()

seconds  = (endtime - starttime).seconds

print seconds

m, s = divmod(seconds, 60)

h, m = divmod(m, 60)

print("%d:%02d:%02d" % (h, m, s))

start = time.time()

run_fun(5)

end = time.time()

seconds = end-start

print seconds

m, s = divmod(seconds, 60)

h, m = divmod(m, 60)

print("%d:%02d:%02d" % (h, m, s))

start = time.clock()

run_fun(5)

end = time.clock()

seconds = end-start

print seconds

m, s = divmod(seconds, 60)

h, m = divmod(m, 60)

print("%d:%02d:%02d" % (h, m, s))

https://edu.csdn.net/topic/python115?utm_source=blogt3two

发布了583 篇原创文章 · 获赞 96 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/bandaoyu/article/details/102578317
今日推荐