bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: efficient way to use matched string in variable substitution


From: Mike Jonkmans
Subject: Re: efficient way to use matched string in variable substitution
Date: Tue, 24 Aug 2021 15:09:55 +0200

On Tue, Aug 24, 2021 at 02:15:46PM +0200, Léa Gris wrote:
> Le 24/08/2021 à 14:06, Greg Wooledge écrivait :
> 
> > unicorn:~$ f6() { local i n=${#1} arr; for ((i=0; i<n; i++)); do 
> > arr[i]="${1:i:1}"; done; }
> > unicorn:~$ time for ((i=1; i<=10000; i++)); do f6 682390; done
> > real 0.381  user 0.381  sys 0.000
> > 
> > Looks like the efficiency of "read -ra" vs. a shell loop just about makes
> > up for the system calls used for the here string (f6 and f7 are almost
> > tied in overall speed, with f6 just a *tiny* bit faster).  Good to know.
> 
> See my featured version to also capture space and newlines:
> 
> https://stackoverflow.com/a/68907322/7939871

Clever hack, Lea.

Nice thread, funny (but slow) solutions:
        echo "abcdefg" | fold -w1
        echo "abcdefg" | grep -o .

This seems to be the fastest:
f12 () { [[ "$1" =~ ${1//?/(.)} ]]; local arr=( "${BASH_REMATCH[@]:1}" ); }
time for ((i=1; i<=10000; i++)); do f0 682390; done
real    0m0,296s
user    0m0,296s
sys     0m0,000s

Regards, Mike Jonkmans



reply via email to

[Prev in Thread] Current Thread [Next in Thread]