Project

General

Profile

« Previous | Next » 

Revision d7fa7e2c

Added by watson1978 (Shizuo Fujita) over 4 years ago

[flori/json] Convert string encoding to UTF-8 only when needed

Before

$ ruby bench_json_generate.rb
Warming up --------------------------------------
                json   129.000  i/100ms
Calculating -------------------------------------
                json      1.300k (± 2.3%) i/s -      6.579k in   5.064656s

After

$ ruby bench_json_generate.rb
Warming up --------------------------------------
                json   189.000  i/100ms
Calculating -------------------------------------
                json      1.964k (± 3.3%) i/s -      9.828k in   5.011237s

Code

require 'json'
require 'benchmark/ips'

obj = []

1000.times do |i|
  obj << {
    "id" => i,
    :age => 42,
  }
end

Benchmark.ips do |x|
  x.report "json" do |iter|
    count = 0
    while count < iter
      JSON.generate(obj)
      count += 1
    end
  end
end

https://github.com/flori/json/commit/c34d01ff6a