Newsgroup: comp.unix.programmer


Date: Wed, 26 Apr 2006 20:40:01 +0300
From: Diomidis Spinellis <dds@aueb.gr>
Organization: Athens University of Economics and Business
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.2) Gecko/20060404 SeaMonkey/1.0.1
MIME-Version: 1.0
Newsgroups: comp.unix.programmer
Subject: Re: Need guru to help inprove efficiency if possible.
References: <1146065413.454410.169380@e56g2000cwe.googlegroups.com>
In-Reply-To: <1146065413.454410.169380@e56g2000cwe.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
billy wrote:
> The following script is called 100K + times
> Can anyone of you see a way to improve its effiency with taking away
> the functionality?
> any .01 second will help
> 
> #!/usr/bin/sh
> #######################################################################
> # Purpose   : To execute and log all commands
> # Author    : Billy N. Patton
> # Original  : 09MAR06
> # Notes     :
> # Changes   :
> #######################################################################
> 
> if [ $# -lt 3 ] ; then
>   printf "Usage : $0 [-log log_file] [-proj project] [-bb BB] [-topic
> topic] command\n";
>   exit 1;
> fi
> 
> while test $# -gt 0; do
>   case "$1" in
>     -log   ) log=$2;    shift; shift;;
>     -proj  ) proj=$2;   shift; shift;;
>     -bb    ) bb=$2;     shift; shift;;
>     -topic ) topic=$2;  shift; shift;;
>     *  ) cmd=${cmd}' '$1; shift;;
>   esac
> done
> 
> ymhms=`date "+%Y%m%d%H%M%S"`;
> today=`date "+%D %T`;
> pid=$$;
> timestamp=${pid}$ymhms;
> 
> 
> ymhms=`date "+%Y%m%d%H%M%S"`;
> today=`date "+%D %T`;
> pid=$$;
> timestamp=${pid}$ymhms;
> 
> fh_both=$(
>   {
>     {
>       {
>         eval "$cmd" 3>&-
>         echo "S:$?" >&3
>       } | sed 's/^/O:/' >&3
>     } 2>&1 | sed 's/^/E:/'
>   } 3>&1
> )
> 
> status=$(printf '%s\n' "$fh_both" | sed -n 's/^S://p')
> header=$(printf "$timestamp |%3s| %10s | %10s | $today | $status | $cmd
> " $proj $bb $topic);
> fh_out=$(printf '%s\n' "$fh_both" | sed -n 's/^O://p')
> fh_err=$(printf '%s\n' "$fh_both" | sed -n 's/^E://p')
> out=$(printf "$header | stdout : $fh_out | stderr : $fh_err\n" $proj
> $bb $topic)
> printf "$out\n" >> $log;
> exit $status;

- Try to minimize the number of processes you start in the script by 
using shell builtins or batching commands together.

- For example, substitute the instances of
printf '%s\n' "$foo"
with
echo $foo

- Rewrite the script in Perl/Ruby/Python.

-- 
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality?cup



Newsgroup comp.unix.programmer contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.