代码的时间部分很容易。

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个参数:namemethodnestedprint_toc

  • name:它是对象的名称。这不是必需的。
  • method:指示应使用哪个方法获取时间。
  • nested:允许在不同的缩进中多次使用同一对象。
  • print_toc:指示是否要打印调用toc时经过的时间。

参数method可以是intstr,也可以是您的方法选择。如果是字符串,则有效值为timeperf_counterprocess_time。如果是整数,则有效值为012

  • time0:time.time

如果python>;2

  • perf_counter1:time.perf_计数器
  • process_time2:时间。进程时间

如果python版本>;=3.7:

  • time_ns3:时间。时间
  • perf_counter_ns4:time.perf_计数器
  • process_time_ns5:时间。进程时间

如果您希望使用您刚刚使用的其他方法(例如使用time.clock

TicToc(method=time.clock)

如何“打包”

用下面的命令。

python setup.py sdist bdist_wheel

如何将其置于pip上

twine upload dist/*

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java获取范围内的素数和总素数   JAVAlang.IllegalArgumentException:找不到无效的URL或资源。JavaFX图像   java我可以在vaadin中直接流式处理ZipFile吗?   apache poi java poi HSSFsheet如何添加包含图片的页眉和页脚?   java如何在swing中使用GridLayout使列大小不同?   java为什么JPasswordField中的getText()被弃用?   java整数对象是如何创建的?   java在JAAS的策略文件中授予URL权限   linux Openssl(shell脚本)相当于java解密代码   java是无服务器的,结合事件源是可能的吗?   java JPA双向Uknown列   javajavax。加密。IllegalBlockSizeException:解密加密的AES字符串时最后一个块未完成   java如何知道我的ProcessBuilder start()是否成功执行了这些命令   本地特定错误消息中的java插值   java控件SFX通知异常   java文本输入if语句JavaFX   swing java未连接到水平组   java泛型,迭代器>无法访问内部类函数   java如何跳过当前页并写入下一页?   java一次导入许多静态字符串