Diary of a B+ Grade Polymath (tcpip) wrote in linux,
Diary of a B+ Grade Polymath

Why is expansion faster than direct parsing in for loops?

Because existing knowledge is faster than research I thought I'd ask the question here.

time for i in {1..1000}; do echo "I hate Windows 10"; done
real 0m0.021s
user 0m0.011s
sys 0m0.006s

time for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24... etc 996 997 998 999 1000; do echo "I hate Windows 10"; done
real 0m0.161s
user 0m0.125s
sys 0m0.012s

To generate the numbers in the second example
printf "%d " {1..1000}
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

Because of the time it takes to parse the longer command line, most likely.
That was my thinking as well. I was wondering about the deep detail on the process of reading in a large standard input compared to a sequence expansion.