代码的时间部分很容易。
ttictoc的Python项目详细描述
总有机碳
它允许您以简单的方式计时代码的某些部分。
你可以从皮普那里得到。
pipinstallttictoc
如何使用
首先导入类
fromttictocimportTicToc
现在,它们是几种使用它的方法。
使用“with”语句
不需要创建任何对象,就可以按如下方式计时代码。
withTicToc('name'):somecode...# Prints the elapsed time
或者通过创建一个对象,你可以做同样的事情。
t=TicToc()witht:somecode...# Prints the elapsed time
显式调用tic toc
您还可以明确地调用tic toc,如下所示。
t=TicToc('name')t.tic()somecode...t.toc()# Prints and returns the elapsed timeprint(t.elapsed)
或
t=TicToc()t.tic()somecode...t.toc()# Prints and returns the elapsed time
嵌套
如果要计时代码的多个级别,也可以将“nested”设置为true。
t=TicToc(nested=True)t.tic()somecode1...t.tic()somecode2...t.tic()somecode3...t.toc()# Prints time for code 3 t.toc()# Prints time for code 2 with code 3t.toc()# Prints time for code 1 with code 2 and 3
不想写“T”?
你可以做如下的事情。
t=TicToc()tic=t.tictoc=t.toctic()somecode...toc()
更好的是,直接进口。
fromttictocimporttic,toc
使用它们。
tic()somecode...toc()
嵌套的tic-toc呢?
tic(nested=True)somecode1...tic(nested=True)somecode2...toc()# Prints the time that took some code 2 to runtoc()# Prints the time that took some code 1 and 2 to run
或
tic(nested=True)somecode1...tic()somecode2...toc()# Prints the time that took some code 2 to runtoc()# Prints the time that took some code 1 and 2 to run
是一样的。
可用方法
set_print_toc
:设置TOC关于打印已用时间的全局行为。但是,如果使用toc(print_elapsed=true),则对于该特定toc,即使将set_print_toc
置于False
,也将打印经过的时间。set_nested
:表示tic-toc是否允许嵌套。
fromttictocimportTicToct=TicToc()t.set_nested(True)t.set_print_toc(False)
参数
类有4个参数:name
、method
、nested
和print_toc
。
name
:它是对象的名称。这不是必需的。method
:指示应使用哪个方法获取时间。nested
:允许在不同的缩进中多次使用同一对象。print_toc
:指示是否要打印调用toc时经过的时间。
参数method
可以是int
、str
,也可以是您的方法选择。如果是字符串,则有效值为time
、perf_counter
和process_time
。如果是整数,则有效值为0
、1
和2
。
time
或0
:time.time
如果python>;2
perf_counter
或1
:time.perf_计数器process_time
或2
:时间。进程时间
如果python版本>;=3.7:
time_ns
或3
:时间。时间perf_counter_ns
或4
:time.perf_计数器process_time_ns
或5
:时间。进程时间
如果您希望使用您刚刚使用的其他方法(例如使用time.clock
:
TicToc(method=time.clock)
如何“打包”
用下面的命令。
python setup.py sdist bdist_wheel
如何将其置于pip上
twine upload dist/*