[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple method overhead

Victor 'Zverok' Shepelev

10/26/2006 9:31:00 AM

Hi all.

I have the method like:
class MyCoolClass
def initialize
@var = []
end

#some other methods push values to @var

#here is method of interest:
def [](idx); @var[idx] end
end

I have those, a bit confusing, profiling results:

Total Self Children Calls Name
0.09 0.05 0.04 66565 MyCoolClass#[]
0.04 0.04 0.00 66565 Array#[]

The strange thing is: method MyCoolClass#[] spends so much time inside itself, though ALL it do - only call Array#[].
Is this normal? Maybe, because of so huge calls count, but I can't understand this :(

Thanks.

V.


1 Answer

Michael Fellinger

10/26/2006 10:07:00 AM

0

On Thursday 26 October 2006 18:31, vshepelev@imho.com.ua wrote:
> Hi all.
>
> I have the method like:
> class MyCoolClass
> def initialize
> @var = []
> end
>
> #some other methods push values to @var
>
> #here is method of interest:
> def [](idx); @var[idx] end
> end
>
> I have those, a bit confusing, profiling results:
>
> Total Self Children Calls Name
> 0.09 0.05 0.04 66565 MyCoolClass#[]
> 0.04 0.04 0.00 66565 Array#[]
>
> The strange thing is: method MyCoolClass#[] spends so much time inside
> itself, though ALL it do - only call Array#[]. Is this normal? Maybe,
> because of so huge calls count, but I can't understand this :(
>
> Thanks.
>
> V.

methods have quite some overhead, so to say :)
and this benchmark almost covers your values... so i'd say, yeah - it's simply
the overhead

# the code
require 'benchmark'

def one; end
def two; one end
def three; two end

iterate = 66_565

Benchmark.bmbm(20) do |x|
x.report("one :") { iterate.times{ one } }
x.report("two :") { iterate.times{ two } }
x.report("three:") { iterate.times{ three } }
end

=begin result
user system total real
one: 0.080000 0.020000 0.100000 ( 0.090971)
two: 0.080000 0.060000 0.140000 ( 0.134608)
three: 0.190000 0.040000 0.230000 ( 0.325138)
=end