Last active
December 27, 2015 03:59
-
-
Save sonots/7263495 to your computer and use it in GitHub Desktop.
http://weblog.riywo.com/post/31254453611の追試。str.bytes.inject(&:+) % num でも均等に散ってた. 結局のところ入力文字の分布によるわけだが、入力文字の分布はわからんのでおいとく
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 'digest/md5' | |
require 'digest/sha1' | |
require 'digest/sha2' | |
require 'murmurhash3' | |
num = 5 | |
test = 10000 | |
def str(len) | |
(("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a).shuffle[0,len].join | |
end | |
res = { | |
"raw" => Array.new(num, 0), | |
"md5" => Array.new(num, 0), | |
"sha1" => Array.new(num, 0), | |
"sha256" => Array.new(num, 0), | |
"murmurhash3" => Array.new(num, 0), | |
} | |
base = str(10) + "." + str(10) + "." + str(10) | |
test.times { | |
str = base + "." + str(10) # construct string like typical fluentd tags (like the last one is hostname) | |
res["raw"][str.bytes.inject(&:+) % num] += 1 | |
res["md5"][Digest::MD5::hexdigest(str).bytes.inject(&:+) % num] += 1 | |
res["sha1"][Digest::SHA1::hexdigest(str).bytes.inject(&:+) % num] += 1 | |
res["sha256"][Digest::SHA256::hexdigest(str).bytes.inject(&:+) % num] += 1 | |
res["murmurhash3"][MurmurHash3::V32.str_hash(str) % num] += 1 | |
} | |
res.each_pair {|k, v| | |
rate = v.map {|x| | |
(x.to_f / test * 100).to_i | |
} | |
p sprintf "%15s => %p", k, rate | |
} | |
" raw => [19, 20, 19, 19, 20]" | |
" md5 => [20, 19, 19, 19, 20]" | |
" sha1 => [19, 20, 19, 19, 19]" | |
" sha256 => [20, 19, 19, 20, 20]" | |
" murmurhash3 => [20, 19, 20, 19, 20]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment