Created
September 17, 2012 16:27
-
-
Save scelis/3738321 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'time' | |
def time_rand from = 0.0, to = Time.now | |
Time.at(from + rand * (to.to_f - from.to_f)) | |
end | |
num_iterations = 3000 | |
dates = [] | |
i = 0 | |
while i < num_iterations | |
d = time_rand | |
dates << d | |
i += 1 | |
end | |
File.open("iso8601.json", 'w:UTF-8') do |f| | |
f.puts "{" | |
f.puts ' "dates" : [' | |
i = 0 | |
while i < num_iterations | |
date = dates[i] | |
f.write " \"#{date.utc.iso8601}\"" | |
if i < num_iterations - 1 | |
f.write "," | |
end | |
f.write "\n" | |
i += 1 | |
end | |
f.puts " ]" | |
f.puts "}" | |
end | |
File.open("objects.json", 'w:UTF-8') do |f| | |
f.puts "{" | |
f.puts ' "dates" : [' | |
i = 0 | |
while i < num_iterations | |
date = dates[i] | |
f.puts " {" | |
f.write " \"timeEpochSeconds\" : " | |
f.write "#{date.to_i},\n" | |
f.write " \"timeZoneOffsetSeconds\" : -21600,\n" | |
f.write " \"timeZoneName\" : \"America/Chicago\",\n" | |
f.write " \"isDST\" : true\n" | |
f.write " }" | |
if i < num_iterations - 1 | |
f.write "," | |
end | |
f.write "\n" | |
i += 1 | |
end | |
f.puts " ]" | |
f.puts "}" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSDateFormatter Total Time: 760ms | |
NSDateFormatter per Iteration: 248832ns | |
NSDateFormatter JSON Parse Time: 10ms | |
NSDateFormatter Creation Time: 3ms | |
Objects Total Time: 281ms | |
Objects per Iteration: 11154ns | |
Objects JSON Parse Time: 248ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SCTimingViewController.m | |
// iOSTester | |
// | |
// Created by Sebastian Celis on 8/6/12. | |
// | |
#import <mach/mach.h> | |
#import <mach/mach_time.h> | |
#import "SCTimingViewController.h" | |
#define kNumIterations 3000 | |
#define kNanosecondsPerMillisecond 1000000 | |
#define kNanosecondsPerSecond 1000000000 | |
@implementation SCTimingViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self setTitle:@"Timing"]; | |
[[self view] setBackgroundColor:[UIColor whiteColor]]; | |
NSMutableString *str = [[NSMutableString alloc] init]; | |
// Get timebase information. | |
uint64_t start, end, elapsed, elapsedNanos; | |
mach_timebase_info_data_t timebase; | |
mach_timebase_info(&timebase); | |
// Date Parsing | |
NSMutableArray *dates1 = [NSMutableArray array]; | |
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"iso8601" ofType:@"json"]; | |
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; | |
start = mach_absolute_time(); | |
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; | |
uint64_t parse1 = mach_absolute_time(); | |
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; | |
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; | |
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"]; | |
uint64_t formatter1 = mach_absolute_time(); | |
for (NSString *dateString in [dict objectForKey:@"dates"]) | |
{ | |
[dates1 addObject:[formatter dateFromString:dateString]]; | |
} | |
end = mach_absolute_time(); | |
elapsed = end - start; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"NSDateFormatter Total Time: %lldms\n", elapsedNanos / kNanosecondsPerMillisecond]; | |
elapsed = end - formatter1; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"NSDateFormatter per Iteration: %lldns\n", elapsedNanos / kNumIterations]; | |
elapsed = parse1 - start; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"NSDateFormatter JSON Parse Time: %lldms\n", elapsedNanos / kNanosecondsPerMillisecond]; | |
elapsed = formatter1 - parse1; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"NSDateFormatter Creation Time: %lldms\n", elapsedNanos / kNanosecondsPerMillisecond]; | |
// Object Parsing | |
NSMutableArray *dates2 = [NSMutableArray array]; | |
NSMutableArray *zones2 = [NSMutableArray array]; | |
NSMutableArray *offsets2 = [NSMutableArray array]; | |
NSMutableArray *isDST2 = [NSMutableArray array]; | |
filePath = [[NSBundle mainBundle] pathForResource:@"objects" ofType:@"json"]; | |
data = [[NSData alloc] initWithContentsOfFile:filePath]; | |
start = mach_absolute_time(); | |
dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]; | |
uint64_t parse2 = mach_absolute_time(); | |
for (NSDictionary *dateDict in [dict objectForKey:@"dates"]) | |
{ | |
[dates2 addObject:[NSDate dateWithTimeIntervalSince1970:[[dateDict objectForKey:@"timeEpochSeconds"] doubleValue]]]; | |
[zones2 addObject:[dateDict objectForKey:@"timeZoneName"]]; | |
[offsets2 addObject:[dateDict objectForKey:@"timeZoneOffsetSeconds"]]; | |
[isDST2 addObject:[dateDict objectForKey:@"isDST"]]; | |
} | |
end = mach_absolute_time(); | |
elapsed = end - start; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"Objects Total Time: %lldms\n", elapsedNanos / kNanosecondsPerMillisecond]; | |
elapsed = end - parse2; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"Objects per Iteration: %lldns\n", elapsedNanos / kNumIterations]; | |
elapsed = parse2 - start; | |
elapsedNanos = (elapsed * (timebase.numer / timebase.denom)); | |
[str appendFormat:@"Objects JSON Parse Time: %lldms\n", elapsedNanos / kNanosecondsPerMillisecond]; | |
// Display results. | |
UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset([[self view] bounds], 10.0, 10.0)]; | |
[label setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; | |
[label setFont:[UIFont systemFontOfSize:12.0]]; | |
[label setNumberOfLines:0]; | |
[label setLineBreakMode:UILineBreakModeWordWrap]; | |
[label setText:str]; | |
[str release]; | |
[[self view] addSubview:label]; | |
[label release]; | |
NSLog(@"%d %d %d %d %d", [dates1 count], [dates2 count], [zones2 count], [offsets2 count], [isDST2 count]); | |
NSLog(@"%@", [label text]); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SCTimingViewController.h | |
// iOSTester | |
// | |
// Created by Sebastian Celis on 8/6/12. | |
// | |
#import <UIKit/UIKit.h> | |
@interface SCTimingViewController : UIViewController | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment