Uri said:
A> But that would give the very slow:
A> # time perl -e 'for my $i (1 .. 1000000) { $char="a"; $_="cacababaA";
A> $n= eval "tr/$char/$char/"; } print $n, "\n"; '
A> perl -e 31.11s user 0.06s system 99% cpu 31.259 total
remove the eval from the loop. create a sub with that eval tr// in it
and call that in the loop. then you should make all the benchmark
entries call subs to equalize them. tr will be much faster than the
others.
Huh...
O
I am certainly dense, but I can't figure how putting the tr// in a sub
and calling that sub would mean anything other than a longer execution
time...
(I tried, but certainly not the way you expected ;o)
In the first question of this thread, it was asked whether a faster
method existed for counting character occurences in a string (other than
splitting the string and looking through the array).
For me, timing the speed of a given method is timing everything that is
involved, and for the sake of reading a meaningful value, looping around
the piece of code that is involved. For example, if it takes, 30ms to
complete and my expected accuracy is about a second, I need to perform
100 to 1000 loops before I can expect a "reasonably accurate" value for
the execution time.
Down to this particular problem, if using tr/// means using an eval
around it in order to get interpolation, so be it...
Suppose the content of the string or the char (that we are trying to
count) come to change, eval should be called each time: IMHO, this
overhead should be part of the timing (some sort of worst case).
But feel free to contradict me
)
A> So, s//g would be a much faster way to count the occurences of
A> $char... or am I still stupid?
}
not stupid, just not familiar with how to isolate the thing you really
want to benchmark. and there are many benchmark techniques and also ways
to speed up code like tr// with intrpolation (caching code refs
generated by eval, etc.) so you don't call eval each time.
I fail to see how we could cache "code refs generated by eval" (whatever
that could be, I am no Perl Guru ;o) if the string (in which we are
looking for the character) and the character itself change...
eval is the killer in that loop.
That, I had guessed...
)