Skip to content

Instantly share code, notes, and snippets.

@samuraisam
Created September 30, 2013 03:55
Show Gist options
  • Select an option

  • Save samuraisam/6759203 to your computer and use it in GitHub Desktop.

Select an option

Save samuraisam/6759203 to your computer and use it in GitHub Desktop.
Testing out some different serialization libraries.
//
// main.m
// mdb-bench
//
// Created by Samuel Sutch on 9/29/13.
// Copyright (c) 2013 Steamboat Labs. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <JSONKit/JSONKit.h>
@interface Hello : NSObject <NSCoding>
@property (nonatomic) NSString *thestring;
@property (nonatomic) NSString *otherstring;
@property (nonatomic) NSDictionary *somedict;
@property (nonatomic) NSNumber *anumber;
- (NSDictionary *)toJSON;
- (id)initWithJSON:(NSDictionary *)dict;
@end
@implementation Hello
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.thestring forKey:@"thestring"];
[aCoder encodeObject:self.otherstring forKey:@"otherstring"];
[aCoder encodeObject:self.somedict forKey:@"somedict"];
[aCoder encodeObject:self.anumber forKey:@"anumber"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_thestring = [aDecoder decodeObjectForKey:@"thestring"];
_otherstring = [aDecoder decodeObjectForKey:@"otherstring"];
_somedict = [aDecoder decodeObjectForKey:@"somedict"];
_anumber = [aDecoder decodeObjectForKey:@"anumber"];
}
return self;
}
- (NSDictionary *)toJSON
{
NSMutableDictionary *d = [[NSMutableDictionary alloc] init];
d[@"thestring"] = self.thestring;
d[@"otherstring"] = self.otherstring;
d[@"somedict"] = self.somedict;
d[@"anumber"] = self.anumber;
return d;
}
- (id)initWithJSON:(NSDictionary *)dict
{
self = [self init];
if (self) {
_thestring = dict[@"thestring"];
_otherstring = dict[@"otherstring"];
_somedict = dict[@"somedict"];
_anumber = dict[@"anumber"];
}
return self;
}
@end
#define NUM_OBJECTS 50
#define NUM_ITERATIONS 1000
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *objs = [NSMutableArray array];
for (int i = 0; i < NUM_OBJECTS; i++) {
Hello *obj = [[Hello alloc] init];
obj.thestring = [NSString stringWithFormat:@"some string: %d", i];
obj.otherstring = [NSString stringWithFormat:@"other string: %d", i];
obj.somedict = @{@"somedict": @{@"somesubdict": @[@"some array value", @"other array value"]}};
obj.anumber = [NSNumber numberWithInt:i];
[objs addObject:obj];
}
NSMutableArray *datas = [NSMutableArray arrayWithCapacity:NUM_ITERATIONS * NUM_OBJECTS];
NSData *dat;
NSTimeInterval start, end;
{
start = [NSDate timeIntervalSinceReferenceDate];
for (int i = 0; i < NUM_ITERATIONS; i++) {
for (int j = 0; j < NUM_OBJECTS; j++) {
[datas addObject:(dat = [NSKeyedArchiver archivedDataWithRootObject:objs[j]])];
}
}
end = [NSDate timeIntervalSinceReferenceDate];
}
NSLog(@"NSKeyedArchiver encode %d objects %f", NUM_ITERATIONS * NUM_OBJECTS, end - start);
NSLog(@"NSKeyedArchiver data size %ld", (unsigned long)dat.length);
{
start = [NSDate timeIntervalSinceReferenceDate];
for (NSData *data in datas) {
[NSKeyedUnarchiver unarchiveObjectWithData:data];
}
end = [NSDate timeIntervalSinceReferenceDate];
}
NSLog(@"NSKeyedUnarchiver decode %ld objects %f", datas.count, end - start);
[datas removeAllObjects];
{
start = [NSDate timeIntervalSinceReferenceDate];
for (int i = 0; i < NUM_ITERATIONS; i++) {
for (int j = 0; j < NUM_OBJECTS; j++) {
[datas addObject:(dat = [[objs[j] toJSON] JSONData])];
}
}
end = [NSDate timeIntervalSinceReferenceDate];
}
NSLog(@"JSONKit encode %d objects %f", NUM_ITERATIONS * NUM_OBJECTS, end - start);
NSLog(@"JSONKit data size %ld", (unsigned long)dat.length);
{
start = [NSDate timeIntervalSinceReferenceDate];
for (NSData *data in datas) {
[data objectFromJSONData];
}
end = [NSDate timeIntervalSinceReferenceDate];
}
NSLog(@"JSONData decode %ld objects %f", datas.count, end - start);
}
return 0;
}
2013-09-29 21:47:15.425 mdb-bench[40114:303] NSKeyedArchiver encode 50000 objects 4.833274
2013-09-29 21:47:15.426 mdb-bench[40114:303] NSKeyedArchiver data size 571
2013-09-29 21:47:16.916 mdb-bench[40114:303] NSKeyedUnarchiver decode 50000 objects 1.489161
2013-09-29 21:47:17.299 mdb-bench[40114:303] JSONKit encode 50000 objects 0.375954
2013-09-29 21:47:17.300 mdb-bench[40114:303] JSONKit data size 158
2013-09-29 21:47:19.248 mdb-bench[40114:303] JSONData decode 50000 objects 1.947726
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment