scripting – performance improvement with file open

2020-04-20 2 min read bash Learning Linux

Sometimes just one line of code can make all the difference. I will show you with example.

 

Here is script with 2 functions. Both are writing some lines of text to a file. First function, “a” I have used the redirection to write to file. Function “b”, I have opened a file descriptor with “>” before going into the loop and use that reference for writing to the file. (This concept remains same for any scripting or programming language).

 

So, in one case, it does a file open, write and then close the file and this is done for each item in loop and in other case, the file is opened, write to file in loop and close the file after loop. See the difference of performance yourself.

#!/bin/bash -
#===============================================================================
#
#          FILE: a.sh
#
#         USAGE: ./a.sh
#
#   DESCRIPTION:
#
#       OPTIONS: ---
#  REQUIREMENTS: ---
#          BUGS: ---
#         NOTES: ---
#        AUTHOR: Amit Agarwal (aka)
#  ORGANIZATION: Individual
#       CREATED: 04/17/2020 11:21
# Last modified: Fri Apr 17, 2020  11:31AM
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error

function a()
{
    for i in $(seq 1 1000000)
    do
        echo $i >> 1
    done
}

function b()
{
    exec 4>> 2
    for i in $(seq 1 1000000)
    do
        echo $i >&4
    done
}

time a
time b

 

Output:

real 10.873 user 6.252 sys 4.583 pcpu 99.6

real 6.955 user 4.622 sys 2.342 pcpu 100.12

This is very simple example but think about when you have to do this million or billion times 🙂

comments powered by Disqus