開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 14章(ブロックとproc), 14.4(練習問題)、よりよいプロファイリングを解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
よりよいプロファイリング
コード(TextWrangler)
#!/usr/bin/env ruby
#encoding: utf-8
def profile block_description, &block
bln = true
if bln
start_time = Time.new
block.call
duration = Time.new - start_time
puts "#{block_description}: #{duration}"
else
block.call
end
end
profile '25000回の倍化' do
number = 1
25000.times do
number = number + number
end
puts "#{number.to_s.length}桁"
end
profile '百万までのカウント' do
number = 0
1000000.times do
number = number + 1
end
end
入出力結果(Terminal)
$ ./sample.rb 7526桁 25000回の倍化: 0.087466 百万までのカウント: 0.266702 $
コード(TextWrangler)
#!/usr/bin/env ruby
#encoding: utf-8
def profile block_description, &block
bln = false
if bln
start_time = Time.new
block.call
duration = Time.new - start_time
puts "#{block_description}: #{duration}"
else
block.call
end
end
profile '25000回の倍化' do
number = 1
25000.times do
number = number + number
end
puts "#{number.to_s.length}桁"
end
profile '百万までのカウント' do
number = 0
1000000.times do
number = number + 1
end
end
入出力結果(Terminal)
$ ./sample.rb 7526桁 $
今回の周ではirb(インタラクティブRuby)も少しずつ活用してみることに。
まだ上記の本を入手してないのでまた最初から。
本書を続けつつ、上記の本を早く入手してそっちに切り替え。
0 コメント:
コメントを投稿