Simple method overhead

G

Guest

Hi all=2E

I have the method like:
class MyCoolClass
def initialize
@var =3D []
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=2E09 0=2E05 0=2E04 66565 MyCoolClass#[]
0=2E04 0=2E04 0=2E00 66565 Array#[]

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

Thanks=2E

V=2E
 
M

Michael Fellinger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,716
Latest member
MiloManley

Latest Threads

Top