Operating System - HP-UX
1834665 Members
2073 Online
110069 Solutions
New Discussion

Performance of Shell Scripts

 
SOLVED
Go to solution
dude70_1
Advisor

Performance of Shell Scripts

Guys,

I am not an expert in unix OS. Can anybody shed info about the performance of shell
(K-shell) scripts compared to java and c programs. That will be very helpful!

Thanks.
7 REPLIES 7
Bill Hassell
Honored Contributor
Solution

Re: Performance of Shell Scripts

C programs are the most efficient since the code is completely compiled into machine language. Java is partially translated into a common base but must be run by a software engine to produce the desired activities. Shell scripts are interpreted line by line with no intermediate code generated. As such, all shell scripts have the potential to be quite slow compared to Java or C code. On the other hand, a shell script is much easier to code and test since it is just a collection of programs and some shell 'glue' to make the commands work.

It is very important to note that there is no 'right' programming language. C (and C++ and others) require a fairly steep learning curve to produce error-free programs. The code must be edited, compiled and linked, run tests and repeat. Java is at a hiogher level with precompiled modules that mean less programming effort but versioning is a nightmare. And some of the Java libraries and modules are notoriously inefficient which can take days to track down.

Shell scripts are quite easy to learn and while tracing and testing is quite easy, functions and subroutines can be clumsy to use and document. You'll find shell scripts invaluable to wrap around applications so that a consistent environment is available. Perl is the next step up in scripting. With more sophisticated structures and libraries, Perl is a great tool for more complex tasks that can be written quickly using an interpreter.


Bill Hassell, sysadmin
A. Clay Stephenson
Acclaimed Contributor

Re: Performance of Shell Scripts

There's not a whole lot that I can add to Bill's response except to say that if I were starting over, I would only learn enough shell to get by and do most of my development in Perl. Because Perl is translated to p-code, it's execution can be amazingly fast and in many cases can rival the performance of C/C++ --- especially since the majority of UNIX tasks are not CPU bound. Perl combines the best features of the shell, grep, sed, and awk --- and it's pattern matching is unrivaled. Tasks which might take hours to code in C can be done in minutes with Perl.
If it ain't broke, I can fix that.
Elmar P. Kolkman
Honored Contributor

Re: Performance of Shell Scripts

It depends on what you want which is quicker.

C is almost alway the fastest, though it needs to be programmed correctly which takes time. And the resulting program is limited to the machine(s) you compiled it for. Sometimes (especialy if it is a sysadmin tool) you need to port it to other OS's (flavour or even version) and CPU types.

JAVA is portable and has quite nice GUI's to program, though those don't produce the fastest JAVA code. Again producing fast code takes time. The speed of the resulting program depends on your time spent optimizing and the amount of memory available. And of course the efficiency of the virtual machine under which a java program runs.

K-shell scripts (though HP-UX uses POSIX shell nowadays) are mainly portable between OS-versions (sometimes minor adjustments are needed) and easy to write since the consist of the same commands you use on the command line. The speed can be good depending on what you want. Especially the time to write them, like said in Bill's explanation, is a major advantage and that is why a lot of tools are just shell scripts on must versions of Unix.

Problem with shell scripts when doing difficult stuff is that you are not only coding in the shell, but using awk, sed and a lot of other tools too. That is why nowadays a lot is written in perl, a scripting/programming language that can do all inside one tool. The shell has to start all those other commands as it reaches them, which can slow a script down.
Perl 'compiles' when starting the script, which is efficient when doing things like loops, since you don't have to compile every command again by the time the program reaches it.
Every problem has at least one solution. Only some solutions are harder to find.
dude70_1
Advisor

Re: Performance of Shell Scripts

Thanks Guys!

I'm impressed by the information!
Jean-Luc Oudart
Honored Contributor

Re: Performance of Shell Scripts

You may also find some shell compilers.
I haven't tried any of them.

May be some feedback from some other "forumer" ?

perl2exe :
http://www.indigostar.com/perl2exe.htm

shell compiler :
http://www.comeaucomputing.com/faqs/ccshfaq.html

shc :
http://www.datsi.fi.upm.es/~frosal/

Regards,
Jean-Luc
fiat lux
Massimo Bianchi
Honored Contributor

Re: Performance of Shell Scripts

Hi,
i tried shc. There is no performance improvement, it's a simple program that prevent the change to the file, by scrambling its content and adding the little code needed for the unscrambling, so it's slower.

I do not know the others....

Massimo
Bill Hassell
Honored Contributor

Re: Performance of Shell Scripts

There have been previous discussions in other forums about shell compilers and the conclusion was that there were no true compilers, just wrappers that protected the code from being read or changed.


Bill Hassell, sysadmin