[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Our Most Precious Resource: Programmer Time (was Re: long term goals)



[ Gustavo Noronha Silva writes ]
> Hello, I'm a new programmer and also a 'will be new developer'...
> A thing I've wondering is: perl and even shell scripts, or what ever
> like this, seem to be very much slower than any program written in
> C. 
> 
> I think C programs would make the debian package management
> system a lot better and faster ( I could not install packages in
> my 486 without having a headache =( )

Writing things in directly compiled code doesn't always make things faster.
Contrariwise, writing it in perl or (k/ba)sh doesnt always make it
drastically slower.
Okay, it often does :-) But oftentimes, there are optimizations that can be
done to the shellscript to make it faster, rather than recoding the whole
thing.

If you find some of the package things slow... I suggest you take this
valuable opportunity to learn how to optimize scripts. Play with them and
see what you can come up with.

Here are the most useful ways to optimize shellscript speed are:

1. Singleshot external programs, with internal stuff.
  For example, in POSIX shell, instead of using
  val=`expr $var1 + var2`

  you can use the *built-in* math functions, to do
    val=$(($var1 + $var2))

  Similarly, replace
     if [ xxx ]
  with
     if [[ xxx ]]

  The first is EXTERNAL. the second is built-in to POSIX-sh

2. replace step-by-step I/O with external functions
  Funny how that seems to contradict, eh? :-)
  But if you have a big old loop like
    while true ; do
       read xyz
       set $xyz
         do stuff with vars
    done

  it can often be speeded up drastically by rewriting that loop in AWK.
  That's what awk is for.
  

3. look over the entire algorithm, and get rid of inefficiencies.
   For example, if the script checks the process table a lot, it is
   probably okay, if you take a SINGLE snapshot of "ps aux" and put it in
   /tmp, instead of running "ps aux" every time. That's a high-cost
   program.




Reply to: