From: SonOfLilit Date: 2007-05-31T04:54:40+09:00 Subject: Re: Enumerable#serially - those nifty functions w/o memory footprint Following is ruby-prof output. First, a few words: 4.75 against 1.56 isn't too good, but it isn't ten times slower. I think it can be excused (and maybe the code can be optimized a bit) in cases where you'd process huge lists with ruby of all languages. This is the code used to profile: require 'rubygems' require 'ruby-prof' module Enumerable def serially(&b) a = [] self.each{|x| t = [x].instance_eval(&b); a << t[0] unless t.empty?} a end end # Profile the code result = RubyProf.profile{ a = (1..50000) b = a.serially{select{|x| x>280}.collect{|x|"0x" << x.to_s}.select{|x| x != "0x15"}} } # Print a graph profile to text printer = RubyProf::GraphPrinter.new(result) printer.print(STDOUT, 0) result = RubyProf.profile{ a = (1..50000) b = a.select{|x| x>280}.collect{|x|"0x" << x.to_s}.select{|x| x != "0x15"} } # Print a graph profile to text printer = RubyProf::GraphPrinter.new(result) printer.print(STDOUT, 0) %total %self total self children calls Name -------------------------------------------------------------------------------- 0.16 0.16 0.00 49720/49720 Array#select 3.37% 3.37% 0.16 0.16 0.00 49720 String#== -------------------------------------------------------------------------------- 0.19 0.19 0.00 49720/49720 Array#collect 4.00% 4.00% 0.19 0.19 0.00 49720 String#<< -------------------------------------------------------------------------------- 4.75 0.79 3.96 1/1 Enumerable#serially 100.00% 16.63% 4.75 0.79 3.96 1 Range#each 3.46 1.01 2.45 50000/50000 Kernel#instance_eval 0.18 0.18 0.00 49720/49720 Array#[] 0.15 0.15 0.00 50000/50000 Array#empty? 0.17 0.17 0.00 49720/49720 Array#<< -------------------------------------------------------------------------------- 3.46 1.01 2.45 50000/50000 Range#each 72.84% 21.26% 3.46 1.01 2.45 50000 Kernel#instance_eval 1.08 0.65 0.43 50000/50000 Array#collect 1.37 1.02 0.35 100000/100000 Array#select -------------------------------------------------------------------------------- 0.24 0.24 0.00 49720/49720 Array#collect 5.05% 5.05% 0.24 0.24 0.00 49720 Fixnum#to_s -------------------------------------------------------------------------------- 0.19 0.19 0.00 50000/50000 Array#select 4.00% 4.00% 0.19 0.19 0.00 50000 Fixnum#> -------------------------------------------------------------------------------- 4.75 0.00 4.75 1/1 #toplevel 100.00% 0.00% 4.75 0.00 4.75 1 Enumerable#serially 4.75 0.79 3.96 1/1 Range#each -------------------------------------------------------------------------------- 1.37 1.02 0.35 100000/100000 Kernel#instance_eval 28.84% 21.47% 1.37 1.02 0.35 100000 Array#select 0.16 0.16 0.00 49720/49720 String#== 0.19 0.19 0.00 50000/50000 Fixnum#> -------------------------------------------------------------------------------- 0.15 0.15 0.00 50000/50000 Range#each 3.16% 3.16% 0.15 0.15 0.00 50000 Array#empty? -------------------------------------------------------------------------------- 1.08 0.65 0.43 50000/50000 Kernel#instance_eval 22.74% 13.68% 1.08 0.65 0.43 50000 Array#collect 0.19 0.19 0.00 49720/49720 String#<< 0.24 0.24 0.00 49720/49720 Fixnum#to_s -------------------------------------------------------------------------------- 0.18 0.18 0.00 49720/49720 Range#each 3.79% 3.79% 0.18 0.18 0.00 49720 Array#[] -------------------------------------------------------------------------------- 0.17 0.17 0.00 49720/49720 Range#each 3.58% 3.58% 0.17 0.17 0.00 49720 Array#<< -------------------------------------------------------------------------------- 100.00% 0.00% 4.75 0.00 4.75 1 #toplevel 4.75 0.00 4.75 1/1 Enumerable#serially Thread ID: 1020900 %total %self total self children calls Name -------------------------------------------------------------------------------- 0.16 0.16 0.00 49720/49720 Array#select 10.00% 10.00% 0.16 0.16 0.00 49720 String#== -------------------------------------------------------------------------------- 0.14 0.14 0.00 49720/49720 Array#collect 8.75% 8.75% 0.14 0.14 0.00 49720 String#<< -------------------------------------------------------------------------------- 0.41 0.22 0.19 1/1 Enumerable#select 25.62% 13.75% 0.41 0.22 0.19 1 Range#each 0.19 0.19 0.00 50000/50000 Fixnum#> -------------------------------------------------------------------------------- 0.24 0.24 0.00 49720/49720 Array#collect 15.00% 15.00% 0.24 0.24 0.00 49720 Fixnum#to_s -------------------------------------------------------------------------------- 0.19 0.19 0.00 50000/50000 Range#each 11.88% 11.88% 0.19 0.19 0.00 50000 Fixnum#> -------------------------------------------------------------------------------- 0.41 0.00 0.41 1/1 #toplevel 25.62% 0.00% 0.41 0.00 0.41 1 Enumerable#select 0.41 0.22 0.19 1/1 Range#each -------------------------------------------------------------------------------- 0.38 0.22 0.16 1/1 #toplevel 23.75% 13.75% 0.38 0.22 0.16 1 Array#select 0.16 0.16 0.00 49720/49720 String#== -------------------------------------------------------------------------------- 0.81 0.43 0.38 1/1 #toplevel 50.62% 26.88% 0.81 0.43 0.38 1 Array#collect 0.14 0.14 0.00 49720/49720 String#<< 0.24 0.24 0.00 49720/49720 Fixnum#to_s -------------------------------------------------------------------------------- 100.00% 0.00% 1.60 0.00 1.60 1 #toplevel 0.41 0.00 0.41 1/1 Enumerable#select 0.81 0.43 0.38 1/1 Array#collect 0.38 0.22 0.16 1/1 Array#select