The Computer Language Shootout

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Fri Mar 17 15:14:06 CET 2006

Kenneth Johansson wrote:
> Really I tested and could not notice any change and to me 
> that is not so strange since I can't imagine why having a 
> longer string could slow us down since it will only match the 
> first character three times and two out of them it will fail 
> on the second char. only once will it do the whole string and 
> that extra work will hardly be possible to measure.

I should clarify that the 18% improvement was on the 
seek_three() function, and not the whole benchmark.
Since the whole benchmark takes about 5 seconds on my
machine, and scanning to the right section takes some
50 ms, there are certainly other things one would
want to focus on first. This happened to be the first 
thing I saw, and it was easy enough to change.

Still, regarding your comment, ...

string:str() looks like this:

str(S, Sub) -> str(S, Sub, 1).

str([C|S], [C|Sub], I) ->
    case prefix(Sub, S) of
        true -> I;
        false -> str(S, [C|Sub], I+1)
str([_|S], Sub, I) -> str(S, Sub, I+1);
str([], _Sub, _I) -> 0.

That is, it _will_ scan the whole line any time
there isn't a match (and there are 836 non-matching
lines preceding the ">THREE" line, most of them are
60 bytes long). In other scenarios, this difference
could certainly be significant.

You could have used string:prefix(">THREE", Line) 
instead, and it would have done what you wanted.

/Ulf W

More information about the erlang-questions mailing list