require 'benchmark' module Enumerable def amap map {|e| Thread.new { yield e } }.map(&:value) end end ary = [1, 2, 3, 4, 5] res = {} Benchmark.bm(4) do |x| x.report('map') { res[:map] = ary.map {|e| sleep 1; e } } x.report('amap') { res[:amap] = ary.amap {|e| sleep 1; e } } end p [:map, res[:map]] p [:amap, res[:amap]]
çµæã¯ãããªæã
user system total real map 0.000000 0.000000 0.000000 ( 5.000851) amap 0.000000 0.010000 0.010000 ( 1.001148) [:map, [1, 2, 3, 4, 5]] [:amap, [1, 2, 3, 4, 5]]
ååãã ããã®ãæ¬ ç¹ã§ããï¼(Rubyã§ã¯ååéè¦)
# ããç¨åº¦æéã®ããããããªå¦çã§ãªãã¨Threadãä½ãã³ã¹ãã®æ¹ãé«ãã¤ãã®ã§ãé ãã§ãã#=> Oh, very useless!
(追è¨)
ï¼å¹´ä»¥ä¸åã«disããã¦ãã件
Ruby 1.8ã¾ã§ã®ã¹ã¬ããã¯ã¹ã¿ãã¯ãæ¯åã¾ããã¨ã³ãã¼ãã¦ããã®ã§ã³ã³ããã¹ãã¹ã¤ããã®ã³ã¹ãããã¡ããã¡ãé«ãããåããã¨ãã使ãããã¨ãã¯è¨ãããã©ãééã£ã¦ããæ§è½ãè¯ããªãããªãã¦ãã¨ã¯è¨ããªãã