Why is search & replace parameter expansion much slower in bash than in zsh?

Viewed 107

I'm solving Advent of Code in bash/zsh and I'm reading today's input (±2000).

The input is separated by double-newlines (real input is ±2000 lines):


vjbzaqs
bovlts

d
r
r
p
d

erxwkjlv
jpoefrlx
kjrwxeld

Since IFS is single character I want to replace 2 newlines with a tab (so I can set IFS to tab and iterate the input):

input="$(cat advent-6.csv)"
input_new=${input//$'\n\n'/$'\t'}
echo $input_new

In zshell this is instant:

➜  bash-game time zsh ./advent-6.sh
./advent-6.sh  0.01s user 0.01s system 80% cpu 0.016 total

However, in bash this takes ~42 seconds:

➜  bash-game time bash ./advent-6.sh
bash ./advent-6.sh  42.52s user 0.04s system 99% cpu 42.590 total

Why is zsh performing so much better at search & replace parameter expansion than bash?

What's happening here?

(Note: I realise there are a million other ways to do this with sed/awk, I don't need help with replacing two newlines with tabs - if you are interested in doing this here is an answer mentioning several ways - I care about why they're different)

0 Answers
Related