find index of first non zeo value in array

X

x1

Just saw this..

@ ruby conf, there were talks about the speed of ruby's regex

Being that ruby was the first language I picked up, I'm curious as to
how other languages(such as perl) compare from a performance
standpoint with your examples.

I'm not expecting perl to be slower an any scenerio, but is the perl
index() quicker or slower than a perl regex? If the latter, is the
performance ratio proportional to that of our ruby examples?



William said:
Robert Klemme wrote:
On 26.11.2006 14:59, Josselin wrote:
with :
array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0]

I wrote :
array.index(array.detect {|x| x > 0}) => 15

is there a better and simpler way to do it ?
thanks

Did we have a solution with #inject already?

array.inject(0){|i,e| if e>0; break i else i+1 end}

array.inject(0){|i,e| break i if e>0; i+1 }

Using a regular expression ...

array.join =~ /[^0]/

The inject method is faster, though, because you do not have to create
a string. I'm assuming that the "array.index((array-[0])[0])" solution
suffers from the same problem -- creating another object under the
covers ...

$ ruby tmp.rb
user system total real
inject 2.874000 0.000000 2.874000 ( 2.884000)
index 0.721000 0.000000 0.721000 ( 0.731000)
regexp 3.755000 0.000000 3.755000 ( 3.765000)

Wow! Looks like the "index" method wins hands down.

But mine is still shorter ;)

TwP
 
D

Devin Mullins

Phrogz said:
Hrm, I assumed that using #each_with_index would be speedier, since
there would be no Ruby call to the #[] method of the array for each
iteration. Instead, it's much slower:
Hrm, that is odd. I assure you it was purely by accident that I picked
the faster implementation. Maybe it has something to do with
each_with_index being implemented by Enumerable, vice Array? I dunno
why.... should just delegate to each, and keep a counter around.

?
Devin
 
P

Phrogz

Devin said:
Phrogz said:
Hrm, I assumed that using #each_with_index would be speedier, since
there would be no Ruby call to the #[] method of the array for each
iteration. Instead, it's much slower:
Hrm, that is odd. I assure you it was purely by accident that I picked
the faster implementation. Maybe it has something to do with
each_with_index being implemented by Enumerable, vice Array? I dunno
why.... should just delegate to each, and keep a counter around.

My totally uninformed guess is that it has something to do with more
than one parameter to the block, possibly creating a temporary array
for the value/index pair and then splatting them on each call.
 

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
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top