Created
May 6, 2014 05:34
-
-
Save hiroeorz/11553850 to your computer and use it in GitHub Desktop.
ツナでもわかるmruby [5回目:配列と遅延評価] ref: http://qiita.com/hiroeorz@github/items/371a12316091c63a3aa8
This file contains hidden or 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
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}" |
This file contains hidden or 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
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}" |
This file contains hidden or 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
[1,2,3,4,5,6,7,8,9,10].lazy.map{|e| e * e}. | |
select{|e| (e % 2) == 0}. | |
take(3). | |
force |
This file contains hidden or 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
$ mruby array-time-lazy.rb | |
process time: 0.17267199999999 |
This file contains hidden or 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
$ 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