|
| 1 | +// |
| 2 | +// AppDelegate.m |
| 3 | +// BMTimeCalculateDemo |
| 4 | +// |
| 5 | +// Created by skyming on 16/5/7. |
| 6 | +// Copyright © 2016年 skyming. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "AppDelegate.h" |
| 10 | +#import "BMTimeCalculate.h" |
| 11 | + |
| 12 | +#define LOOPAGE 10000 |
| 13 | + |
| 14 | +@interface AppDelegate () |
| 15 | +{ |
| 16 | + NSMutableArray *test; |
| 17 | + double totalTime; |
| 18 | + double totalTimex; |
| 19 | +} |
| 20 | +@end |
| 21 | + |
| 22 | +@implementation AppDelegate |
| 23 | + |
| 24 | + |
| 25 | +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
| 26 | + // Override point for customization after application launch. |
| 27 | + |
| 28 | + CFTimeInterval start = CACurrentMediaTime(); |
| 29 | + // dosomething |
| 30 | + [NSThread sleepForTimeInterval:3.0f]; |
| 31 | + CFTimeInterval end = CACurrentMediaTime(); |
| 32 | + NSLog(@"时间损耗 = %f", end - start); |
| 33 | + |
| 34 | + clock_t startx = clock(); |
| 35 | + [NSThread sleepForTimeInterval:3.0f]; |
| 36 | + clock_t endx = clock(); |
| 37 | + NSLog(@"时间损耗 = %f", (double)(endx - startx)/CLOCKS_PER_SEC); |
| 38 | + |
| 39 | + test = [NSMutableArray array]; |
| 40 | + for (int i= 0; i < 100000000; i++) { |
| 41 | + [test addObject:@(i)]; |
| 42 | + } |
| 43 | + totalTime = 0.0f; |
| 44 | + totalTimex = 0.0f; |
| 45 | + |
| 46 | + // 同步方法测试 |
| 47 | +// [self synTest1]; |
| 48 | +// [self synTest2]; |
| 49 | + for (int i = 0; i < 10; i ++) { |
| 50 | + [self synTest3]; |
| 51 | + } |
| 52 | + NSLog(@"\n"); |
| 53 | + NSLog(@"ForLoop Time-Block: %.3f ms", totalTime/10.0f); |
| 54 | + NSLog(@"Enumeration Time-Block: %.3f ms\n", totalTimex/10.0f); |
| 55 | + NSLog(@"\n"); |
| 56 | + |
| 57 | +// NSLog(@"4 start"); |
| 58 | +// [self syntest4]; |
| 59 | + |
| 60 | + totalTime = 0.0f; |
| 61 | + totalTimex = 0.0f; |
| 62 | + |
| 63 | + for (int i = 0; i < 10; i ++) { |
| 64 | + [self syntest5]; |
| 65 | + } |
| 66 | + NSLog(@"\n"); |
| 67 | + NSLog(@"ForLoop Time-Model: %.3f ms", totalTime/10.0f); |
| 68 | + NSLog(@"Enumeration Time-Model: %.3f ms\n", totalTimex/10.0f); |
| 69 | + NSLog(@"\n"); |
| 70 | + // 异步方法测试 |
| 71 | + BMTimeCalculateModel *model = [BMTimeCalculateModel initWithTitle:@"动画操作"]; |
| 72 | + // 开始计时: |
| 73 | + [BMTimeCalculate startCalculate:model]; |
| 74 | + [UIView animateWithDuration:1.0f animations:^{ |
| 75 | + //结束计时: |
| 76 | + [BMTimeCalculate endCalculate:model]; |
| 77 | + }]; |
| 78 | + |
| 79 | + return YES; |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +- (void)synTest1 { |
| 84 | + double time; |
| 85 | + |
| 86 | + // A |
| 87 | + time = bmOperateConsumeCalculate(^{ |
| 88 | + NSLog(@"Block: %@", [NSThread currentThread]); |
| 89 | + for (int i = 0; i < LOOPAGE; i++) { |
| 90 | + NSLog(@"TestX %d", i); |
| 91 | + } |
| 92 | + }); |
| 93 | + NSLog(@"循环耗时Block: %.3f ms", time); |
| 94 | + |
| 95 | + BMTimeCalculateModel *model = [BMTimeCalculateModel initWithTitle:@"循环耗时Model"]; |
| 96 | + // 开始计时: |
| 97 | + [BMTimeCalculate startCalculate:model]; |
| 98 | + NSLog(@"Block: %@", [NSThread currentThread]); |
| 99 | + for (int i = 0; i < LOOPAGE; i++) { |
| 100 | + NSLog(@"TestX %d", i); |
| 101 | + } |
| 102 | + [BMTimeCalculate endCalculate:model]; |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +- (void)synTest2 { |
| 107 | + __block int i, j, count; |
| 108 | + double current = 0.0; |
| 109 | + __block double answer = 0.0; |
| 110 | + double elapsed = 0.0; |
| 111 | + int dim1 = 256; |
| 112 | + int dim2 = 256; |
| 113 | + int size = 4*dim1*dim2; |
| 114 | + |
| 115 | + //Allocatesome memory and warm it up |
| 116 | + double *array =(double*)malloc(size*sizeof(double)); |
| 117 | + for(i=0;i<size;i++) |
| 118 | + { |
| 119 | + array[i] = (double)i; |
| 120 | + } |
| 121 | + |
| 122 | + count = 5; |
| 123 | + for(i = 0; i < count; i++) |
| 124 | + { |
| 125 | + current = bmOperateConsumeCalculate(^{ |
| 126 | + //dosome work |
| 127 | + for(j=0;j< size; j++) |
| 128 | + { |
| 129 | + answer += sqrt(array[j]); |
| 130 | + } |
| 131 | + }); |
| 132 | + printf("Timefor iteration: %.3f ms for answer: %f\n",current, answer); |
| 133 | + elapsed+= current; |
| 134 | + } |
| 135 | + |
| 136 | + printf("\nTotaltime in seconds = %.3f for answer: %f\n",elapsed/count,answer); |
| 137 | + free(array); |
| 138 | +} |
| 139 | + |
| 140 | +- (void)synTest3 { |
| 141 | + |
| 142 | + NSLog(@"3 start"); |
| 143 | + |
| 144 | + double start = CFAbsoluteTimeGetCurrent(); |
| 145 | + //You code here... |
| 146 | + __block int sum = 0; |
| 147 | + double time = bmOperateConsumeCalculate(^{ |
| 148 | + for (int i = 0;i < test.count; i++) { |
| 149 | + int key = [test[i] intValue]; |
| 150 | + sum += key; |
| 151 | + sum -= key; |
| 152 | + } |
| 153 | + }); |
| 154 | + |
| 155 | + totalTime += time; |
| 156 | + double end = CFAbsoluteTimeGetCurrent(); |
| 157 | + NSLog(@"cost time = %@", @((end - start))); |
| 158 | +// NSDate* tmpStartData = [NSDate date]; |
| 159 | +// //You code here... |
| 160 | +// double deltaTime = [[NSDate date] timeIntervalSinceDate:tmpStartData]; |
| 161 | +// NSLog(@"cost time = %f s", deltaTime); |
| 162 | +// clock_t start = clock(); |
| 163 | +// time_t startx = time(&startx) |
| 164 | + NSLog(@"ForLoop Time-Block: %.3f ms", time); |
| 165 | + |
| 166 | + time = bmOperateConsumeCalculate(^{ |
| 167 | + for (id obj in test) { |
| 168 | + int key = [obj intValue]; |
| 169 | + sum += key; |
| 170 | + sum -= key; |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + |
| 175 | + totalTimex += time; |
| 176 | + |
| 177 | + NSLog(@"Enumeration Time-Block: %.3f ms", time); |
| 178 | +} |
| 179 | + |
| 180 | +- (void)syntest4 { |
| 181 | + int sum = 0; |
| 182 | + |
| 183 | + double date_s = CACurrentMediaTime() * 1000; |
| 184 | + for (int i = 0;i < test.count; i++) { |
| 185 | + sum += 1; |
| 186 | + } |
| 187 | + double date_e = CACurrentMediaTime() * 1000; |
| 188 | + NSLog(@"ForLoop Time-CAMedia: %.3f ms", date_e - date_s); |
| 189 | + |
| 190 | + date_s = CACurrentMediaTime() * 1000; |
| 191 | + for (id obj in test) { |
| 192 | + sum += 1; |
| 193 | + } |
| 194 | + date_e = CACurrentMediaTime() * 1000; |
| 195 | + NSLog(@"Enumeration Time-CAMedia: %.3f ms", date_e - date_s); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +- (void)syntest5 { |
| 200 | + |
| 201 | + int sum = 0; |
| 202 | + |
| 203 | + NSLog(@"5 start"); |
| 204 | + |
| 205 | + BMTimeCalculateModel *model = [BMTimeCalculateModel initWithTitle:@"ForLoop Time-Model"]; |
| 206 | + // 开始计时: |
| 207 | + [BMTimeCalculate startCalculate:model]; |
| 208 | + for (int i = 0;i < test.count; i++) { |
| 209 | + int key = [test[i] intValue]; |
| 210 | + sum += key; |
| 211 | + sum -= key; |
| 212 | + } |
| 213 | + //结束计时: |
| 214 | + [BMTimeCalculate endCalculate:model]; |
| 215 | + |
| 216 | + totalTime += model.elapsedTime; |
| 217 | + |
| 218 | + NSLog(@"ForLoop Time-Model: %.3f ms", model.elapsedTime); |
| 219 | + |
| 220 | + model = [BMTimeCalculateModel initWithTitle:@"Enumeration Time-Model"]; |
| 221 | + // 开始计时: |
| 222 | + [BMTimeCalculate startCalculate:model]; |
| 223 | + for (id obj in test) { |
| 224 | + int key = [obj intValue]; |
| 225 | + sum += key; |
| 226 | + sum -= key; |
| 227 | + } |
| 228 | + //结束计时: |
| 229 | + [BMTimeCalculate endCalculate:model]; |
| 230 | + totalTimex += model.elapsedTime; |
| 231 | + |
| 232 | + NSLog(@"Enumeration Time-Model: %.3f ms", model.elapsedTime); |
| 233 | + |
| 234 | + |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | + |
| 240 | +- (void)applicationWillResignActive:(UIApplication *)application { |
| 241 | + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. |
| 242 | + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. |
| 243 | +} |
| 244 | + |
| 245 | +- (void)applicationDidEnterBackground:(UIApplication *)application { |
| 246 | + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. |
| 247 | + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. |
| 248 | +} |
| 249 | + |
| 250 | +- (void)applicationWillEnterForeground:(UIApplication *)application { |
| 251 | + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. |
| 252 | +} |
| 253 | + |
| 254 | +- (void)applicationDidBecomeActive:(UIApplication *)application { |
| 255 | + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. |
| 256 | +} |
| 257 | + |
| 258 | +- (void)applicationWillTerminate:(UIApplication *)application { |
| 259 | + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. |
| 260 | +} |
| 261 | + |
| 262 | +@end |
0 commit comments