First of all, I very much thank all of you who responded to my query. I've
read all of your useful and helpful replies, but I am only responding to
this final one, so as to minimize bandwidth.
"Robert Klemme" <bob.news / gmx.net> writes:
> [ ... ]
>
> require 'benchmark'
> include Benchmark
>
> str = "Hammer and tongs"
>
> bm(7) do |x|
> x.report("1") { 100000.times { str.reverse.split(//).each { |ch|
> ch } } }
> x.report("2") { 100000.times { str.reverse.split('').each { |ch|
> ch } } }
> x.report("3") { 100000.times { str.reverse.each_byte { |ch| ch.chr } } }
> x.report("4") { 100000.times { (str.length-1).downto(0) {|idx| ch =
> str[idx]} } }
> x.report("5") { 100000.times { len = str.size; for idx in 1..len ; ch =
> str[len-idx] end } }
> end
> user system total real
> 1 6.828000 0.000000 6.828000 ( 6.907000)
> 2 6.891000 0.000000 6.891000 ( 6.936000)
> 3 5.078000 0.000000 5.078000 ( 5.098000)
> 4 2.281000 0.000000 2.281000 ( 2.287000)
> 5 3.391000 0.000000 3.391000 ( 3.395000)
I have been so steeped in Ruby-ish thinking that I had completely
forgotten about array indexing. Duh!
And being so reminded, at first I would not have suspected that the
algorithms based on indexing would be faster than the others. But I
suspect that the absence of 'reverse' is what accounts for the speed
differences.
By the way, I tried this with longer strings: between 40 and 8192 bytes,
which are the most likely lengths for the strings I will be dealing
with. As the lengths increase beyond something like 50-60 bytes,
algorithm number 5 becomes faster than algorithm 4, and the difference
in speeds keeps increasing with longer strings.
But if I replace "ch.chr" in algorithm 3 and with just "ch" by itself,
that algorithm suddenly becomes the fastest. The state machine to which
I want to feed the characters can deal with integer representations as
easily as their character equivalents. So, it looks like the following
would be the fastest for my purposes:
string.reverse.each_byte {
|c|
feedToMyStateMachine(c)
}
Thanks again to all of you!
--
Lloyd Zusman
ljz / asfast.com
God bless you.