Skip to content

Instantly share code, notes, and snippets.

@hiroeorz
Created May 6, 2014 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroeorz/11553850 to your computer and use it in GitHub Desktop.
Save hiroeorz/11553850 to your computer and use it in GitHub Desktop.
ツナでもわかるmruby [5回目:配列と遅延評価] ref: http://qiita.com/hiroeorz@github/items/371a12316091c63a3aa8
start_time = Time.now
Array.new(1000000, 1).lazy.map{|e| e}.take(3).force
end_time = Time.now
process_time = end_time - start_time
puts "process time: #{process_time}"
start_time = Time.now
Array.new(1000000, 1).map{|e| e}[0, 3]
end_time = Time.now
process_time = end_time - start_time
puts "process time: #{process_time}"
[1,2,3,4,5,6,7,8,9,10].lazy.map{|e| e * e}.
select{|e| (e % 2) == 0}.
take(3).
force
$ mruby array-time-lazy.rb
process time: 0.17267199999999
$ ruby array-time-lazy.rb
process time: 0.00678566
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment