TicToc 0.1.0

TicToc 0.1.0

Roberto Sartori维护。



TicToc 0.1.0

  • 作者
  • terrordrummer

TicToc

CI Status Version License Platform

示例

要运行示例项目,首先克隆仓库,然后在示例目录中运行pod install

安装

您可以通过CocoaPods使用TicToc。要安装它,只需将以下行添加到您的Podfile中

pod 'TicToc'

概述

TicToc是一个简单的模块,用于测量两个事件之间的耗时:分别是tic()toc()。在tic()事件触发开始时间戳后,会调用多个toc()事件以获取累积的耗时。

一旦调用tic()事件,就可以调用多个toc()事件以获取增量耗时。

有两个可用的tic/toc事件:一个静态配对和一个绑定到实例的配对。当tic/toc事件需要从不同的类触发时,静态配对很有用,而绑定到实例的配对则用于本地测量。

提供了一些额外的静态辅助方法,以便以更易读的方式编写耗时测量代码。

示例

静态Tic/Toc

调用静态方法 TicToc.tic() 来开始测量,调用 TicToc.toc() 来检索耗时。

实例绑定的Tic/Toc

任何需要的地方都可以创建一个TicToc类的实例

let tictoc = TicToc()

默认情况下,创建一个 TicToc() 实例时,会调用 tic() 方法。然后,调用 toc() 方法以获取耗时。

// ... some operations
let elapsedTime = tictoc.toc()

测量辅助

提供了两个静态方法 TicToc.measure,作为可选项,使测量操作更易于阅读。

一个版本是同步测量操作,而另一个版本提供一个完成块,在操作完成后调用(因此操作块可以包含一些异步分派的操作)。

同步版本接受一个标签,作为日志字符串的前缀使用

TicToc.measure(label: "Operation completed in") {
	// ...some operations
}
// this will log "Operation completed in 3.23 sec"

第二个版本接受标签并同步执行操作块,但闭包会收到一个完成回调,在操作完成时调用

TicToc.measure(label: "Operation completed in") { (completion) in

	// ...do whatever needed, dispatch, fetch, etc...
	
	// wherever the measured operation is completed
	completion()
}

日志控制

通常,您可能不希望在生产环境中生成时间测量日志。为解决这个问题,提供了一个静态标志 logEnabled,用来启用/禁用后一个 measure 方法的输出。

TicToc.logEnabled = false // <- inhibits the log output
TicToc.measure(label: "Operation completed in") {
	// ...
}
// no logs will be printed

作者

terrordrummer, [email protected], http://robertosartoridev.com

许可证

TicToc提供MIT许可。有关更多信息,请参阅LICENSE文件。