summary refs log tree commit diff stats
path: root/libs/cocos2d/Support/CCProfiling.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/Support/CCProfiling.m')
-rwxr-xr-xlibs/cocos2d/Support/CCProfiling.m117
1 files changed, 117 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/CCProfiling.m b/libs/cocos2d/Support/CCProfiling.m new file mode 100755 index 0000000..13c8c81 --- /dev/null +++ b/libs/cocos2d/Support/CCProfiling.m
@@ -0,0 +1,117 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2010 Stuart Carnie
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26#import "../ccConfig.h"
27
28#if CC_ENABLE_PROFILERS
29
30#import "CCProfiling.h"
31
32@interface CCProfilingTimer()
33- (id)initWithName:(NSString*)timerName andInstance:(id)instance;
34@end
35
36@implementation CCProfiler
37
38static CCProfiler* g_sharedProfiler;
39
40+ (CCProfiler*)sharedProfiler {
41 if (!g_sharedProfiler)
42 g_sharedProfiler = [[CCProfiler alloc] init];
43
44 return g_sharedProfiler;
45}
46
47+ (CCProfilingTimer*)timerWithName:(NSString*)timerName andInstance:(id)instance {
48 CCProfiler* p = [CCProfiler sharedProfiler];
49 CCProfilingTimer* t = [[CCProfilingTimer alloc] initWithName:timerName andInstance:instance];
50 [p->activeTimers addObject:t];
51 [t release];
52 return t;
53}
54
55+ (void)releaseTimer:(CCProfilingTimer*)timer {
56 CCProfiler* p = [CCProfiler sharedProfiler];
57 [p->activeTimers removeObject:timer];
58}
59
60- (id)init {
61 if (!(self = [super init])) return nil;
62
63 activeTimers = [[NSMutableArray alloc] init];
64
65 return self;
66}
67
68- (void)dealloc {
69 [activeTimers release];
70 [super dealloc];
71}
72
73- (void)displayTimers {
74 for (id timer in activeTimers) {
75 printf("%s\n", [[timer description] cStringUsingEncoding:[NSString defaultCStringEncoding]]);
76 }
77}
78
79@end
80
81@implementation CCProfilingTimer
82
83- (id)initWithName:(NSString*)timerName andInstance:(id)instance {
84 if (!(self = [super init])) return nil;
85
86 name = [[NSString stringWithFormat:@"%@ (0x%.8x)", timerName, instance] retain];
87
88 return self;
89}
90
91- (void)dealloc {
92 [name release];
93 [super dealloc];
94}
95
96- (NSString*)description {
97 return [NSString stringWithFormat:@"%@ : avg time, %fms", name, averageTime];
98}
99
100void CCProfilingBeginTimingBlock(CCProfilingTimer* timer) {
101 gettimeofday(&timer->startTime, NULL);
102}
103
104typedef unsigned int uint32;
105void CCProfilingEndTimingBlock(CCProfilingTimer* timer) {
106 struct timeval currentTime;
107 gettimeofday(&currentTime, NULL);
108 timersub(&currentTime, &timer->startTime, &currentTime);
109 double duration = currentTime.tv_sec * 1000.0 + currentTime.tv_usec / 1000.0;
110
111 // return in milliseconds
112 timer->averageTime = (timer->averageTime + duration) / 2.0f;
113}
114
115@end
116
117#endif