Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Commit

Permalink
Added YAJL and updated benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Jun 21, 2010
1 parent d9bb99a commit 18ec5f3
Show file tree
Hide file tree
Showing 27 changed files with 826 additions and 238 deletions.
92 changes: 61 additions & 31 deletions Classes/JBAppDelegate.m
Expand Up @@ -16,6 +16,9 @@
#import "NSString+SBJSON.h"
#import "NSObject+SBJSON.h"

// YAJL
#import "NSObject+YAJL.h"

// Apple JSON
#import "JSONParser.h"
#import "JSONWriter.h"
Expand All @@ -26,100 +29,127 @@ - (void)applicationDidFinishLaunching:(UIApplication *)application {

// Configuration
NSUInteger times = 100;
NSLog(@"Starting benchmarks with %i iterations for each library\n", times);
NSStringEncoding stringEncoding = NSUTF8StringEncoding;
NSStringEncoding dataEncoding = stringEncoding; //NSUTF32BigEndianStringEncoding;

NSLog(@"Loading JSON string");
// Load JSON string
NSString *jsonString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"twitter_public_timeline" ofType:@"json"] encoding:stringEncoding error:nil];
NSData *jsonData = [jsonString dataUsingEncoding:dataEncoding];
NSArray *array = (NSArray *)[[CJSONDeserializer deserializer] deserialize:jsonData error:nil];
NSUInteger x = 0;

NSLog(@"*** Parsing with TouchJSON");
NSTimeInterval touchJSONParseTotal = 0.0;
// Read with TouchJSON
NSTimeInterval touchJSONReadTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
id object = [[CJSONDeserializer deserializer] deserialize:jsonData error:nil];
NSDate *after = [NSDate date];
NSTimeInterval time = [after timeIntervalSinceDate:before];
touchJSONReadTotal += time;
[object description]; // Eliminate warning
[pool release];
}
NSLog(@"TouchJSON average read time: %f", (touchJSONReadTotal / times));

// Write with TouchJSON
NSTimeInterval touchJSONWriteTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
NSString *writtenString = [[CJSONSerializer serializer] serializeArray:array];
NSDate *after = [NSDate date];
NSTimeInterval time = [after timeIntervalSinceDate:before];
touchJSONParseTotal += time;
touchJSONWriteTotal += time;
[writtenString description]; // Eliminate warning
[pool release];
}
NSLog(@" * TouchJSON average parse time: %f", (touchJSONParseTotal / times));
NSLog(@"TouchJSON average write time: %f", (touchJSONWriteTotal / times));

NSLog(@"*** Parsing with JSON Framework");
NSTimeInterval jsonFrameworkParseTotal = 0.0;
// Read with JSON Framework
NSTimeInterval jsonFrameworkReadTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
id object = [jsonString JSONValue];
NSDate *after = [NSDate date];
[object description]; // Eliminate warning
NSTimeInterval time = [after timeIntervalSinceDate:before];
jsonFrameworkParseTotal += time;
jsonFrameworkReadTotal += time;
[object description]; // Eliminate warning
[pool release];
}
NSLog(@" * JSON Framework average parse time: %f", (jsonFrameworkParseTotal / times));
NSLog(@"JSON Framework average read time: %f", (jsonFrameworkReadTotal / times));

NSLog(@"*** Parsing with Apple JSON");
NSTimeInterval appleJSONParseTotal = 0.0;
// Write with JSON Framework
NSTimeInterval jsonFrameworkWriteTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
id object = [JSON objectWithData:jsonData options:0 error:nil];
NSString *writtenString = [array JSONRepresentation];
NSDate *after = [NSDate date];
[object description]; // Eliminate warning
NSTimeInterval time = [after timeIntervalSinceDate:before];
appleJSONParseTotal += time;
jsonFrameworkWriteTotal += time;
[writtenString description]; // Eliminate warning
[pool release];
}
NSLog(@" * Apple JSON average parse time: %f", (appleJSONParseTotal / times));
NSLog(@"JSON Framework average write time: %f", (jsonFrameworkWriteTotal / times));

NSLog(@"*** Writing with TouchJSON");
NSTimeInterval touchJSONWriteTotal = 0.0;
// Read with YAJL
NSTimeInterval yajlReadTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
NSString *writtenString = [[CJSONSerializer serializer] serializeArray:array];
id object = [jsonString yajl_JSON];
NSDate *after = [NSDate date];
[writtenString description]; // Eliminate warning
NSTimeInterval time = [after timeIntervalSinceDate:before];
touchJSONWriteTotal += time;
yajlReadTotal += time;
[object description]; // Eliminate warning
[pool release];
}
NSLog(@" * TouchJSON average write time: %f", (touchJSONWriteTotal / times));
NSLog(@"YAJL average read time: %f", (yajlReadTotal / times));

NSLog(@"*** Parsing with JSON Framework");
NSTimeInterval jsonFrameworkWriteTotal = 0.0;
NSTimeInterval yajlWriteTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
NSString *writtenString = [array JSONRepresentation];
NSString *writtenString = [array yajl_JSONString];
NSDate *after = [NSDate date];
NSTimeInterval time = [after timeIntervalSinceDate:before];
yajlWriteTotal += time;
[writtenString description]; // Eliminate warning
[pool release];
}
NSLog(@"YAJL average write time: %f", (yajlWriteTotal / times));

NSTimeInterval appleJSONReadTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
id object = [JSON objectWithData:jsonData options:0 error:nil];
NSDate *after = [NSDate date];
NSTimeInterval time = [after timeIntervalSinceDate:before];
jsonFrameworkWriteTotal += time;
appleJSONReadTotal += time;
[object description]; // Eliminate warning
[pool release];
}
NSLog(@" * JSON Framework average write time: %f", (jsonFrameworkWriteTotal / times));
NSLog(@"Apple JSON average read time: %f", (appleJSONReadTotal / times));

NSLog(@"*** Parsing with Apple JSON");
NSTimeInterval appleJSONWriteTotal = 0.0;
for (x = 0; x < times; x++) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate *before = [NSDate date];
NSString *writtenString = [JSON stringWithObject:array options:0 error:nil];
NSDate *after = [NSDate date];
[writtenString description]; // Eliminate warning
NSTimeInterval time = [after timeIntervalSinceDate:before];
appleJSONWriteTotal += time;
[writtenString description]; // Eliminate warning
[pool release];
}
NSLog(@" * Apple JSON average write time: %f", (appleJSONWriteTotal / times));
NSLog(@"Apple JSON average write time: %f", (appleJSONWriteTotal / times));

NSLog(@"*** DONE");
NSLog(@"Done. Quitting...");
abort();
}

@end
50 changes: 50 additions & 0 deletions Classes/Vendor/JSON Framework/JSON.h
@@ -0,0 +1,50 @@
/*
Copyright (C) 2009 Stig Brautaset. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
@mainpage A strict JSON parser and generator for Objective-C
JSON (JavaScript Object Notation) is a lightweight data-interchange
format. This framework provides two apis for parsing and generating
JSON. One standard object-based and a higher level api consisting of
categories added to existing Objective-C classes.
Learn more on the http://code.google.com/p/json-framework project site.
This framework does its best to be as strict as possible, both in what it
accepts and what it generates. For example, it does not support trailing commas
in arrays or objects. Nor does it support embedded comments, or
anything else not in the JSON specification. This is considered a feature.
*/

#import "SBJSON.h"
#import "NSObject+SBJSON.h"
#import "NSString+SBJSON.h"

4 changes: 2 additions & 2 deletions Classes/Vendor/JSON Framework/SBJsonParser.m
Expand Up @@ -58,8 +58,8 @@ @implementation SBJsonParser

static char ctrl[0x22];

+ (void)initialize
{

+ (void)initialize {
ctrl[0] = '\"';
ctrl[1] = '\\';
for (int i = 1; i < 0x20; i++)
Expand Down
21 changes: 15 additions & 6 deletions Classes/Vendor/JSON Framework/SBJsonWriter.m
Expand Up @@ -42,6 +42,14 @@ - (NSString*)indent;

@implementation SBJsonWriter

static NSMutableCharacterSet *kEscapeChars;

+ (void)initialize {
kEscapeChars = [[NSMutableCharacterSet characterSetWithRange: NSMakeRange(0,32)] retain];
[kEscapeChars addCharactersInString: @"\"\\"];
}


@synthesize sortKeys;
@synthesize humanReadable;

Expand All @@ -66,6 +74,13 @@ - (NSString*)stringWithObject:(id)value {
if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
return [self stringWithFragment:value];
}

if ([value respondsToSelector:@selector(proxyForJson)]) {
NSString *tmp = [self stringWithObject:[value proxyForJson]];
if (tmp)
return tmp;
}


[self clearErrorTrace];
[self addErrorWithCode:EFRAGMENT description:@"Not valid type for JSON"];
Expand Down Expand Up @@ -183,12 +198,6 @@ - (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json {

- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json {

static NSMutableCharacterSet *kEscapeChars;
if( ! kEscapeChars ) {
kEscapeChars = [[NSMutableCharacterSet characterSetWithRange: NSMakeRange(0,32)] retain];
[kEscapeChars addCharactersInString: @"\"\\"];
}

[json appendString:@"\""];

NSRange esc = [fragment rangeOfCharacterFromSet:kEscapeChars];
Expand Down
4 changes: 2 additions & 2 deletions Classes/Vendor/TouchJSON/CDataScanner.h
@@ -1,9 +1,9 @@
//
// CDataScanner.h
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 04/16/08.
// Copyright (c) 2008 Jonathan Wight
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
4 changes: 2 additions & 2 deletions Classes/Vendor/TouchJSON/CDataScanner.m
@@ -1,9 +1,9 @@
//
// CDataScanner.m
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 04/16/08.
// Copyright (c) 2008 Jonathan Wight
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// CDataScanner_Extensions.h
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSScanner_Extensions.m
// TouchJSON
// CDataScanner_Extensions.m
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSCharacterSet_Extensions.h
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSCharacterSet_Extensions.m
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSDictionary_JSONExtensions.h
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 04/17/08.
// Copyright (c) 2008 Jonathan Wight
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSDictionary_JSONExtensions.m
// TouchJSON
// TouchCode
//
// Created by Jonathan Wight on 04/17/08.
// Copyright (c) 2008 Jonathan Wight
// Copyright 2008 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSScanner_Extensions.h
// CocoaJSON
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down
@@ -1,9 +1,9 @@
//
// NSScanner_Extensions.m
// CocoaJSON
// TouchCode
//
// Created by Jonathan Wight on 12/08/2005.
// Copyright (c) 2005 Jonathan Wight
// Copyright 2005 toxicsoftware.com. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
Expand Down

0 comments on commit 18ec5f3

Please sign in to comment.