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

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

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded 

  • 3 comments