Re: [問題] 請教關於 static method

看板Ruby作者 (godfat 真常)時間17年前 (2008/02/13 21:38), 編輯推噓1(103)
留言4則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《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
原來要叫 class method 喔!
02/14 00:41, 1F

02/14 00:41, , 2F
我用你的 code 測試的結果跟你一樣, class method 比較慢!
02/14 00:41, 2F

02/14 00:43, , 3F
然後還測試了 global、instance、class、local等變數
02/14 00:43, 3F

02/14 00:45, , 4F
local>global>instance>class變數, 不過差別微乎其微就是了
02/14 00:45, 4F
文章代碼(AID): #17ilBcHx (Ruby)
討論串 (同標題文章)
文章代碼(AID): #17ilBcHx (Ruby)