Last active
August 5, 2021 13:33
-
-
Save monsha/771d4fb5fb9724012cf8d9437c9dc411 to your computer and use it in GitHub Desktop.
Benchmarking hashing algorithms with json
This file contains 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 "benchmark/memory" | |
payload = { | |
"was_changed"=>0, | |
"store"=>"gp", | |
"os"=>nil, | |
"thumbs_down_cnt"=>0, | |
"manufactorer"=>nil, | |
"content"=>"The voices are so soothing. I am only six days in, but I feel good about what little I have done and what I am doing. If I remember to do so I will give an update at the end of the month.", | |
"is_answer"=>0, | |
"device"=>nil, | |
"title"=>"", | |
"answer_date"=>nil, | |
"is_deleted"=>0, | |
"rating"=>5, | |
"internal_id"=>11111111, | |
"thumbs_up_cnt"=>0, | |
"app_version_code"=>nil, | |
"user_id"=>nil, | |
"date"=>"2021-08-01", | |
"created"=>"2021-08-01 22:43:45", | |
"has_answer"=>0, | |
"country"=>nil, | |
"answer_text"=>nil, | |
"author"=>"J", | |
"time"=>"19:25:06", | |
"answer_time"=>nil, | |
"app_version"=>"3.68", | |
"device_type"=>nil, | |
"review_id"=>"gp:AOqpTOHDK3vPjOQBf5Xbr", | |
"lang"=>"en", | |
"external_id"=>"co.thefabulous.app" | |
} | |
TIMES = 1000000 | |
def sha_256(payload) | |
TIMES.times do | |
Digest::SHA256.hexdigest(payload.to_json) | |
end | |
end | |
def sha_1(payload) | |
TIMES.times do | |
Digest::SHA1.hexdigest(payload.to_json) | |
end | |
end | |
def md5(payload) | |
TIMES.times do | |
Digest::MD5.hexdigest(payload.to_json) | |
end | |
end | |
Benchmark.bmbm do |x| | |
x.report("MD5") { md5(payload) } | |
x.report("SHA-1") { sha_1(payload) } | |
x.report("SHA-256") { sha_256(payload) } | |
end | |
Benchmark.memory do |x| | |
x.report("MD5") { md5(payload) } | |
x.report("SHA-1") { sha_1(payload) } | |
x.report("SHA-256") { sha_256(payload) } | |
x.compare! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment