Looking up properties and speed

J

Jonathan Leighton

Hi,

In Javascript, if one were to use the same property value twice or more,
one would store it in a local variable because looking up the property
slows things down.

Is the situation the same in Ruby? Is there any speed difference between
these:

def foo
val = obj.prop
puts val
puts val
end

def boo
puts obj.prop
puts obj.prop
end

I assume not because I think the reason it's like that with Javascript
is because of the DOM (yeah, that's not strictly Javascript).

Cheers
 
E

Eric Hodel

In Javascript, if one were to use the same property value twice or
more,
one would store it in a local variable because looking up the property
slows things down.

Is the situation the same in Ruby? Is there any speed difference
between
these:

def foo
val = obj.prop
puts val
puts val
end

def boo
puts obj.prop
puts obj.prop
end

I assume not because I think the reason it's like that with Javascript
is because of the DOM (yeah, that's not strictly Javascript).

Why do you care?

Why not make clean, readable code and optimize the parts that are
causing the biggest slowdown?
 
C

Chad Perrin

Why do you care?

Why not make clean, readable code and optimize the parts that are
causing the biggest slowdown?

Hey, I'm curious about the answer too, whether I ever use that knowledge
or not. What's wrong with curiosity?
 
D

David Vallner

Jonathan said:
Hi,

In Javascript, if one were to use the same property value twice or more,
one would store it in a local variable because looking up the property
slows things down.

Is the situation the same in Ruby? Is there any speed difference between
these:

def foo
val = obj.prop
puts val
puts val
end

def boo
puts obj.prop
puts obj.prop
end

I assume not because I think the reason it's like that with Javascript
is because of the DOM (yeah, that's not strictly Javascript).

Cheers
Hmm. Accessing the property should be slower, because it involves a
method call.

<rant>
BUT! Quite a few guides on coding style would say temporary variables
should be avoided whenever possible in clean OO code and replaced with
queries (computed property getters). And using a temporary variable only
to cheat the interpreter is plain wrong. Avoid. The speed improvement
you gain will most likely be next to insignificant, and if not, you
still should never optimize without profiling the code first.

That said, the code you show is more likely to end up refactored as a
method of ``obj'', where you could access the instance variable
directly. A method that only manipulates data on another object indeed
should be a method of that object.

Suggested reading: Martin Fowler's "Refactoring...", a timeless, and IMO
highly respected classic.
</rant>

David
 
D

David Vallner

David said:
Hmm. Accessing the property should be slower, because it involves a
method call.
More recommended reading: documentation for the "benchmark" module.
Would probably give an answer much faster than asking on here at any rate ;P

David Vallner
 
G

Gavin Kistner

Is the situation the same in Ruby? Is there any speed difference
between
these:

def foo
val = obj.prop
puts val
puts val
end

def boo
puts obj.prop
puts obj.prop
end

Even moreso. In Javascript it's a slowdown as the property resolution
occurs. In Ruby, it's an entirely new method call.
 
E

Eric Hodel

Hey, I'm curious about the answer too, whether I ever use that
knowledge
or not. What's wrong with curiosity?

Nothing. But worrying about micro-optimizations will take all the
fun away and give you little measurable benefit (by the 90/10 rule).

If you write clean, readable code from the beginning and then go
looking for the slow spots when you *need* to you'll be happiest. I
promise.
 
J

Jonathan Leighton

Hmm. Accessing the property should be slower, because it involves a
method call.

<rant>
BUT! Quite a few guides on coding style would say temporary variables
should be avoided whenever possible in clean OO code and replaced with
queries (computed property getters). And using a temporary variable only
to cheat the interpreter is plain wrong. Avoid. The speed improvement
you gain will most likely be next to insignificant, and if not, you
still should never optimize without profiling the code first.

That said, the code you show is more likely to end up refactored as a
method of ``obj'', where you could access the instance variable
directly. A method that only manipulates data on another object indeed
should be a method of that object.

Suggested reading: Martin Fowler's "Refactoring...", a timeless, and IMO
highly respected classic.
</rant>

Thanks for the input and explanation. It *was* more a theoretical
question than something I was actually considering as general coding
practise -- I find it strangely interesting to find out which ways of
doing things are faster or slower and why.

Anyway, thanks everyone

Jon
 
R

Robert Klemme

David Vallner wrote:

<rant>
BUT! Quite a few guides on coding style would say temporary variables
should be avoided whenever possible in clean OO code and replaced with
queries (computed property getters).

I don't think that this general rule holds. A crucial bit to remember -
and that hasn't been mentioned if I'm not mistaken - is that there is a
semantic difference between

obj.foo << bar
obj.foo << baz
obj.foo << buz
obj.foo << bum

and

f = obj.foo
f << bar
f << baz
f << buz
f << bum

This code will behave quite different if

- multiple instances access obj concurrently

- obj.foo does not simply return an object but creates a new one for
every call (or even more complex behavior)

It depends on the situation at hand which of the two is the more
appropriate solution.
And using a temporary variable
only to cheat the interpreter is plain wrong. Avoid. The speed
improvement you gain will most likely be next to insignificant, and
if not, you still should never optimize without profiling the code
first.
Definitely!

That said, the code you show is more likely to end up refactored as a
method of ``obj'', where you could access the instance variable
directly. A method that only manipulates data on another object indeed
should be a method of that object.

It depends: this might be taken as an indication to move the method there
but I don't subscribe to this general rule. It might be the case that the
method doesn't fit the class (i.e. doesn't make sense to be part of the
interface).
Suggested reading: Martin Fowler's "Refactoring...", a timeless, and
IMO highly respected classic.

+1

Kind regards

robert
 
D

David Vallner

[Sidetracking gruesomely]

In which case, making #foo look like a property accessor is probably =20
misleading, naming the method #make_foo or #create_foo is probably better=
=20
style.

David Vallner
 
S

Stefan Kaes

Jonathan said:
Hi,

In Javascript, if one were to use the same property value twice or more,
one would store it in a local variable because looking up the property
slows things down.

Is the situation the same in Ruby? Is there any speed difference between
these:

def foo
val = obj.prop
puts val
puts val
end

def boo
puts obj.prop
puts obj.prop
end

I assume not because I think the reason it's like that with Javascript
is because of the DOM (yeah, that's not strictly Javascript).

Cheers

Accessing a property involves a function call, which is probably slower
than caching the value in a local variable. Even more so if the
computation of the property value is expensive. However, I would worry
about the speed difference only when the coding path is an application
hot spot.

-- stefan
 
R

Ryan Leavengood

Hi,

In Javascript, if one were to use the same property value twice or more,
one would store it in a local variable because looking up the property
slows things down.

Is the situation the same in Ruby?

In almost any language method calls will be slower than directly
accessing a variable. I did a quick benchmark with my "quickbench"
utility (I'll release it one of these days), and here are the results:

-------------------------------------------------------------------------
| QuickBench Session Started |
| 3000000 Iterations |
-------------------------------------------------------------------------
user system total real
1. test.foo =3D=3D other_var 2.547000 0.000000 2.547000 ( 2.40700=
0)
2. var =3D=3D other_var 1.688000 0.000000 1.688000 ( 1.59500=
0)
-------------------------------------------------------------------------
| Fastest was <2. var =3D=3D other_var> =
|
-------------------------------------------------------------------------

So the method call it about half as fast, which isn't that bad. Here
is the code:

require 'quickbench'

class Test
attr_reader :foo
def initialize(foo)
@foo =3D foo
end
end

test =3D Test.new('something')
val =3D test.foo
other_val =3D 'blah'
QuickBench.go(3000000, 25) {}

__END__
test.foo =3D=3D other_val
val =3D=3D other_val
 

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,201
Messages
2,571,051
Members
47,656
Latest member
rickwatson

Latest Threads

Top