Re: [問題] 請教關於 static method
※ 引述《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://www.ptt.cc/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
推
02/14 00:41, , 1F
02/14 00:41, 1F
→
02/14 00:41, , 2F
02/14 00:41, 2F
→
02/14 00:43, , 3F
02/14 00:43, 3F
→
02/14 00:45, , 4F
02/14 00:45, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 3 篇):
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章