Python学习日记11计时器

在复查y=wx+b的回归代码时,考虑到sess.run的注入机制,有时feed_dict为单个样本,有时为一批样本数据。编程试验二者的耗时,使用计时器。

查找python官网,
https://docs.python.org/3.5/library/time.html

import time

time.time()
1542160081.5948985

time.gmtime(0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

time.clock()
87.74517806460884

time.clock():在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。(http://qinxuye.me/article/details-about-time-module-in-python/)

import time
print (“clock0:%s” % time.clock() )
time.sleep(1)
print (“clock1:%s” % time.clock() )
time.sleep(1)
print (“clock2:%s” % time.clock() )
time.sleep(1)
print (“clock3:%s” % time.clock())
结果
clock0:315.5617051360925
clock1:316.56123542491656
clock2:317.5612064453067
clock3:318.56131572258545

在代码中填入

start_time = time.clock()

end_time=time.clock()
print(‘it takes’,end_time-start_time,‘seconds’)

结果分别是

it takes 1.3319517706843556 seconds
it takes 66.22324744300221 seconds

单个注入数据的周期计时

start time is 563.0112670308035
563.0114560020093
563.156922775981
Epoch: 1 cost= 0.34154907 W= [1.1450434] b= [0.9310361]
574.805068654507
Epoch: 201 cost= 0.0738145 W= [1.9640125] b= [0.7771161]
586.3616057601804
Epoch: 401 cost= 0.0738145 W= [1.9640125] b= [0.7771161]
597.9211386330197
Epoch: 601 cost= 0.0738145 W= [1.9640125] b= [0.7771161]
609.7356063409801
Epoch: 801 cost= 0.0738145 W= [1.9640125] b= [0.7771161]
it takes 58.254618474380436 seconds
Finished!

试验结果
批量注入数据比一次注入单个样本速度快很多
但估计单次注入过多样本也可能出现问题,因此每次要注入适量的样本最好

???疑问,官网中设置时钟的命令
time.clock_settime(clk_id, time)
AttributeError: module ‘time’ has no attribute ‘clock_settime’
使用查询语句

time
<module ‘time’ (built-in)>
dir(time)
[’_STRUCT_TM_ITEMS’,
doc’,
loader’,
name’,
package’,
spec’,
‘altzone’,
‘asctime’,
‘clock’,
‘ctime’,
‘daylight’,
‘get_clock_info’,
‘gmtime’,
‘localtime’,
‘mktime’,
‘monotonic’,
‘perf_counter’,
‘process_time’,
‘sleep’,
‘strftime’,
‘strptime’,
‘struct_time’,
‘time’,
‘timezone’,
‘tzname’]

最后重新回到官网说明https://docs.python.org/3.5/library/time.html,发现写得很清楚,只有在Unix下可用
time.clock_gettime(clk_id)
Return the time of the specified clock clk_id.
Availability: Unix.
New in version 3.3.

3.5的文档中
time.clock_getres(clk_id)
Return the resolution (precision) of the specified clock clk_id.
Availability: Unix.

New in version 3.3.
在3.7的说明文档中
time.clock_getres(clk_id)
Return the resolution (precision) of the specified clock clk_id. Refer to Clock ID Constants for a list of accepted values for clk_id.
Availability: Unix.
New in version 3.3.

猜你喜欢

转载自blog.csdn.net/weixin_43387285/article/details/84061584
今日推荐