# Parallel assignments vs. Serial assigments

**URL:** <https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423>\
**Category:** ruby-talk\
**Created:** [15 February 2010 16:25 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423 "2010-02-15T16:25:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gavin](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Gavin](https://rubytalk.org/u/Gavin)\
**Post date:** [15 February 2010 16:25 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/1 "2010-02-15T16:25:07Z")

</div>

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

&nbsp;&nbsp;def initialize(first\_name, last\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;@first\_name = first\_name  
&nbsp;&nbsp;&nbsp;&nbsp;@last\_name = last\_name  
&nbsp;&nbsp;end

end

class Two

&nbsp;&nbsp;def initialize(first\_name, last\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;@first\_name, @last\_name = first\_name, last\_name  
&nbsp;&nbsp;end

end  
require "rubygems"  
require "benchmark"

Benchmark.bmbm do |test|  
&nbsp;&nbsp;test.report("serial") do  
&nbsp;&nbsp;&nbsp;&nbsp;10000.times { |n| var = One.new("gavin#{n}", "morrice")}  
&nbsp;&nbsp;end  
&nbsp;&nbsp;test.report("parallel") do  
&nbsp;&nbsp;&nbsp;&nbsp;10000.times { |n| var = Two.new("gavin#{n}", "morrice")}  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Gavin](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Gavin](https://rubytalk.org/u/Gavin)\
**Post date:** [15 February 2010 16:30 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/2 "2010-02-15T16:30:21Z")

</div>

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

> **···**
>
> On Feb 15, 4:23 pm, Gavin \<thinkersplaygro...@googlemail.com\> 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

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [15 February 2010 16:47 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/3 "2010-02-15T16:47:18Z")

</div>

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

class One

def initialize(first\_name, last\_name)  
&nbsp;&nbsp;&nbsp;@first\_name = first\_name  
&nbsp;&nbsp;&nbsp;@last\_name = last\_name  
end

end

class Two

def initialize(first\_name, last\_name)  
&nbsp;&nbsp;&nbsp;@first\_name, @last\_name = first\_name, last\_name  
end  
end

class Three

def initialize(\*args)  
&nbsp;&nbsp;&nbsp;@first\_name, @last\_name = \*args  
end  
end

require "rubygems"  
require "benchmark"

GC.disable

puts "Arrays before serial: #{ObjectSpace.each\_object(Array){}}"  
10000.times { |n| var = One.new("gavin#{n}", "morrice")}  
puts "Arrays after serial: #{ObjectSpace.each\_object(Array){}}"  
10000.times { |n| var = 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.

> **···**
>
> On Mon, Feb 15, 2010 at 5:30 PM, Gavin \<thinkersplayground@googlemail.com\> wrote:
> 
> > (My results show that parallel assignment is slower in this test)
> > 
> > On Feb 15, 4:23 pm, Gavin \<thinkersplaygro...@googlemail.com\> 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?

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [15 February 2010 16:50 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/4 "2010-02-15T16:50:57Z")

</div>

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 = One.new("gavin#{n}", "morrice")}  
puts "Arrays after serial: #{ObjectSpace.each\_object(Array){}}"  
10000.times { |n| var = Two.new("gavin#{n}", "morrice")}  
puts "Arrays after parallel: #{ObjectSpace.each\_object(Array){}}"  
10000.times { |n| var = 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.

> **···**
>
> 2010/2/15 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> 
> > 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 = 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
> > 
> > class Three
> > 
> > def initialize(\*args)  
> > @first\_name, @last\_name = \*args  
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [16 February 2010 18:01 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/5 "2010-02-16T18:01:20Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> On 02/15/2010 05:50 PM, Jesús Gabriel y Galán wrote:
> 
> > 2010/2/15 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
> > 
> > > I think it might be because the parallel assigment creates an array  
> > > under the hood?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [16 February 2010 18:16 UTC](https://rubytalk.org/t/parallel-assignments-vs-serial-assigments/57423/6 "2010-02-16T18:16:31Z")

</div>

> > > 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

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.

> **···**
>
> On Tue, Feb 16, 2010 at 7:01 PM, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 02/15/2010 05:50 PM, Jesús Gabriel y Galán wrote:
> > 
> > > 2010/2/15 Jesús Gabriel y Galán \<jgabrielygalan@gmail.com\>:
