Created
October 3, 2016 11:31
-
-
Save unak/19da92f6cc4996bb503e08aa8e252089 to your computer and use it in GitHub Desktop.
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
RSTRLEN_MAX = 50 | |
N = 10000 | |
# user system total real | |
# ana1 0.109000 0.000000 0.109000 ( 0.104961) | |
# ana2 0.078000 0.000000 0.078000 ( 0.074173) | |
require 'benchmark' | |
def ana1(s1, s2) | |
s1.chars.sort == s2.chars.sort | |
end | |
def ana2(s1, s2) | |
s1.chars.inject(s2.dup){|b,c| b.sub!(c,'') or return false}.empty? | |
end | |
def rstr(len) | |
str = String.new(capacity: len) | |
len.times do | |
str << (rand(0x7F-0x20)+0x20).chr | |
end | |
str | |
end | |
data = Array.new(N) | |
N.times do |i| | |
data[i] = [rstr(rand(RSTRLEN_MAX)), rstr(rand(RSTRLEN_MAX))] | |
end | |
Benchmark.bm do |bm| | |
bm.report("ana1") do | |
data.each do |s1, s2| | |
ana1(s1, s2) | |
end | |
end | |
bm.report("ana2") do | |
data.each do |s1, s2| | |
ana2(s1, s2) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
問: RSTRLEN_MAX = 50 の時に2つのランダム文字列が偶然アナグラムになる確率はどれくらいか、求めよ。
(正解は各自で考えよう!)