Forum: Ruby Looking up properties and speed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d4ec8946f933a18a1d15b094cc3c425?d=identicon&s=25 Jonathan Leighton (Guest)
on 2006-01-12 01:11
(Received via mailing list)
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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-01-12 02:06
(Received via mailing list)
On Jan 11, 2006, at 4:08 PM, Jonathan Leighton wrote:

>   val = obj.prop
> 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?

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 Chad Perrin (Guest)
on 2006-01-12 03:01
(Received via mailing list)
On Thu, Jan 12, 2006 at 10:03:30AM +0900, Eric Hodel wrote:
> >
> >
> >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?

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

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

"A script is what you give the actors.  A program
is what you give the audience." - Larry Wall
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 03:07
(Received via mailing list)
Jonathan Leighton wrote:

>  val = obj.prop
>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
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 03:10
(Received via mailing list)
David Vallner wrote:

> 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
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-01-12 03:13
(Received via mailing list)
Chad Perrin wrote:
..>
> ... What's wrong with curiosity?
>

Don't ask.


James



--

http://www.ruby-doc.org       - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com      - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com     - Playing with Better Toys
http://www.30secondrule.com   - Building Better Tools
Ec9233451f7c6ba37a83388b87a1f565?d=identicon&s=25 Gavin Kistner (Guest)
on 2006-01-12 05:07
(Received via mailing list)
On Jan 11, 2006, at 5:08 PM, Jonathan Leighton wrote:
> 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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-01-12 06:20
(Received via mailing list)
On Jan 11, 2006, at 5:58 PM, Chad Perrin wrote:

>>> between
>>>  puts obj.prop
>
> 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.

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
9d4ec8946f933a18a1d15b094cc3c425?d=identicon&s=25 Jonathan Leighton (Guest)
on 2006-01-12 11:09
(Received via mailing list)
On Thu, 2006-01-12 at 11:05 +0900, David Vallner wrote:
>
> 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
Cb2b768a5e546b24052ea03334e43676?d=identicon&s=25 Dr Nic (Guest)
on 2006-01-12 11:25
(Received via mailing list)
>> ... What's wrong with curiosity?
>Don't ask.

Yeah, wasn't there something about a cat, and a curiosity-related
fatality.

Nic
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-01-12 12:23
(Received via mailing list)
David Vallner wrote:

<snip/>

> <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
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-01-12 12:29
(Received via mailing list)
[Sidetracking gruesomely]

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

David Vallner
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-01-12 12:49
(Received via mailing list)
Jonathan Leighton wrote:
>   val = obj.prop
> 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
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2006-01-13 16:06
(Received via mailing list)
On 1/11/06, Jonathan Leighton <lists@turnipspatch.com> wrote:
> 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 == other_var      2.547000   0.000000   2.547000 (
2.407000)
2. var == other_var           1.688000   0.000000   1.688000 (
1.595000)
-------------------------------------------------------------------------
|                   Fastest was <2. var == 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 = foo
  end
end

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

__END__
test.foo == other_val
val == other_val
This topic is locked and can not be replied to.