Created
July 5, 2011 05:11
-
-
Save ferrous26/1064281 to your computer and use it in GitHub Desktop.
preliminary benchmarks for json on MacRuby
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
| require 'rubygems' | |
| gem 'minitest', '>= 2.3.1' | |
| require 'minitest/autorun' | |
| require 'minitest/benchmark' | |
| require 'json' | |
| framework 'Foundation' | |
| class Bench < MiniTest::Unit::TestCase | |
| def self.order | |
| :alpha | |
| end | |
| def self.bench_range | |
| bench_exp 1_000, 100_000 | |
| end | |
| def json val | |
| assert_performance_linear do |n| | |
| n.times { val.to_json } | |
| end | |
| end | |
| # @todo varying lengths of strings, not important now because it does | |
| # not stress the part of the code that is responsible for the | |
| # slow down | |
| def bench_string | |
| json ['blah'] | |
| end | |
| def bench_nsstring | |
| json [NSString.stringWithString('blah')] | |
| end | |
| def bench_number | |
| json [100] | |
| end | |
| def bench_hash | |
| # hash = { this:{is:{a:{test:{}}}}, life:{universe:42}, pie:'cake', hash:{with:1,few:2,keys:3} } | |
| json a: {}, b: {} | |
| end | |
| def bench_nsdict | |
| x = NSDictionary | |
| def NSDictionary.y *args | |
| dictionaryWithObjectsAndKeys *args.reverse, nil | |
| end | |
| # hash = x.y('this', x.y('a', x.y('test', x.y())), 'life', x.y('universe',42), 'pie', 'cake', 'hash', x.y('with',1,'few',2,'keys',3)) | |
| json x.y('a',x.y, 'b',x.y) | |
| end | |
| def bench_array | |
| # array = [[[[[[]]]]], [[]], [[[[],[]],[[],[],[]],[]]], [[],[]], [[[],[]]]] | |
| json [[1],[2]] | |
| end | |
| def bench_nsarray | |
| x = NSArray | |
| def x.y *args | |
| arrayWithObjects *args, nil | |
| end | |
| # array = x.y x.y(x.y(x.y(x.y(x.y())))), x.y(x.y), x.y(x.y(x.y(x.y,x.y),x.y(x.y,x.y,x.y), x.y)), x.y(x.y,x.y), x.y(x.y(x.y,x.y)) | |
| json x.y(x.y(1), x.y(2)) | |
| end | |
| def bench_nil | |
| json [nil] | |
| end | |
| def bench_boolean | |
| json [true, false] | |
| end | |
| def bench_bignum | |
| json [1_000_000_000_000_000_000_000_000_000_000_000_000_000] | |
| end | |
| def bench_float | |
| json [2.71828183] | |
| end | |
| def bench_object | |
| json [Object.new] | |
| end | |
| def bench_smorgasbord | |
| json can: [{you: 'take', me: 'to', funky: 'town?'}, 1, 2, 3000, Object.new, true, -3.1415, nil] | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is from applying appropriate patches to the upstream JSON repository:
Bench 1000 10000 100000 bench_array 0.009178 0.068889 0.576111 bench_nsarray 0.006231 0.071549 0.659718 bench_bignum 0.013563 0.143416 1.418103 bench_boolean 0.005108 0.065971 0.591222 bench_float 0.008823 0.121051 1.025400 bench_hash 0.032613 0.262340 2.698372 bench_nsdict 0.067821 0.338544 3.332269 bench_nil 0.005587 0.066914 0.592831 bench_number 0.005283 0.068908 0.547478 bench_object 0.042540 0.205396 2.025258 bench_smorgasbord 0.077420 0.754987 7.198482 bench_string 0.013055 0.124828 1.179611 bench_nsstring 0.013120 0.150757 1.456123 Finished benchmarks in 26.300107s, 0.4943 tests/s, 0.4943 assertions/s.