Operating System - HP-UX
1821584 Members
3414 Online
109633 Solutions
New Discussion юеВ

Performance issues -- Perl vs. ksh-93.

 
SOLVED
Go to solution
Leslie Chaim
Regular Advisor

Performance issues -- Perl vs. ksh-93.

Greetings Perl Gurus,

We all know about the joys of Perl but I have to defend the following argument:

"For my money ksh-93 beats Perl for performance, readability, and usability."

I believe that this person is not a Perl expert. Nevertheless, he is highly intelligent and I need some help to convince him(and myself:).

Thanks,
Leslie
If life serves you lemons, make lemonade
11 REPLIES 11
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Performance issues -- Perl vs. ksh-93.

Try to convince him with the time you *save* in developing your scripts. Added to the portability, and extenability.

perl seldom wins in actual execution time, unless you do highly advanced stuff in connecting different databases and such in which perl is virtuously unbeatable. Execution time should not be the issue. Development time should.

One *HUGE* advantage of perl over ksh is that you can chage (long running) scripts *while* they run, because perl reads the script before it executes. Try that with ksh, and you get a nasty surprise.

FWIW, I think that perl beats ksh in readability, usability, and documentability
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Performance issues -- Perl vs. ksh-93.

For performance, a rather simple test would be to a loop that simply increments by one to 10000. Perl does this about 10 times faster than the equivalent shell even using internal arithmetic and tests. i.e. using "while [[" rather than "while [" and using "$((" rather than expr for the addition. In this simple test, perl was literally an order of magnitude faster. This is not really surprising since Perl translates to very efficient p-code. In fact, I've often seen Perl do operations almost as fast as the equivalent C.

This is really not a fair comparison because to do this comparison you really have to change your statement to something like: "For my money ksh + awk + sed + tr + cut ... beats Perl."

I will concede that Perl can be written to appear to be almost gibberish and still function well. Some Perl programmers consider this a wonderful trait; I do not. There is nothing like coming back to some code written six months ago and trying to figure out what this incredibly cute and powerful one-liner does. Perl does not have to be obscure. I tend to write very clear code because I know I might have to change it someday.

You are also ignoring one extremely good feature of Perl - portability. Many of my Perl scripts can run completely unchanged on Windows as well as other platforms.

If it ain't broke, I can fix that.
Gregory Fruth
Esteemed Contributor

Re: Performance issues -- Perl vs. ksh-93.

Perl is often accused of being unreadable, but
I feel this is undeserved. Any language can be
abused. How about this common nugget of ksh:

export FILE=$HOME/.envfile
export ENV='${FILE[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'

When I write Perl the code is virtually
indistinguishable from C code. I also avoid some
of the shortcuts that often cause unreadability
in Perl.

As for usability, I concede that Perl is not a very
good interactive shell (base Perl anyway, there's
probably an extension module that provides an
interactive shell). However, Perl has an excellent
debugger, while AFAIK ksh only has kshdb, which
is rather primitive and is a user-contributed item
from the O'Reilly ksh book (Rosenblatt). For
complex programs printf just isn't enough for
debugging.

As for performance, I suspect Perl will be faster
because it is essentially a compiled language
rather than an interpreted one. It also goes
through an optimization phase before it is
run.

Also, for complex operations ksh will probably
have to spawn external programs like awk, grep,
etc. while these can often be done internally in
Perl, which will be much faster.


dirk dierickx
Honored Contributor

Re: Performance issues -- Perl vs. ksh-93.

Depends on what your guy wants to do with it, but normally you will notice a perl script gains in speed the longer your script gets (certainly if there are some itterations in it). every time you call on a command in a shell script, the shell has to run this command every time, while perl only loads once.

The readability might not always be easy to grasp for someone who is not used to perl. But that is just a side effect of being a beginner. just like for a beginner it's hard to understand every shell script. the message is always the same, you can make things as complicated as you want/can.

Perl is also capable of doing advanced stuff which you can not possibly do with shell scripting (most of which you don't need for system maintenance tasks, but still...)
Leslie Chaim
Regular Advisor

Re: Performance issues -- Perl vs. ksh-93.

Thanks so much for all your replies. This should be convincing. Personally, I agree that Perl statements can be gibberish, but that's why you have to *really* learn it. Aside from the basic syntax, I can think of three major step one must to *master* Perl:

1) Understand the implicit contexts.
2) Get a strong grip on Perl's concise data structure handling. (Hash, array, refs, and all possible combinations)
3) Master regex -- the Perl way. There's a lot to be fitted here, I believe that this is the most important step.

What are your thoughts?

BTW, and let's try to argue for the sake this 'guy'...:) What are the situations where you *should* use the Shell?
If life serves you lemons, make lemonade
H.Merijn Brand (procura
Honored Contributor

Re: Performance issues -- Perl vs. ksh-93.

When to use a shell:

?? When it can be done in two lines of less than 78 characters
?? When perl is not available
?? When the script is short and is to be called from cron or at
?? When there is a lot of simple interactivity (though very easy to do in perl, it is most likely that (beginner) scripters read things easier in shell

For perl

?? Understand perldoc
?? Understand CPAN (and *use* it)
. Understand basic flow control. I mean that perl has some control structures that other languages lack:

if ($expression) { action () }
$expression and action ();
action () if ($expression);

are all the same

And for long(term) projects:

?? Understand modules
?? Understand POD

Don't be afraid to use the perl newsgroups and forums. Perl people tend to be very helpful
Enjoy, Have FUN! H.Merijn
Gregory Fruth
Esteemed Contributor

Re: Performance issues -- Perl vs. ksh-93.

I use ksh for wrapper scripts around
existing commands. For example, if
you have to set environment variables
before you run a program, a wrapper is
the perfect way to do it. You could tell
users they have to set up the env vars
in their .profile or .login files, but I prefer
to do it automatically because users
can't be trusted. You could also set the
vars up globally, but I don't want to pollute
everybody's environment with stuff they
don't need. Another reason for wrapper
scripts is to do clean up after the process
completes. For example, the version of
Matlab we have here doesn't exit cleanly
on HP-UX, so after you quit you
sometimes have to kill leftover processes.
(Actually, the other admin wrote that script
in Perl; if it were me I'd have written it in
ksh.)

Critical admin scripts should probably be
written in ksh or POSIX sh (and use the
version of those shells on the root filesystem).
Perl is probably in the /usr or /opt filesystems,
so if you're in single user or LV maintenance
modes Perl may not be available. (I prefer not
to install Perl in /.) The same is true for scripts
in the startup and shutdown sequences.
Leslie Chaim
Regular Advisor

Re: Performance issues -- Perl vs. ksh-93.

For completeness, here is the complete story:)

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=13&t=000575

And some nice words from one of the Moderators of that forum.

If life serves you lemons, make lemonade
H.Merijn Brand (procura
Honored Contributor

Re: Performance issues -- Perl vs. ksh-93.

Thanks for sharing and feedback. Fun to read that thread.

FWIW I'll stick to perl if you don't mind :)
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Performance issues -- Perl vs. ksh-93.

Leslie,

Some questions for DK or ME:

(1) How would one "extend" KSH-93? Would it be as simple as perl :-?

(2) Can a shell script in ksh be compiled into a run time program :-?

(3) Is there an awk to ksh converter :-?

(4) And what does one do without Data::Dumper ??

Of course, my favorite feature in perl is: hash of hashes.

I have converted hundreds of ksh scripts to perl, and I have NEVER found a single case where ksh was faster than perl. I have seen ksh scripts literally beat the crap out of machines, and when converted to perl they behave very well. Now, if someone has an example of where ksh is faster than perl, I'd like to see the two pieces of code!


live free or die
harry
Live Free or Die
Leslie Chaim
Regular Advisor

Re: Performance issues -- Perl vs. ksh-93.

And all the folks shout:

Harry, Harry, Harry, Harry:)

I could not have done it better myself, and there are plenty of uses for Perl before we go to "hash of hashes". The regex flavor and the command line interface is just mind-boggling.

There is one thing where awk has the edge over Perl, (unless this was changed in 5.8, please let me know if it did): The -F switch and the $/ (RS variable), awk allows a full fledged regex and Perl doesn't:(

BTW, I recently came across this link:
http://perl.plover.com/qotw/
You may want to subscribe, I did.

The most amazing quizzes that you can think of in Perl. I challenge anyone to take any Perl solution and re-write it in C or (horrors) Java.

Thanks Harry!
If life serves you lemons, make lemonade