Problem with arrays

  • Thread starter Michel Boaventura
  • Start date
M

Michel Boaventura

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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

Lloyd Linklater

Michel said:
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
 

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,270
Messages
2,571,349
Members
48,036
Latest member
nickwillsonn

Latest Threads

Top