4 ms·
I kinda sorta suspect he was using ruby-json, which is substantially slower at JSON serialization than something like Oj. It's not going to make the Rails imple
by cheald 13y ago
I kinda sorta suspect he was using ruby-json, which is substantially slower at JSON serialization than something like Oj. It's not going to make the Rails implementation start seriously competing with Go, but it would help matters substantially.
require 'benchmark'
require 'json'
require 'oj'
require 'open-uri'
TIMES = 100000
hash = JSON.parse(open("https://gist.github.com/gnip/764239/raw/6c6a2297f3e4e29a626f07db0c57b45af7d7e5d7/Twitter+%28json+format%29.js").read)
Benchmark.bm do |x|
x.report("Oj") { TIMES.times { Oj.dump(hash) } }
x.report("ruby-json") { TIMES.times { JSON.dump(hash) } }
end
# user system total real
# Oj 0.980000 0.000000 0.980000 ( 0.983426)
# ruby-json 8.780000 0.000000 8.780000 ( 8.832761)
Edit: I'm working on a Go implementation of the above benchmark. I haven't touched Go before today, though, so it's a bit of learning. :)
Edit edit: Here's the benchmark in Go. First time I've touched it, tell me if I stuffed something up awfully. Amusingly, it ended up being almost exactly 10x many lines as the Ruby code. :)
https://gist.github.com/cheald/5784471 https://gist.github.com/cheald/5784471
BenchmarkJsonEncode 50000 53661 ns/op
So the final results:
Implementation ns/op
------------------------
Ruby (ruby-json) 88,327
Ruby (via Oj) 9,834
Go 53,661
I'm not quite sure that feels right, so if someone else could sanity check me, please do.