‘壹’ python计算程序运行时间—time.time()、time.clock()
在Python中,为了计算程序运行时间,可以使用time.time和time.clock函数,它们各自有不同的用途和特点:
time.time:
time.clock:
总结: 使用time.time可以测量程序运行的实际时间,包括所有延迟。 使用time.clock可以测量CPU密集型任务的执行时间,但在Python 3中应考虑使用time.perf_counter或time.process_time。 在编写代码时,应尽量减少不必要的开销,如频繁的打印操作,以优化程序性能。
‘贰’ python编程,使用Tkinter中的文本框显示系统时间
Python编程中,用Tkinter中的文本框获取系统当前的时间并且显示,代码如下:
importsys
fromtkinterimport*
importtime
deftick():
globaltime1
#从运行程序的计算机上面获取当前的系统时间
time2=time.strftime('%H:%M:%S')
#如果时间发生变化,代码自动更新显示的系统时间
iftime2!=time1:
time1=time2
clock.config(text=time2)
#
#
#coulse>200ms,butdisplaygetsjerky
clock.after(200,tick)
root=Tk()
time1=''
status=Label(root,text="v1.0",bd=1,relief=SUNKEN,anchor=W)
status.grid(row=0,column=0)
clock=Label(root,font=('times',20,'bold'),bg='green')
clock.grid(row=0,column=1)
tick()
root.mainloop()