Skip to content

Instantly share code, notes, and snippets.

@takuma104
Created March 5, 2010 22:54
Show Gist options
  • Save takuma104/323272 to your computer and use it in GitHub Desktop.
Save takuma104/323272 to your computer and use it in GitHub Desktop.
/*
PerfCheck: Stopwatch for Debugging (ObjC)
Usage:
PFBEGIN(@"hoge");
// some process
PFEND(@"hoge");
PFBEGIN(@"fuga");
// some process
PFEND(@"fuga");
PFBEGIN(@"hoge");
// some process
PFEND(@"hoge");
// when app quitting (UIApplicationDelegate#applicationWillTerminate)
PFDUMP;
// ->
// 2010-03-06 07:47:14.386 PerformanceChecker[1851:207] [PerfCheck] hoge in total 8.7ms
// 2010-03-06 07:47:14.401 PerformanceChecker[1851:207] [PerfCheck] fuga in total 9.0ms
*/
////////////// PerfCheck.h
#import <Foundation/Foundation.h>
void PerfCheckStopwatchStart(NSString *name);
void PerfCheckStopwatchEnd(NSString *name);
void PerfCheckDump();
#ifdef PERFCHECK_ENABLE
#define PFBEGIN(x) PerfCheckStopwatchStart(x)
#define PFEND(x) PerfCheckStopwatchEnd(x)
#define PFDUMP PerfCheckDump()
#else
#define PFBEGIN(x) (0)
#define PFEND(x) (0)
#define PFDUMP (0)
#endif
////////////// PerfCheck.m
#import "PerfCheck.h"
static NSMutableDictionary *totalDict = nil;
static NSMutableDictionary *runningDict = nil;
void PerfCheckStopwatchStart(NSString *name) {
if (runningDict == nil) {
runningDict = [[NSMutableDictionary alloc] init];
}
[runningDict setObject:[NSNumber numberWithDouble:CFAbsoluteTimeGetCurrent()]
forKey:name];
}
void PerfCheckStopwatchEnd(NSString *name) {
double now = CFAbsoluteTimeGetCurrent();
double before = [[runningDict valueForKey:name] doubleValue];
if (totalDict == nil) {
totalDict = [[NSMutableDictionary alloc] init];
}
double total = [[totalDict valueForKey:name] doubleValue];
total += now - before;
[totalDict setObject:[NSNumber numberWithDouble:total]
forKey:name];
}
void PerfCheckDump() {
NSArray *keys = [[totalDict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (NSString *k in keys) {
NSLog(@"[PerfCheck] %@ in total %.1fms",
k, [[totalDict valueForKey:k] doubleValue] * 1000.0);
}
[runningDict release];
runningDict = nil;
[totalDict release];
totalDict = nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment