• + 1 comment

    Sorry for late.

    head -n 22 | tail -n 11 is faster than sed because of multi thread by pipe processing. But head-tail is longer code and spending more CPU resource. (head -num and tail -num are deoprecated option in POSIX.)

    :
    ## \file      time-middle-of-text.sh
    
    yes | head -n 30 >tmp
    
    echo "sed -n '12,22p' tmp >/dev/null"
    time -p sh <<-'EOT'
    for i in $(yes | head -n 1000); do
    	sed -n '12,22p' tmp >/dev/null
    done
    EOT
    
    echo "head -n 22 tmp | tail -n 11 >/dev/null"
    time -p sh <<-'EOT'
    for i in $(yes | head -n 1000); do
    	head -n 22 tmp | tail -n 11 >/dev/null
    done
    EOT
    
    rm -f tmp
    

    Result:

    sed -n '12,22p' tmp >/dev/null
    real 1.14
    user 0.02
    sys 0.07
    head -n 22 tmp | tail -n 11 >/dev/null
    real 1.02
    user 0.01
    sys 0.12