├── .gitignore ├── DKBenchmark.h ├── DKBenchmark.m ├── DKBenchmark.vendorspec └── Readme.markdown /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | *.pbxuser 3 | *.mode1v3 4 | .DS_Store 5 | pkg 6 | *.xcworkspace 7 | xcuserdata 8 | contents.xcworkspacedata 9 | -------------------------------------------------------------------------------- /DKBenchmark.h: -------------------------------------------------------------------------------- 1 | // 2 | // DKBenchmark.h 3 | // DiscoKit 4 | // 5 | // Created by Keith Pitt on 30/06/11. 6 | // Copyright 2011 Mostly Disco. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef void (^DKBenchmarkBlock)(void); 12 | 13 | @interface DKBenchmark : NSObject 14 | 15 | + (void)benchmark:(NSString *)name block:(DKBenchmarkBlock)block; 16 | + (void)benchmark:(NSString *)name iterations:(NSInteger)iterations block:(DKBenchmarkBlock)block; 17 | 18 | @end -------------------------------------------------------------------------------- /DKBenchmark.m: -------------------------------------------------------------------------------- 1 | // 2 | // DKBenchmark.m 3 | // DiscoKit 4 | // 5 | // Created by Keith Pitt on 30/06/11. 6 | // Copyright 2011 Mostly Disco. All rights reserved. 7 | // 8 | 9 | #import "DKBenchmark.h" 10 | 11 | @implementation DKBenchmark 12 | 13 | + (void)benchmark:(NSString *)name block:(DKBenchmarkBlock)block { 14 | 15 | // If you don't specify an iterations loop, lets just default 16 | // it to 100. 17 | [self benchmark:name iterations:100 block:block]; 18 | 19 | } 20 | 21 | + (void)benchmark:(NSString *)name iterations:(NSInteger)iterations block:(DKBenchmarkBlock)block { 22 | 23 | // Store the results of the iteration in this array 24 | NSMutableArray * results = [[NSMutableArray alloc] init]; 25 | 26 | // Define variables that we're going to use in the loop. 27 | NSDate * date; 28 | NSTimeInterval timePassed; 29 | 30 | for (NSInteger i = 0; i < iterations; i++) { 31 | 32 | // Grab the current date 33 | date = [[NSDate alloc] init]; 34 | 35 | // Run the block 36 | block(); 37 | 38 | // Calcualte the time that has passed, and convert to a 39 | // signed integer by * -1 40 | timePassed = [date timeIntervalSinceNow] * -1; 41 | 42 | // Release the date 43 | [date release], date = nil; 44 | 45 | // Add the result 46 | [results addObject:[NSNumber numberWithDouble:timePassed]]; 47 | 48 | } 49 | 50 | // Start at 0 51 | NSDecimalNumber * average = [NSDecimalNumber decimalNumberWithDecimal: 52 | [[NSNumber numberWithInt:0] decimalValue]]; 53 | 54 | // Add all our results together 55 | for (NSNumber * timePassed in results) { 56 | average = [average decimalNumberByAdding: 57 | [NSDecimalNumber decimalNumberWithDecimal: 58 | [timePassed decimalValue]]]; 59 | } 60 | 61 | // Divide them by the total 62 | average = [average decimalNumberByDividingBy: 63 | [NSDecimalNumber decimalNumberWithDecimal: 64 | [[NSNumber numberWithInt:iterations] decimalValue]]]; 65 | 66 | // Show the results 67 | NSLog(@"[DKBenchmark] \"%@\" took %@ seconds on average with (%i iterations)", name, average, iterations); 68 | 69 | [results release]; 70 | 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /DKBenchmark.vendorspec: -------------------------------------------------------------------------------- 1 | Vendor::Spec.new do |s| 2 | 3 | s.name = "DKBenchmark" 4 | s.version = "0.1" 5 | 6 | s.authors = "keithpitt" 7 | s.email = "me@keithpitt.com" 8 | s.description = "Easy benchmarking in Objective-C using blocks" 9 | 10 | s.source = "https://github.com/keithpitt/DKBenchmark" 11 | 12 | s.files = `git ls-files`.split("\n") 13 | 14 | end 15 | -------------------------------------------------------------------------------- /Readme.markdown: -------------------------------------------------------------------------------- 1 | # DKBenchmark 2 | 3 | `DKBenchmark` is a utility class that makes it easy to benchmark code in Objective-C using blocks. 4 | Results are logged via `NSLog` 5 | 6 | It is used in the apps written by [Mostly Disco](http://www.mostlydisco.com) and [The Frontier Group](http://www.thefrontiergroup.com.au) 7 | 8 | ## Installation 9 | 10 | Copy the files into to your project folder, and add them to your Xcode project. 11 | 12 | ## Usage 13 | 14 | To benchmark some code (with 100 iterations) 15 | 16 | ```objective-c 17 | #import "DKBenchmark.h" 18 | 19 | NSString * someString = @"Greatest String Ever..."; 20 | 21 | [DKBenchmark benchmark:@"Appending Strings" block:^{ 22 | someString = [someString stringByAppendingString:@"...or is it?"]; 23 | }]; 24 | ``` 25 | 26 | If you want to specifiy the number of iterations: 27 | 28 | ```objective-c 29 | NSString * someString = @"It's over 9"; 30 | 31 | [DKBenchmark benchmark:@"Appending Strings" iterations: 9001 block:^{ 32 | someString = [someString stringByAppendingString:@"000"]; 33 | }]; 34 | ``` 35 | 36 | Running a benchmark will yield an output similar to the following: 37 | 38 | 2011-08-18 14:33:31.483 YourApplication[3544:10707] [DKBenchmark] "Appending Strings" took 0.25 seconds on average with (100 iterations) 39 | 40 | ## How does it work? 41 | 42 | It runs the code in the block you provide x times (default is 100). It 43 | records how long each iteration of the block takes, and at the end, gives 44 | you a number that represents the average time for all the iterations. 45 | 46 | ## Notes on benchmarking code 47 | 48 | If you are writing an application for iPhone, be sure to test your code 49 | on the actual device. Using the iPhone Simulator does not give you true 50 | results as it uses your computers CPU to do the calculations. iOS 51 | devices have a far less powerfull CPU. 52 | 53 | ## Note on Patches/Pull Requests 54 | 55 | * Fork the project. 56 | * Make your feature addition or bug fix. 57 | * Send me a pull request. Bonus points for topic branches. 58 | 59 | ## Contributers 60 | 61 | * [Keith Pitt](http://www.keithpitt.com) 62 | * [The Frontier Group](http://www.thefrontiergroup.com.au) 63 | * [Mostly Disco](http://www.mostlydisco.com) 64 | 65 | ## License 66 | 67 | DKBenchmark is licensed under the MIT License: 68 | 69 | Copyright (c) 2011 Keith Pitt (http://www.keithpitt.com/) 70 | 71 | Permission is hereby granted, free of charge, to any person obtaining a copy 72 | of this software and associated documentation files (the "Software"), to deal 73 | in the Software without restriction, including without limitation the rights 74 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 75 | copies of the Software, and to permit persons to whom the Software is 76 | furnished to do so, subject to the following conditions: 77 | 78 | The above copyright notice and this permission notice shall be included in 79 | all copies or substantial portions of the Software. 80 | 81 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 82 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 83 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 84 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 85 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 86 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 87 | THE SOFTWARE. 88 | --------------------------------------------------------------------------------