writing a string made up of a smaller string repeated many times
Wednesday, 4 February 2026 05:07 pmThis was an interesting diversion today. I needed a way to construct a long string from a smaller one repeated many times, so I tried a few ways of my own devising and found many more on the net. There were some surprising ways to do this, but the really big surprises came when I decided to time them so as to work out which is the fastest. The winner is
NOTE: The time values I give below are the times to make a string repeating "test" 100,000 times, and I've added the times when sent to /dev/null instead of printing the string to the terminal.
EDIT: I have a late addition that uses perl. It surprisingly runs as fast as the yes/head/tr pipeline. I've added it at the bottom.
EDIT 2: I've found a way to use awk without the overhead of looping. It is much faster. I've added it to the other awk versions. I also asked KimiAI to help me write a C program to do the simple job of repeating a string n times. It absolutely blows all the others away, making it the fastest of all. I've added it at the bottom.
Here is my list of 20 solutions:
0.018 seconds (sent to /dev/null)
A tricky way to use
0.164 seconds (sent to /dev/null)
0.165 seconds (sent to /dev/null)
A slightly different way to
0. seconds (sent to /dev/null)
An
It requires that $nothing1 $nothing2 $nothing3 ... be non-existent variables.
0.196 seconds (sent to /dev/null)
simple
2.890 seconds (sent to /dev/null) Slower!!!
two simple
2.852 seconds (sent to /dev/null) Slower!!!
2.850 seconds (sent to /dev/null) Slower!!!
simple c-style loop
3.205 seconds (sent to /dev/null) Slower!!!
a weird one
This is impractical to use for large numbers of repeats.
0.055 seconds (sent to /dev/null)
0.045 seconds (sent to /dev/null)
0.040 seconds (sent to /dev/null) Big difference!
0.030 seconds (sent to /dev/null)
0.079 seconds (sent to /dev/null)
or
0.089 seconds (sent to /dev/null)
or
0.154 seconds (sent to /dev/null)
0.035 seconds (sent to /dev/null)
0.011 seconds (sent to /dev/null)
0.039 seconds (sent to /dev/null)
0.006 seconds (sent to /dev/null) Wow!!!
yes. That really surprised me. (Another big surprise was perl -- it ties for fastest.) The next two fastest are sed -E (extended regex) and plain sed.NOTE: The time values I give below are the times to make a string repeating "test" 100,000 times, and I've added the times when sent to /dev/null instead of printing the string to the terminal.
EDIT: I have a late addition that uses perl. It surprisingly runs as fast as the yes/head/tr pipeline. I've added it at the bottom.
EDIT 2: I've found a way to use awk without the overhead of looping. It is much faster. I've added it to the other awk versions. I also asked KimiAI to help me write a C program to do the simple job of repeating a string n times. It absolutely blows all the others away, making it the fastest of all. I've added it at the bottom.
Here is my list of 20 solutions:
yes is a command that endlessly prints out a stringyes "test" | head -n 100000 | tr "\n" " " ; echo0.044 seconds
0.018 seconds (sent to /dev/null)
A tricky way to use
printfprintf 'test %.0s' {1..100000} ; echo0.190 seconds0.164 seconds (sent to /dev/null)
printf to a variableprintf -v testvar 'test %.0s' {1..100000}0.169 seconds0.165 seconds (sent to /dev/null)
A slightly different way to
printf to a variableprintf -v testvar '%s ' test$_{1..100000}0.194 seconds0. seconds (sent to /dev/null)
An
echo {} hack.It requires that $nothing1 $nothing2 $nothing3 ... be non-existent variables.
echo test$nothing{1..100000}0.208 seconds0.196 seconds (sent to /dev/null)
simple
{} sequence loopfor i in {1..100000} ; do echo -n "test " ; done ; echo1.482 seconds2.890 seconds (sent to /dev/null) Slower!!!
two simple
seq loopsfor i in $(seq 100000) ; do echo -n "test " ; done ; echo1.617 seconds
2.852 seconds (sent to /dev/null) Slower!!!
for i in `seq 100000` ; do echo -n "test " ; done ; echo1.367 seconds
2.850 seconds (sent to /dev/null) Slower!!!
simple c-style loop
n=100000 ; for (( c=1; c<=n; c++)) ; do echo -n "test " ; done ; echo2.130 seconds
3.205 seconds (sent to /dev/null) Slower!!!
a weird one
This is impractical to use for large numbers of repeats.
echo 'test'{,,,,,,,,,}awk and seqseq 100000 | awk '{printf "test "}'; echo0.769 seconds0.055 seconds (sent to /dev/null)
awk for loopawk 'BEGIN{for (n=0; n<100000; n++) printf "test "; print ""}'0.880 seconds0.045 seconds (sent to /dev/null)
awk while loopawk -v n="100000" -v s="test " 'BEGIN { while (i++ < n) printf s}'0.580 seconds0.040 seconds (sent to /dev/null) Big difference!
awk (non-looping)awk 'BEGIN {OFS="test "; NF=10000+1; print}'0.055 seconds0.030 seconds (sent to /dev/null)
sed and seqseq 100000 | sed -E 's/.+/test/' | tr '\n' ' ' ; echo0.089 seconds
0.079 seconds (sent to /dev/null)
or
seq 100000 | sed 's/.*/test/' | tr '\n' ' ' ; echo0.096 seconds
0.089 seconds (sent to /dev/null)
or
seq 100000 | sed -z 's/[^\n]*\n/test/g' ; echo0.173 seconds
0.154 seconds (sent to /dev/null)
sed and /dev/zerohead -c 100000 < /dev/zero | sed 's/\x0/test /g'; echo0.058 seconds
0.035 seconds (sent to /dev/null)
perlperl -e 'print "test " x 100000; print "\n"'0.038 seconds
0.011 seconds (sent to /dev/null)
python3python -c 'print ("test " * 10)'0.062 seconds0.039 seconds (sent to /dev/null)
repeat (C program written by Kimi AI)repeat "test" 1000000.032 seconds
0.006 seconds (sent to /dev/null) Wow!!!