I googled how one can print a text 80 times in a row.
The first hit was in python.
Then I remembered the same exists in perl. And it was 8 times faster.
Where certainly the majority is the initial program startup.
Finally I found a tricky printf solution. This is fastest because it is a built-in command.
$ time python -c 'print("xy"*80)'
xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
real 0m1.076s
user 0m0.708s
sys 0m0.368s
$ time perl -le 'print "xy"x80'
xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
real 0m0.131s
user 0m0.010s
sys 0m0.121s
$ time { printf "xy%.0s" {1..80}; echo; }
xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
real 0m0.005s
user 0m0.005s
sys 0m0.000s
2 Likes
one second for python seems a bit strange. On my Laptop (Intel i7, 8 cores), python2 takes 0.011s, python3 0.018s, perl 0.005s on average. Of course a shell builtin is faster, cause it doesn't have to call a binary.
You should compare execution times for python and perl within the source code itself (e.g.. with timeit
in python), not from shell.
In addition, a test with only 80*2 chars is not really meaningful, that should be at least 1000 or even better 100k.
Update time python2 -c ''
0.011s, time python3 -c ''
0.018s, time perl -le ''
0.005s. No differences.
$ time awk -v char='xy' -v r=80 'BEGIN{$r=OFS=char;print}'
xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
real 0m0.031s
user 0m0.000s
sys 0m0.031s
joining the 'fun'
time yes xY | head -80 |tr -d '\n'
xYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxY
real 0m0.002s
user 0m0.002s
sys 0m0.002s
time { echo xy{1..80}|tr -d '[0-9 \n]'; }
xyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxyxy
real 0m0.002s
user 0m0.003s
sys 0m0.001s
time ./repeat xY 80
xYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxY
real 0m0.001s
user 0m0.001s
sys 0m0.000s
#hand coded c
time ./rpt xY 80
xYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxYxY
real 0m0.001s
user 0m0.001s
sys 0m0.000s
Could you elaborate this part for me ?
$r=OFS=char;
My understanding: at beginning, set 80 fields for a record ($r=OFS); OFS="xy"; and simply print out the first record.
Is this correct?
Thanks!