ãããã³ã°å¦çã«Tokyo Cabinetã使ã£ããé«éããã¦å¹¸ç¦ãå®ç¾ãã
1000ä¸ä»¶ãããã®ãã¹ã¿ã¼ãã¼ã¿ã«ã50ä¸ä»¶ãããã®ãã©ã³ã¶ã¯ã·ã§ã³ãã¼ã¿ãçªã£è¾¼ã¿ããããã ããæ¿å
¥ã§ããã®ã¨éè¤ããã®ãææ¡ãããã
MySQLã ã¨ã©ãé å¼µã£ã¦ã30åããããããã®ã§ãTokyo Cabinetã使ã£ã¦ãã£ã¦ã¿ãã
ã¾ãã¯ãã¹ã¿ã¼ãã¼ã¿ä½æã1000ä¸ä»¥ä¸ã®ä¹±æ°ãçºçããã¦ãããããã¼ã«ã1000ä¸åã¹ãã¢ããã
$ cat tcTest.rb require 'tokyocabinet' include TokyoCabinet db = HDB.new db.open('db.tch', TDB::OWRITER | TDB::OCREAT) 10000000.times{|i| db[rand(10000000)] = 0 } db.close
5åãããããã£ããä½ä»¶å ¥ã£ãã確èªãã¦ã¿ãã
$ cat tcTestR.rb require 'tokyocabinet' include TokyoCabinet db = HDB.new db.open('db.tch', TDB::OWRITER | TDB::OCREAT) p db.rnum db.close $ ruby tcTestR.rb 6321134
632ä¸ä»¶å ¥ã£ãã次ã¯ãã©ã³ã¶ã¯ã·ã§ã³ãã¼ã¿ãä½ãã
$ seq 1 500000 > 50man.txt
ãã¹ã¿ã¼ã¨ãã©ã³ã¶ã¯ã·ã§ã³ãã§ããã®ã§ããããã³ã°ããã¦ã¿ãã
$ cat tcTestS.rb require 'tokyocabinet' include TokyoCabinet db = HDB.new db.open('db.tch', TDB::OWRITER | TDB::OCREAT) dup = File.open("dup.txt", "w") add = File.open("add.txt", "w") File.open("50man.txt"){|file| while line = file.gets if db[line.chomp] == nil then add.puts(line.chomp) db[line.chomp] = 0 else dup.puts(line.chomp) end end } add.close dup.close db.close
$ time ruby tcTestS.rb real 0m12.500s user 0m2.800s sys 0m1.520s
12ç§!!! éã!!!
åºåãã¼ã¿ãã¡ããã¨ã§ãããä¸å¿ç¢ºèªããã
$ head add.txt | tr "\n" " "; echo 1 2 6 7 10 11 12 18 19 23 $ head dup.txt | tr "\n" " "; echo 3 4 5 8 9 13 14 15 16 17 $ tail add.txt | tr "\n" " "; echo 499976 499978 499980 499981 499984 499994 499995 499997 499998 500000 $ tail dup.txt | tr "\n" " "; echo 499986 499987 499988 499989 499990 499991 499992 499993 499996 499999
ããããªãã±ã¼ã
åç´ãªã©ã³ãã¦ã¿ããä»åº¦ã¯å
¨ã¦ãéè¤ã«ãªãã¯ãã
$ time ruby tcTestS.rb real 0m3.010s user 0m2.100s sys 0m0.910s $ diff 50man.txt dup.txt $ wc -l add.txt 0 add.txt
ãããå ¨é¨éè¤ã«ãªã£ãããªãã±ã¼ãã¦ãããå ¨ãã¹ãã¢ããªãã¨3ç§ã§çµããã®ãã!!!