Parallel assignments vs. Serial assigments

Discussion in 'Ruby' started by Gavin, Feb 15, 2010.

  1. Gavin

    Gavin Guest

    Hey all

    I was always under the impression that parallel assignments in Ruby
    were faster than assigning variables individually.

    Recently I was curious to see how much faster it was and decided to
    test it:

    class One

    def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name
    end

    end

    class Two

    def initialize(first_name, last_name)
    @first_name, @last_name = first_name, last_name
    end

    end
    require "rubygems"
    require "benchmark"

    Benchmark.bmbm do |test|
    test.report("serial") do
    10000.times { |n| var = One.new("gavin#{n}", "morrice")}
    end
    test.report("parallel") do
    10000.times { |n| var = Two.new("gavin#{n}", "morrice")}
    end
    end

    The results I get show that it's slower (in both Ruby 1.8.7 and Ruby
    1.9.1)

    Can anyone elaborate?

    Thanks
    Gavin, Feb 15, 2010
    #1
    1. Advertising

  2. Gavin

    Gavin Guest

    (My results show that parallel assignment is slower in this test)

    On Feb 15, 4:23 pm, Gavin <> wrote:
    > Hey all
    >
    > I was always under the impression that parallel assignments in Ruby
    > were faster than assigning variables individually.
    >
    > Recently I was curious to see how much faster it was and decided to
    > test it:
    >
    > class One
    >
    >   def initialize(first_name, last_name)
    >     @first_name = first_name
    >     @last_name  = last_name
    >   end
    >
    > end
    >
    > class Two
    >
    >   def initialize(first_name, last_name)
    >     @first_name, @last_name = first_name, last_name
    >   end
    >
    > end
    > require "rubygems"
    > require "benchmark"
    >
    > Benchmark.bmbm do |test|
    >   test.report("serial") do
    >     10000.times { |n| var =  One.new("gavin#{n}", "morrice")}
    >   end
    >   test.report("parallel") do
    >     10000.times { |n| var =  Two.new("gavin#{n}", "morrice")}
    >   end
    > end
    >
    > The results I get show that it's slower (in both Ruby 1.8.7 and Ruby
    > 1.9.1)
    >
    > Can anyone elaborate?
    >
    > Thanks
    Gavin, Feb 15, 2010
    #2
    1. Advertising

  3. On Mon, Feb 15, 2010 at 5:30 PM, Gavin
    <> wrote:
    > (My results show that parallel assignment is slower in this test)
    >
    > On Feb 15, 4:23=A0pm, Gavin <> wrote:
    >> Hey all
    >>
    >> I was always under the impression that parallel assignments in Ruby
    >> were faster than assigning variables individually.
    >>
    >> Recently I was curious to see how much faster it was and decided to
    >> test it:
    >>
    >> class One
    >>
    >> =A0 def initialize(first_name, last_name)
    >> =A0 =A0 @first_name =3D first_name
    >> =A0 =A0 @last_name =A0=3D last_name
    >> =A0 end
    >>
    >> end
    >>
    >> class Two
    >>
    >> =A0 def initialize(first_name, last_name)
    >> =A0 =A0 @first_name, @last_name =3D first_name, last_name
    >> =A0 end
    >>
    >> end
    >> require "rubygems"
    >> require "benchmark"
    >>
    >> Benchmark.bmbm do |test|
    >> =A0 test.report("serial") do
    >> =A0 =A0 10000.times { |n| var =3D =A0One.new("gavin#{n}", "morrice")}
    >> =A0 end
    >> =A0 test.report("parallel") do
    >> =A0 =A0 10000.times { |n| var =3D =A0Two.new("gavin#{n}", "morrice")}
    >> =A0 end
    >> end
    >>
    >> The results I get show that it's slower (in both Ruby 1.8.7 and Ruby
    >> 1.9.1)
    >>
    >> Can anyone elaborate?


    I think it might be because the parallel assigment creates an array
    under the hood?

    class One

    def initialize(first_name, last_name)
    @first_name =3D first_name
    @last_name =3D last_name
    end

    end

    class Two

    def initialize(first_name, last_name)
    @first_name, @last_name =3D first_name, last_name
    end
    end

    class Three

    def initialize(*args)
    @first_name, @last_name =3D *args
    end
    end


    require "rubygems"
    require "benchmark"

    GC.disable

    puts "Arrays before serial: #{ObjectSpace.each_object(Array){}}"
    10000.times { |n| var =3D One.new("gavin#{n}", "morrice")}
    puts "Arrays after serial: #{ObjectSpace.each_object(Array){}}"
    10000.times { |n| var =3D Two.new("gavin#{n}", "morrice")}
    puts "Arrays after parallel: #{ObjectSpace.each_object(Array){}}"

    $ ruby test_parallel_assignment.rb
    Arrays before serial: 3589
    Arrays after serial: 3589
    Arrays after parallel: 13589

    Jesus.
    Jesús Gabriel y Galán, Feb 15, 2010
    #3
  4. 2010/2/15 Jes=FAs Gabriel y Gal=E1n <>:
    > I think it might be because the parallel assigment creates an array
    > under the hood?
    >
    > class One
    >
    > =A0def initialize(first_name, last_name)
    > =A0 @first_name =3D first_name
    > =A0 @last_name =A0=3D last_name
    > =A0end
    >
    > end
    >
    > class Two
    >
    > =A0def initialize(first_name, last_name)
    > =A0 @first_name, @last_name =3D first_name, last_name
    > =A0end
    > end
    >
    > class Three
    >
    > =A0def initialize(*args)
    > =A0 @first_name, @last_name =3D *args
    > =A0end
    > end


    I have a class Three cause I was also testing this other form, which
    also creates arrays:

    GC.disable
    puts "Arrays before serial: #{ObjectSpace.each_object(Array){}}"
    10000.times { |n| var =3D One.new("gavin#{n}", "morrice")}
    puts "Arrays after serial: #{ObjectSpace.each_object(Array){}}"
    10000.times { |n| var =3D Two.new("gavin#{n}", "morrice")}
    puts "Arrays after parallel: #{ObjectSpace.each_object(Array){}}"
    10000.times { |n| var =3D Three.new("gavin#{n}", "morrice")}
    puts "Arrays after parallel with array: #{ObjectSpace.each_object(Array){}}=
    "

    $ ruby test_parallel_assignment.rb
    Arrays before serial: 3589
    Arrays after serial: 3589
    Arrays after parallel: 13589
    Arrays after parallel with array: 23589

    Jesus.
    Jesús Gabriel y Galán, Feb 15, 2010
    #4
  5. On 02/15/2010 05:50 PM, Jesús Gabriel y Galán wrote:
    > 2010/2/15 Jesús Gabriel y Galán <>:
    >> I think it might be because the parallel assigment creates an array
    >> under the hood?


    However it is done technically, parallel assignment needs more space
    because it has to evaluate *all* right hand sides before doing any
    assignments. Otherwise swapping would not be possible

    a, b = b, a

    So, yes, it's likely an Array under the hood but even if not the
    parallel assignment of two variables needs to store two object
    references while sequential assignments of an arbitrary number of
    elements gets away with space for a single reference (if you need it at
    all).

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 16, 2010
    #5
  6. On Tue, Feb 16, 2010 at 7:01 PM, Robert Klemme
    <> wrote:
    > On 02/15/2010 05:50 PM, Jes=FAs Gabriel y Gal=E1n wrote:
    >>
    >> 2010/2/15 Jes=FAs Gabriel y Gal=E1n <>:
    >>>
    >>> I think it might be because the parallel assigment creates an array
    >>> under the hood?

    >
    > However it is done technically, parallel assignment needs more space beca=

    use
    > it has to evaluate *all* right hand sides before doing any assignments.
    > =A0Otherwise swapping would not be possible
    >
    > a, b =3D b, a


    Good point.

    > So, yes, it's likely an Array under the hood


    Yep, that was my thought and that's why I checked with ObjectSpace,
    that indeed shows Arrays being created.
    It was easier for me than checking the implementation :).

    Jesus.
    Jesús Gabriel y Galán, Feb 16, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    532
  2. scattered

    Re: dynamic assigments

    scattered, Mar 24, 2011, in forum: Python
    Replies:
    8
    Views:
    194
    Steven D'Aprano
    Mar 25, 2011
  3. Steven D'Aprano

    Re: dynamic assigments

    Steven D'Aprano, Mar 24, 2011, in forum: Python
    Replies:
    5
    Views:
    211
    Steven D'Aprano
    Mar 25, 2011
  4. Andrew Huh?

    Dumb question about assigments

    Andrew Huh?, Jul 24, 2006, in forum: Ruby
    Replies:
    9
    Views:
    117
    F. Senault
    Jul 25, 2006
  5. Kedar Mhaswade
    Replies:
    14
    Views:
    301
    Adam Prescott
    Jan 17, 2011
Loading...

Share This Page