Spliter of for...each: looping

“junk#{x}morejunk” is much faster then “junk” << x << “morejunk”

oh, FYI, both of these are faster then “junk” + x + “morejunk”

~transami

···

On Thu, 2002-07-11 at 13:22, Tom Sawyer wrote:

Seems a little cleaner/clearer, but that’s IMHO of course :slight_smile: Your
method is probably more efficient, since the one I mentioned creates
an intermediate array. I guess it depends on how performance critical
the code in question is.

actually joins faster. just tested it out. also discovered that:

thanks for all the help guys! i think i can speed up miter a bit just by
using join.

~transmi

On Thu, 2002-07-11 at 09:50, Josh Huber wrote:

Tom Sawyer transami@transami.net writes:

what i now do:
q = ‘’
arr.each_with_nindex do |x, n|
q << ‘junk’ << x << ‘morejunk’
q << ‘,’ if n != -1
end

how do you do it with join?

note: how i used to do it is probably a tad faster actually

Hmm, how about something like…

arr.collect { |x| ‘junk’ + x + ‘morejunk’ }.join(“,”)

?

Seems a little cleaner/clearer, but that’s IMHO of course :slight_smile: Your
method is probably more efficient, since the one I mentioned creates
an intermediate array. I guess it depends on how performance critical
the code in question is.


Josh Huber


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin

Speaking of “faster” – while determining “faster” by measuring
actual elapsed time might be an acceptance test of determining
“faster” … is there any way in Ruby to ask the interpreter
about its internals: particularly the number of objects
constructed throughout its lifetime?

I imagine “junk#{x}morejunk” is faster than “junk” << x << “morejunk”
which is faster than “junk” + x + “morejunk” because of the number
of intermediate objects that need to be created for each of these.
Being able to write a test that says:

total_objects_created = Kernel.total_objects_created

do some stuff

new_total_objects_created = Kernel.total_objects_created
approximate_number_of_objects_created_for_operation =
new_total_objects_created - total_objects_created

The method that has the smallest value for
approximate_number_of_objects_created I imagine would
also be the “fastest” one with regard to elapsed time.
Probably also use the least system resources.

Just another idea for adding to my Test::Unit suites …

– Dossy

···

On 2002.07.12, Tom Sawyer transami@transami.net wrote:

“junk#{x}morejunk” is much faster then “junk” << x << “morejunk”

oh, FYI, both of these are faster then “junk” + x + “morejunk”


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Tom Sawyer transami@transami.net writes:

“junk#{x}morejunk” is much faster then “junk” << x << “morejunk”

oh, FYI, both of these are faster then “junk” + x + “morejunk”

Right, this isn’t too surprising since the number of extra
intermediate objects created is vastly less with the version using
string interpolation.

In any case, the collect & join method makes it clearer (to me) what
is actually going on.

···


Josh Huber

“Tom Sawyer” wrote in

“junk#{x}morejunk” is much faster then “junk” << x << “morejunk”

oh, FYI, both of these are faster then “junk” + x + “morejunk”

If you are into speed try

‘junk’ << arr.join(‘morejunk, junk’) << ‘morejunk’

/Christoph