作者godfat (godfat 真常)
看板Ruby
標題Re: [問題] 請教關於 static method
時間Wed Feb 13 21:38:44 2008
※ 引述《mokuku (mokuku)》之銘言:
: Ruby 定義 method 時,
: 可以用
: def self.methodname
: end
: 來定義 static method
在 ruby 請稱之為 class method, static 這個字眼根本不合理
: 哪種比較快阿?
require 'benchmark'
class C
def im; end
def self.cm; end
end
Benchmark.bm{ |bm|
c = C.new
bm.report('instance method call'){ 1000000.times{ c.im } }
bm.report('class method call'){ 1000000.times{ C.cm } }
}
> ruby bm_method_call.rb
user system total real
instance method call 0.310000 0.000000 0.310000 ( 0.310461)
class method call 0.330000 0.000000 0.330000 ( 0.339216)
差不多吧
--
#!/usr/bin/env ruby [露比] /Programming (Kn|N)ight/ 看板《Ruby》
# if a
dog nailed
extra legs that
http://webptt.com/m.aspx?n=bbs/Ruby/index.html
#
walks like an octopus, and Welcome ~
Ruby@ptt~
#
talks like an octopus, then
◢█◣ http://www.ruby-lang.org/
# we are happy to treat it as
█ http://www.ruby-doc.org/
# if it were
an octopus.
◥ ◤ http://www.rubyforge.org/
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.28.18
1F:推 mokuku:原來要叫 class method 喔! 02/14 00:41
2F:→ mokuku:我用你的 code 測試的結果跟你一樣, class method 比較慢! 02/14 00:41
3F:→ mokuku:然後還測試了 global、instance、class、local等變數 02/14 00:43
4F:→ mokuku:local>global>instance>class變數, 不過差別微乎其微就是了 02/14 00:45