[Tutor] Slightly OT - Python/Java

Alan Gauld alan.gauld at freenet.co.uk
Mon Jan 10 01:54:14 CET 2005


> I've been forcing myself to learn Java, and I was wondering if
> anyone's used Jython.
> To clarify - Jython generates Java bytecode?

I'm learning its vagaries as a way of testing objects
being written by my develoment teams in Java.

> Personally, I should've learnt Java first (although my success at
that
> without my Python experiences would've been limited.)

I don;t know why you think that would help?
Or do you mean Java before Jython? If so it depends
what you intend using Jython for!

> I find it's real nasty forcing myself to think through very
(relative
> to Python) narrow hoops to keep the Java compiler happy.

Yep, thats the horror of most 3GL languages after Python :-)

> All that stuff with typing variables - I can understand that you'd
> want to specify when the compiler needs to reserve 64 bits for a
long
> integer, but beyond that is the typing really necessary for
> performance reasons?

No, its allegedly for reliability reasons - if it compiles then
you should never get a runtime eror due to the wrong kind of
object being passed. I used to believe passionately in that
principle, now, after using Python I'm not so convinced it
matters as much as I thought. THe type conversion functions
in Java(and C++) can do funny things to data that bring their
own problems!

> (Also very frustrating is having lists that can be comprised only of
> one variable type.)

But the type can be object, which covers everything in Java...
You just need to remember to convert the type of the object
to the real class beforecalling any specific methods.

> Time to write Python programme - 3 minutes.
> Time to write Java programme - 30 minutes. (Why can't a non-static
> method comparison be called from a static reference? What does that
> mean anyway?

static means its associated with the class. non static with the
object.
The static reference doesn't know about instances.

> Runtime in Python 17.605 seconds (avg over 10 runs)
> Runtime in Java     12.302 seoncds (avg over 10 runs)
>
> So yes - if Jython compiles to Java bytecode, and gives that 5
seconds
> faster performance,

Not so simple. Jython is the Python interpreter written in Java.
So all the dynamic stuff that Python does has to be translated
into Java code which will slow things down. In practive Jython
code - even after compilation to JVM via jythonc will be slower
than both CPython and Java. The exception being if you simply
use Jython to glue together Java classes where the equivalent
CPython modules are written in Python. In this case Jython may
be slightly faster than CPython. But still slower than 100%
Java.

> but I write it in Python, and it gives me that 27
> minutes of not messing about counting my braces, I'm in heaven.

Thats the win of Jython. You can quickly write the core code,
even classes. Then compile them and use them in a HJAva program.
The slow bits can be recoded in Java once you know the design
works. The glue and non time critical classes can usually stay
in Python.

> PS I'm only learning Java because it's what they teach the COSC 1st
> year course that I plan to do.

Its worth learnoing at least one statically/strictly typed language.
There are some advantages but for me they are outweighed by
the disadvantages, but then I'm not selling commercial software
that gets benchmarked against my cometitorsin PC Magazine etc...
In that scenario every millisecond counts!

Alan G.



More information about the Tutor mailing list