Effect on speed of comments in bash script loops?

Chris F.A. Johnson chris-E7bvbYbpR6jSUeElwK9/Pw at public.gmane.org
Thu Nov 15 21:18:09 UTC 2012


On Thu, 15 Nov 2012, Walter Dnes wrote:

>  Assume a bash script crunching through a large text file, with an
> inner loop that gets called a lot.  I like to heavily comment complex
> scripts, so 6 months later I don't have to spend days trying to figure
> out what they do.  Are the comments parsed+discarded on each pass
> through the loop?  If necessary, I could keep a commented "master"
> script and an uncommented "working" script, like so...
>
> echo '#!/bin/bash' > working
> grep -v "^#" master >> working
> chmod 744 working
>
>  The working script would execute the same as the commented master
> script, but possibly faster.

     Crunching through a large text file with a loop is inefficient to
     begin with. Comments are not going to make any difference.

     The following two loops execute with no more than a millisecond
     difference between them -- and sometimes the commented version is
     the faster. Other goings on in your computer will make more
     difference than the comments.

time for n in {1..10000}
do
   :
   :
   :
done

time for n in {1..10000}
do
   : ### commented
   : ### commented
   : ### commented
done

-- 
    Chris F.A. Johnson, <http://cfajohnson.com/>
    Author:
    Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
    Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list