|
From: | Léa Gris |
Subject: | Re: efficient way to use matched string in variable substitution |
Date: | Tue, 24 Aug 2021 14:15:46 +0200 |
User-agent: | Telnet/1.0 [tlh] (PDP11/DEC) |
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 -- Léa Gris
[Prev in Thread] | Current Thread | [Next in Thread] |