miriam_e: from my drawing MoonGirl (Default)
[personal profile] miriam_e
This 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 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 string
yes "test" | head -n 100000 | tr "\n" " " ; echo
0.044 seconds
0.018 seconds (sent to /dev/null)

A tricky way to use printf
printf 'test %.0s' {1..100000} ; echo
0.190 seconds
0.164 seconds (sent to /dev/null)

printf to a variable
printf -v testvar 'test %.0s' {1..100000}
0.169 seconds
0.165 seconds (sent to /dev/null)

A slightly different way to printf to a variable
printf -v testvar '%s ' test$_{1..100000}
0.194 seconds
0. 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 seconds
0.196 seconds (sent to /dev/null)

simple {} sequence loop
for i in {1..100000} ; do echo -n "test " ; done ; echo
1.482 seconds
2.890 seconds (sent to /dev/null) Slower!!!

two simple seq loops
for i in $(seq 100000) ; do echo -n "test " ; done ; echo
1.617 seconds
2.852 seconds (sent to /dev/null) Slower!!!
for i in `seq 100000` ; do echo -n "test " ; done ; echo
1.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 ; echo
2.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 seq
seq 100000 | awk '{printf "test "}'; echo
0.769 seconds
0.055 seconds (sent to /dev/null)

awk for loop
awk 'BEGIN{for (n=0; n<100000; n++) printf "test "; print ""}'
0.880 seconds
0.045 seconds (sent to /dev/null)

awk while loop
awk -v n="100000" -v s="test " 'BEGIN { while (i++ < n) printf s}'
0.580 seconds
0.040 seconds (sent to /dev/null) Big difference!

awk (non-looping)
awk 'BEGIN {OFS="test "; NF=10000+1; print}'
0.055 seconds
0.030 seconds (sent to /dev/null)

sed and seq
seq 100000 | sed -E 's/.+/test/' | tr '\n' ' ' ; echo
0.089 seconds
0.079 seconds (sent to /dev/null)

or
seq 100000 | sed 's/.*/test/' | tr '\n' ' ' ; echo
0.096 seconds
0.089 seconds (sent to /dev/null)

or
seq 100000 | sed -z 's/[^\n]*\n/test/g' ; echo
0.173 seconds
0.154 seconds (sent to /dev/null)

sed and /dev/zero
head -c 100000 < /dev/zero | sed 's/\x0/test /g'; echo
0.058 seconds
0.035 seconds (sent to /dev/null)

perl
perl -e 'print "test " x 100000; print "\n"'
0.038 seconds
0.011 seconds (sent to /dev/null)

python3
python -c 'print ("test " * 10)'
0.062 seconds
0.039 seconds (sent to /dev/null)

repeat (C program written by Kimi AI)
repeat "test" 100000
0.032 seconds
0.006 seconds (sent to /dev/null) Wow!!!

Profile

miriam_e: from my drawing MoonGirl (Default)
miriam_e

February 2026

S M T W T F S
123 4 567
8910 11121314
15161718192021
222324 25262728

Style Credit

Expand Cut Tags

No cut tags
Page generated Wednesday, 25 February 2026 08:05 pm
Powered by Dreamwidth Studios