为代码计时以识别性能瓶颈。
将您要计时的代码包裹在 +[XINBlockTimer timeBlock:]
里面。
#import "XINBlockTimer.h"
[XINBlockTimer timeBlock:^{
// For example: time creating an attributed string with HTML //
NSString *html = @"<style>p {font: 12px HelveticaNeue; text-align: center;}</style><p>This is some <b>bold</b> text.</p>";
NSAttributedString *text = [[NSAttributedString alloc] initWithData:[html dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
documentAttributes:nil
error:nil];
label2.attributedText = text;
} withName:@"attributed string from HTML"];
输出通过 NSLog
attributed string from HTML: 0.174494 sec
如果您跟踪返回的 NSTimeInterval
,您可以将它们传递给一个汇总您发现的结果的方法。
NSTimeInterval t1 = [XINBlockTimer timeBlock:^{ ... }];
NSTimeInterval t2 = [XINBlockTimer timeBlock:^{ ... }];
[XINBlockTimer compareTimeInterval:t1 withTimeInterval:t2];
示例输出
First interval is 100.520548x faster
Difference between intervals is insignificant: 0.000002
请查看附带的示例项目获取更多使用示例。
XINBlockTimer 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。