作者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/cn.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