Hello, On Wed, 07 Dec 2011, David C. Rankin wrote:
On 12/03/2011 12:36 PM, Martti Laaksonen wrote:
[[ ${#str} -gt ${lim} ]]&& { begin=${str:0:$((lim-rem))}; end=${str:$((lim-rem)):${rem}}; echo ${begin}${end//?/${char}}; } || echo "${str}"
OK,
That is pretty darn slick! Thanks Martti. I wouldn't have thought about string substitution using a single char wildcard as the substring no matter how many times I looked at ${string//substring/replacement} :)
Me too. At least at the time.
Hmm... elm.sh=Martti's way; eld.sh=David's way (first run nothing in cache):
10:15 providence:~/scr/dev> time sh elm.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++
real 0m0.010s user 0m0.003s sys 0m0.003s
10:17 providence:~/scr/dev> time sh eld.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++
real 0m0.011s user 0m0.007s sys 0m0.003s
Martti's wins -- now let's run it a second time with the files in cache:
That's noise. That's basically the startup time of the shell. And BTW: both versions use bash-specific stuff (I think, you should use 'bash'). ==== If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. ====
10:17 providence:~/scr/dev> time sh elm.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++
real 0m0.010s user 0m0.007s sys 0m0.003s
10:19 providence:~/scr/dev> time sh eld.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++
real 0m0.010s user 0m0.003s sys 0m0.003s
Now -- I just don't get it -- results are opposite. (although the times are both so small as to be in the noise) Oh well, I still like the elegance of the string substitution that eliminates the for loop.
Same again. "Noise". Rule of thumb: anything running under ~10s is not reliable. Note the "user" times in above 4 examples, they're a clear indication of the measurement being "noise".
OK, what about 100 times:
time for ((i=0;i<100;i++)); do sh elm.sh my-very-long-string-of-stuff 24 4 +; done
real 0m1.021s user 0m0.587s sys 0m0.220s
time for ((i=0;i<100;i++)); do sh eld.sh my-very-long-string-of-stuff 24 4 +; done
real 0m1.005s user 0m0.577s sys 0m0.220s
Still basically noise. But the explanation is easy: both versions use shell-builtins only, and you can bet that the ${v//p/r} internally expands to some loop, with the substition done inside the loop. With the loop version, you have a truncation, an explicit loop and simple concatenations inside the loop, which _might_ be faster than the substitutions. Please run again (outer-) looping (say 2000 times instead of 100?) so often, that the faster one is over 10s running time, but that's only out of academical interest ;) -dnh, BTW: one more thing I like about perl: it has the "Benchmark" module where you can e.g. compare stuff, with a sane default minimum running time ;) -- Wow, I'm being shot at from both sides. That means I *must* be right. :-) -- Larry Wall in <199710211959.MAA18990@wall.org> -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org