[Tutor] Sun says: Don't use Java, use Python!

Erik Price erikprice@mac.com
Tue Feb 11 07:25:02 2003


On Tuesday, February 11, 2003, at 01:22  AM, Paul Tremblay wrote:

> However, the new modular design is 3 times as slow! It took 50 seconds
> to parse a dense file on a slow, 100 MHZ computer. The new modular
> design took 2 minutes and 20 seconds.
>
> Some of the speed factors could definitely be due to my inexperience.
> Keep in mind that these two comparisons are both with perl scripts. I
> don't know how Python will fare as far as speed is concerned.

I'm not a seasoned Python programmer, but I think that each language 
has certain features that can be optimized -- in other words, what is 
fast in Perl might not be fast in Python, or vice versa.  This list is 
a good place to ask about what might be bottlenecks in Python so you 
can look for them in your code.  (But not me, since I really don't 
know.)

> My reason for converting the script to Python is:
>
> --Python is so darn elegant and easy to read
>
> --Since it is so easy to read, others are more likely to take an
> interest in the project and help out, or simply use it.

Have you posted the script on sourceforge or the like?  It is a good 
place to advertise it.  Especially if it's written in Python -- I'd be 
more inclined to check out a Python script than a Perl script simply 
for the reason you give, that it's very easy to read.  Of course, I've 
seen Python scripts that are still difficult to read, and Perl scripts 
that are easy.  (For instance, just about any Perl script written by 
Randal Schwartz tends to be very easy to follow, but he is also a paid 
professional Perl instructor so I should hope so.)

> --My reasoning for java is that Java is much faster than perl or 
> python.

Hmm... I can't seem to get a hard and fast answer on this myself.  C 
could be much faster than Perl or Python (depending on the 
circumstances) but both this list and a Perl mailing list that I am 
subscribed to have been discussing the controversial document from Sun, 
which now I'm hearing might not even be legit.  Either way, it might be 
worth rewriting in Java for two reasons:  1. To learn more about Java, 
2. to see if Java really is any faster.  One thing for sure, the actual 
process of *writing* Java is slower than Python or Perl.

If you're really considering re-writing the code with Java, bear in 
mind that Java will force you to write it in full object oriented 
style.  Since you are just now working on a Python version and Python 
allows this, you may wish to try doing something like this in Python.  
It will be easier to move your program to Java later if it is already 
laid out as a collection of objects and relationships between objects.

> Of course, speed can be very tricky. Perl is faster at regexp than any
> language. However, most of the script reads in one line at a time and
> uses the substr function in perl, which is the same as slicing in
> Python. I believe that Java would give the user a huge speed increase.

It might.

> However, this thread originally asserted that java was *not* that much
> faster. In which case, no need to learn and struggle with java!

Java is a pretty big language.  There is a lot involved in learning it. 
  If you're only interested in amping out the program's performance, I 
don't really recommend learning Java just for that, since the speed 
advantage of Java isn't very well-defined -- C would be a better choice 
if you want something fast.  The problem [that I have] with C is that 
it's very hard to program [to me].  You have to do a lot of the dirty 
work yourself, like managing strings as arrays of characters, and 
memory management.  It might not be worth it, but that's entirely up to 
you.

> I agree with your statements about java forcing a programmer to uses
> classes. I noticed this too, even though I know hardly any java. I 
> guess
> that accounts for my attraction to Python--it allows you to break code
> into simple re-usable chunks.

Perl and Python and, I think just about every language, let you break 
code up into chunks.  But I think that because Perl and Python are so 
"natural" to write, it can be hard to stop yourself from just busting 
out the code, and think about making a modular design.  You can just 
jump in and before you know it, you've got hundreds of lines of 
spaghetti.  I strongly agree with Jeff Shannon, you might want to stop 
and think about the different aspects of your program.

One way to do this, that I first heard about from Alan Gauld on this 
very list, is to make a list of the different features that your 
program does -- then write each feature down on a separate index card, 
and make a list of the responsibilities of each feature that it needs 
to be responsible for in order to perform its role as a feature.  After 
you've got every responsibility of your program divided up into the 
various features, you may have a decent idea of what classes you should 
use (the features would ideally match a class somehow), and the 
responsibilities can probably be easily translated into methods.  It's 
called CRC, or Class Responsibility Cards.  Hopefully it will be 
helpful to you.


Erik




-- 
Erik Price

email: erikprice@mac.com
jabber: erikprice@jabber.org