# Problem with arrays

**URL:** <https://www.ruby-forum.com/t/problem-with-arrays/119643>\
**Category:** Ruby\
**Created:** [November 8, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/problem-with-arrays/119643 "2007-11-08T21:08:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbarre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vbarre](https://www.ruby-forum.com/u/vbarre)\
**Post date:** [November 8, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/problem-with-arrays/119643/1 "2007-11-08T21:08:03Z")

</div>

Hello,

I’m coding a program which implements the levenshtein distance between  
two  
strings. It works, but very slowly.

I discover that the slowest line in the code is:

current[j] = m

where current is an array of Fixnum, and m is a Fixnum.

If i comment this line the program runs about 50 times faster.  
If I change it to any other thing, like “current[j] = 1” it still  
running  
fast.

I can’t understand why “current[j] = 1” is faster than “current[j] = m”.

Thanks,

Michel B.

---

<div class="post-metadata">

**Author:** ![vbarre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vbarre](https://www.ruby-forum.com/u/vbarre)\
**Post date:** [November 8, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/problem-with-arrays/119643/2 "2007-11-08T23:59:36Z")

</div>

Michel B. wrote:

> I discover that the slowest line in the code is:
> 
> current[j] = m
> 
> where current is an array of Fixnum, and m is a Fixnum.
> 
> If i comment this line the program runs about 50 times faster.  
> If I change it to any other thing, like “current[j] = 1” it still  
> running  
> fast.
> 
> I can’t understand why “current[j] = 1” is faster than “current[j] = m”.

As far as I can tell, it is not faster.

n = 10\_000\_000

start\_time = Time.now  
intarr = [1, 2, 3]  
n.times {intarr[1] = n}  
p Time.now - start\_time

start\_time = Time.now  
intarr = [1, 2, 3]  
n.times {intarr[1] = 5}  
p Time.now - start\_time

result:  
3.438  
3.437

---

<div class="post-metadata">

**Author:** ![vbarre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vbarre](https://www.ruby-forum.com/u/vbarre)\
**Post date:** [December 17, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/problem-with-arrays/119643/3 "2009-12-17T19:58:38Z")

</div>

Opened [http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3314](http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3314).  
That’s wacky! Doesn’t happen if you remove the last or first item …  
it’s gonna be fun to track that down. =)
