Created
September 1, 2016 17:27
-
-
Save BitOfUniverse/53a879bd8784a29cf0dc9a943f3a4421 to your computer and use it in GitHub Desktop.
Benchmark: Hash to JSON convertation methods
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 'benchmark' | |
require 'json/ext' | |
iterations = 1_000_000 | |
hash = { who: 'Me', first_name: 'Alexey', last_name: 'Black', title: 'Doctor', | |
birthdate: Time.now, favorite_color: 'transparent', car: 'BMW', hands: 2, | |
city: 'Quanzi', planet: 'Third' } | |
Benchmark.bm(27) do |bm| | |
bm.report('hash.to_json') do | |
iterations.times do | |
hash.to_json | |
end | |
end | |
bm.report('string interpolation') do | |
iterations.times do | |
"who: #{hash[:who]} first_name: #{hash[:first_name]} last_name: #{hash[:last_name]} \ | |
title: #{hash[:title]} birthdate: #{hash[:birthdate]} favorite_color: #{hash[:favorite_color]}\ | |
car: #{hash[:car]} hands: #{hash[:hands]} city: #{hash[:city]} planet: #{hash[:planet]}" | |
end | |
end | |
bm.report('string#format') do | |
iterations.times do | |
format "who: %{who} first_name: %{first_name} last_name: %{last_name} \ | |
title: %{title} birthdate: %{birthdate} favorite_color: %{favorite_color}\ | |
car: %{car} hands: %{hands} city: %{city} planet: %{planet}", hash | |
end | |
end | |
end | |
# user system total real | |
# hash.to_json 11.930000 0.050000 11.980000 ( 12.069950) | |
# string interpolation 3.350000 0.030000 3.380000 ( 3.437143) | |
# string#format 4.950000 0.020000 4.970000 ( 4.993995) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍