Arthur T. <art...@munged.invalid> wrote:
> I put together a quick-and-dirty time test of the methods so
>far, plus two of my own. On my machine, method 4 is best or
>next-best in time for all tested strings.
> Method 2, which was guessed to be best for long, complicated strings, is
> instead very bad for them (strings.6).
If you mean what I posted... I suggested that that method would be bad, not
best.
Your lastpos methods are an excellent idea!
I tried a slightly different main routine, but the same four METHOD1/2/3/4
routine defs, though I added a fifth, very like method3 except the '+1' is
in a different place, making that aspect more like method4... ie trying to
make just a substr/parse difference:
METHOD5:
str = strip(arg(1))
t0 = time("R")
do numloops
lastblank = lastpos(' ', str) + 1
amount = substr(str, lastblank)
end
t1 = time("R")
methtot.5 = methtot.5 + t1
return t1
My main code was based on yours:
numloops = 1e5
strings.1 = 'This is a normal test.'
strings.2 = 'This has a blank at the end. '
strings.3 = '' /* will null break algorithm? */
strings.4 = ' ' /* will single blank break algorithm? */
strings.5 = 'OneWord'
strings.6 = copies(' a',1000) 'b' /* 1001 words */
strings.7 = copies(' abc',10000) 'def' /* 10001 words */
strings.0 = 7
methtot. = 0
methtot.0 = 5
do i = 1 for strings.0
say "Numloops:" numloops
say "String" i "L="length(strings.i) "Beg:" left(strings.i,30)
say 'm1 rev rev' format(method1(strings.i),4,4) "'"amount"'"
say 'm2 word(words)' format(method2(strings.i),4,4) "'"amount"'"
say 'm3 last & substr+1' format(method3(strings.i),4,4) "'"amount"'"
say 'm4 last+1 & parse' format(method4(strings.i),4,4) "'"amount"'"
say 'm5 last+1 & substr' format(method5(strings.i),4,4) "'"amount"'"
say
end
say
say "Total times per method, numloops="numloops":"
say 'm1 rev rev' methtot.1
say 'm2 word(words)' methtot.2
say 'm3 last & substr+1' methtot.3
say 'm4 last+1 & parse' methtot.4
say 'm5 last+1 & substr' methtot.5
say
exit 0
and for example, on a pretty basic netbook, using oorexx 4.1.1, I got:
Numloops: 1E5
String 1 L=22 Beg: This is a normal test.
m1 rev rev 0.1870 'test.'
m2 word(words) 0.3750 'test.'
m3 last & substr+1 0.1880 'test.'
m4 last+1 & parse 0.1560 'test.'
m5 last+1 & substr 0.1410 'test.'
Numloops: 1E5
String 2 L=29 Beg: This has a blank at the end.
m1 rev rev 0.2660 'end.'
m2 word(words) 0.4530 'end.'
m3 last & substr+1 0.2190 'end.'
m4 last+1 & parse 0.2340 'end.'
m5 last+1 & substr 0.2820 'end.'
Numloops: 1E5
String 3 L=0 Beg:
m1 rev rev 0.1410 ''
m2 word(words) 0.5470 ''
m3 last & substr+1 0.1870 ''
m4 last+1 & parse 0.1720 ''
m5 last+1 & substr 0.1720 ''
Numloops: 1E5
String 4 L=1 Beg:
m1 rev rev 0.1720 ''
m2 word(words) 0.3280 ''
m3 last & substr+1 0.1560 ''
m4 last+1 & parse 0.1880 ''
m5 last+1 & substr 0.1720 ''
Numloops: 1E5
String 5 L=7 Beg: OneWord
m1 rev rev 0.2190 'OneWord'
m2 word(words) 0.4220 'OneWord'
m3 last & substr+1 0.2190 'OneWord'
m4 last+1 & parse 0.1870 'OneWord'
m5 last+1 & substr 0.2350 'OneWord'
Numloops: 1E5
String 6 L=2002 Beg: a a a a a a a a a a a a a a a
m1 rev rev 1.4060 'b'
m2 word(words) 7.7040 'b'
m3 last & substr+1 0.2340 'b'
m4 last+1 & parse 0.2340 'b'
m5 last+1 & substr 0.1880 'b'
Numloops: 1E5
String 7 L=40004 Beg: abc abc abc abc abc abc abc a
m1 rev rev 22.2810 'def'
m2 word(words) 70.6240 'def'
m3 last & substr+1 0.1880 'def'
m4 last+1 & parse 0.2340 'def'
m5 last+1 & substr 0.1870 'def'
Total times per method, numloops=1E5:
m1 rev rev 24.672000
m2 word(words) 80.453000
m3 last & substr+1 1.391000
m4 last+1 & parse 1.405000
m5 last+1 & substr 1.377000