why python is slower than java?

M

Mike Meyer

Maurice LING said:
I'm wrong again. I suppose what I am trying to suggest is that design
decisions made in the last may have a longer effect than what we
consciously think. At least this is the impression I get while reading
James Gosling's argument that Java should be object-oriented from day
1 (and not added onto the language, like in C++) in The Java
Programming Environment: A white paper.

The design decisions about the syntax and grammar of the languages may
have a longer effect (thought it's not clear how much of Java can be
blamed on CPL). The design decisions about the implementation tend to
vanish every time the language is implemented, and to vanish over time
as an implementation evolves. I'd be surprised if all the 1.5.2 code
has vanished from the complete python distribution. On the other hand,
the core compiler and VM may well have been completely replaced since
then.

<mike
 
R

Roy Smith

file('sorted_autoexec.bak','w').writelines(sorted(file('autoexec.bat')))

73 chars in one line. Yeah, I cheated -- I _would_ normally put a space
after the comma, making the real total into seventyFOUR...

I understand what you're doing with the above one-liner, but I don't
think that's the best way to write it. Unless there's some over-riding
efficiency requirement, I'm always going to vote for easier to read over
fewer characters.

Chaining all those operations together into one big line makes it (IMHO)
more difficult to understand what's going on, especially since it's a
mix of method calls and global function calls. To understand the
sequence of operations, you need to read from the inside-out starting
from two different places, then put those two conceptual units together
from left-to-right.

You could refactor this in a multitude of ways, with varying levels of
compactness or verbosity, depending on how many intermediate variables
you want to use. I think I would go for something like:

inFile = file ('autoexec.bat')
outFile = file ('sorted_autoexec.bak', 'w')
outFile.writelines (sorted (inFile))

Some would say that the intermediate variables just add bulk, but I
think they provide conceptual punctuation, i.e. a place for the reader
to say, "OK, I understand that chunk, now let's see what the next chunk
does".
one can almost see you writing it with a quill dipped in
ink, at a carved oak desk, on your steam-powered computer. Charming!

Should you ever decide to move into the 21st century, though, don't
worry: Python will be there to help you do so.

I know you didn't write those lines with me in mind, but I can't help
chuckle over them. I'm tagged as a luddite by my co-workers because I
still don't have a cell phone or a palm pilot or an MP-3 player, not to
mention that I know what the program drum is used for on an 029 card
punch. I am however typing this from my wireless PowerBook :)
 
A

Alex Martelli

Roy Smith said:
I understand what you're doing with the above one-liner, but I don't
think that's the best way to write it. Unless there's some over-riding
efficiency requirement, I'm always going to vote for easier to read over
fewer characters.

I agree, and efficiency is just the same if you _do_ give names to
intermediate objects. Terseness is worthy as long as it _helps_ reading
the code, by avoiding violations of Occam's Razor (introducing entities
without necessity).

Chaining all those operations together into one big line makes it (IMHO)
more difficult to understand what's going on, especially since it's a
mix of method calls and global function calls. To understand the
sequence of operations, you need to read from the inside-out starting
from two different places, then put those two conceptual units together
from left-to-right.

It's actually right-to-left, as that's the way the inside-out order
works out. But if you read left->right, it's "to file
sorted_autoexec.bak, write lines from sorted file autoexec.bat", which
doesn't scan all THAT badly in English, I think;-). It's right-left if
you think of things HAPPENING, of IMPERATIVE code, but it's not
necessarily that way if you think of the code as descriptive;-).

You could refactor this in a multitude of ways, with varying levels of
compactness or verbosity, depending on how many intermediate variables
Right.

you want to use. I think I would go for something like:

inFile = file ('autoexec.bat')
outFile = file ('sorted_autoexec.bak', 'w')
outFile.writelines (sorted (inFile))

Some would say that the intermediate variables just add bulk, but I
think they provide conceptual punctuation, i.e. a place for the reader
to say, "OK, I understand that chunk, now let's see what the next chunk
does".

My preference here might be to have a name for the output file variable
and not for the input one, but that's a pretty thin consideration. Your
version lets one close both files explicitly, which is good.

I know you didn't write those lines with me in mind, but I can't help
chuckle over them. I'm tagged as a luddite by my co-workers because I
still don't have a cell phone or a palm pilot or an MP-3 player, not to
mention that I know what the program drum is used for on an 029 card
punch. I am however typing this from my wireless PowerBook :)

Heh -- you're a notch up from me, since the wireless Mac laptop I'm
using is a humble iBook 12";-).


Alex
 
?

=?ISO-8859-1?Q?F=E1bio?= Mendes

what about:
fat = lambda n: reduce(lambda x,y: x*y, range(1,n))
int = lambda f,a,b,dx: sum([f(x) for x in arange(a,b,dx)])*dx/(b-a)

Thoss (poorly) implements the factorial and integration function in just
one line. So what does my pet examples show us? Nothing! The same goes
for yours. So your poor affirmation about java compactness is the mere
statement that the only kind of code you can produce is autoexec.bat
sorting and the kinds. Every language has it's pros and cons, and having
a compact expressive syntax is NOT one place where Java outshines
python, ask any serious person.
 
T

Terry Hancock

My guess is the poor performance had nothing to do with the language you
wrote it in, and everything to do with the algorithms you used.

Wrong guess. ;-)

There was nothing wrong with the *algorithms* when considered apart
from the language. But considered *with* the language, they were
very bad choices.

Consider the case that you want to perform a string operation which
is not quite covered by the standard library. How do you solve that?

In C, you might very well write a function from scratch that does
what you want and nothing else. Usually this will be faster than
calling some standard library function that gets you halfway there
and then another that tries to fix the mistakes the first one made.

Not so in Python. Anything you write from scratch is going to be
interpreted, and therefore goes at a snail's pace compared to the
C code running in various built-ins and modules (consider the re
module, for example!). In fact, you might very well write a
regular expression solution that could have been done by less
impressive means in C, but would take much longer if you literally
translated the code from C to Python. The re solution will actually
do a lot more work "below the waterline" than the literally
translated version, but it won't have the loop overhead and such
to slow it down.

Generally speaking the simplest *conceptual* way to solve a problem
in Python is also the fastest.

I certainly learned this lesson. In a way, it's a good thing,
because the simplest conceptual way is probably the easiest for
a reader of your code to understand, too.

The reason why this is relevant is NOT because I'm saying "Python
is slow", but rather that it can be very slow if poorly programmed.
Thus an experienced Java programmer who tries Python may very well
find it to be "slow", since they will not necessarily make the
right choices in re-implementing a program in Python.

That's probably true for any two languages -- the one you are
most familiar with has a significant home-team advantage. ;-)

Cheers,
Terry
 
B

Bengt Richter

I am wondering the impact when IBM decided that the base memory to not
exceed 64kb, in the late 1960s...

I suppose more experienced people in this list can agree that certain
decisions made can be almost an edict. So, there is a re-building
process every now and then, hopefully to by-pass such edicts. Python
itself is already such an example.


Are the codebase of Python 1.5.2 and Java 1.1 totally replaced and
deprecated?

Lisp compiler is the 1st compiler to be created (according to the
Red-Dragon book, I think) and almost all others are created by
bootstrapping to LISP compiler. What are the implications of design
decisions made in LISP compiler then affecting our compilers today? I
don't know. I repeat myself, I DO NOT KNOW.
google is your friend. I would suggest prefixing "UIAM" to statements
or strong implications of "fact" that you are not really sure of ;-)
Perhaps you are not familiar with the various typical ways of softening
the assertiveness of statements in English? I notice several statements
above that start out looking like authoritative statements of fact, but
which you qualify in your way, yet it seems you were unsatisfied with
the effect, and added the last sentence.

"We are all ignorant, only on different subjects", as (UIAM ;-) Will Rogers
said. But here at c.l.py it is unlikely that all are totally ignorant on a
particular computer language subject, so if you post an unqualified statement
of "fact" (or slip an implicit statement of "fact" into a question, as in the
subject line of this thread ;-) that may not be so, someone will notice,
and wonder why you are doing that (since you haven't annnounced that you're
a political candidate ;-)

One reason might be language difficulties, but your English is too good
for that to jump immediately to mind.

Another reason might be forgetting that posting here is actually engaging other
human beings, and thinking of c.l.py posts as a kind of automated google
mechanism which has no feelings about how it is being used. But humans do care,
and provocative techniques of eliciting response do "provoke." Kids intent on
their immediate goals sometimes forget, but generally respond to a gentle reminder.

Another reason might be to spread disinformation for some private reason,
e.g. as a sleazy way to discredit competition. I don't think that applies here.
Or are you heavily invested in particular stocks? ;-)

Anyway, I think the best we can do is to help each other find out what the
real facts are, which requires not presenting suppositions as fact, while
recognizing that appearances are often deceiving to even the most experienced.
In that spirit, we wind up expressing doubts without bruising egos, and thanking
each other for contributions to improved understanding instead of defending against
feeling a fool for having believed something that was not so, or being annoyed
at careless spread of disinformation.

(of course, I haven't personally verified the "facts" stated in the
following sites ;-)

http://www.emacswiki.org/cgi-bin/wiki/CommonLisp
"""
The first Lisp compiler was developed by JohnMcCarthy at Dartmouth College
between 1955 and 1959. This makes it the second oldest programming language
in common use today (only Fortran is older, developed between 1954 and 1957).
"""

http://www.ibiblio.org/pub/languages/fortran/ch1-1.html
"""
This wonderful first FORTRAN compiler was designed and written from
scratch in 1954-57 by an IBM team lead by John W. Backus and staffed with
super-programmers like Sheldon F. Best, Harlan Herrick, Peter Sheridan,
Roy Nutt, Robert Nelson, Irving Ziller, Richard Goldberg, Lois Haibt
and David Sayre. By the way, Backus was also system co-designer of the
computer that run the first compiler, the IBM 704.
"""
http://www.latrobe.edu.au/philosophy/phimvt/sympas/s49hiteco.html
http://www.moorecad.com/standardpascal/pcom.pas

Regards,
Bengt Richter
 
R

Roger Binns

Alex said:
Python's dynamism at runtime is definitely one of its fortes.

We are talking about different things. Once you have written
some nice big Java "Enterprise" app, you'll see the difference.
I am not talking about the type system. It is more about how
Java applications are constructed. There are multiple JAR files,
many many properties files and the end user/adminstrator can
change what happens and plumb them together by changing the
properties files.

You could do *exactly* the same thing in Python, but people don't.
That doesn't make either language right or wrong, just that the
emphasis and what is considered "normal" is different.
Sure, since the semantics are different. Which is why we have the
makefile method of socket objects: for those occasions in which you
want signature-polymorphism at the expense of the overhead of
adaptation.

Yes. The point is that Java I/O is always done in that polymorphic
way, whereas Python doesn't. In both cases, you can do what the
other language does, but people usually don't. But it does mean
that if you want to measure I/O performance, you have to decide
if you want to do it with code optimised for speed, or what is "normal"
in each language.
Generators are a reasonably recent addition to Python, and I have no
idea what you mean by stating that Python emphasizes typing more than
Java does

I meant Python's typing mechanism ("duck typing"). Python programs
take advantage of that, just as Java programs take advantage of
the Java typing mechanism. Python's is more defined by the absence
of pervasive programmer defined typing, whereas the Java one requires
the specification of type information for all names, and interfaces
are used a lot.
Python frameworks. And factory-based design patterns are everywhere
in Python, of course; indeed, it's in Java that you see lots of 'new
ThisClass' constructs which build an instance of some hardwired
concrete class --

Java programs have *way* more factory stuff. Just a different emphasis.
Python's default makes it trivially easy to read most files in a
single gulp, so it's appropriate in many cases; Java's makes it hard
and slow to read ANY file, so it's never appropriate.

Your Java code was not representative of what an average programmer
would do in Java. In fact it was a spectacularly bad example of
Java programming. You can find bad examples of programming in
every language on the web.
Python does, but drops it during blocking I/O operations so that the
relevance should be just about the same in both cases.

Python will be worse with the penalty being based on how much concurrent
I/O is happening and how many processers the host has. This is due to
the serialisation of the Python bytecode, and non-serialisation of the
Java bytecode. My best guess that this will be a few percentage points
at a low number of concurrent I/O and probably up to 20% with
thousands.

(At this moment at work I am in fact dealing with this very thing in some
Python code and a form of proxy server).
I did observe (at some point along the substantial chain of small
benchmarks I and some other posters exchanged) that the 4:1 ratio in
runtime in favour of Python exactly matched the 4:1 ratio in
pagefaults, again in favour of Python, btw.

Page faults aren't a valid way of measuring this. You don't incur page
faults for malloced/brk memory but you do for executable pagein, and
for mmap (usually). Using executable pagein and mmap is better when
you have multiple instances of programs since they will share
system memory. Again this all comes down to what the runtime
environment is optimised for.
However, firms that choose not to release their business critical
applications as open source are likely to require at the very least a
non-disclosure agreement before they show you those sources, making it
impractical to use those sources to meet your wishes.

That is missing my point completely. If I wanted to write a ray tracer
and I could find them all over the net and in business success stories
in all languages, I would want to examine the innards of them to see
if I could do the same thing.

We have all heard the Yahoo Stores/Paul Graham/Lisp story. I don't
think many people have seen that code. Do you think you could do something
similar in Lisp? Could you write a ray tracer in Lisp? If you could
see examples of even unrelated applications, and their code, you would
have a better idea. You would be able to judge if you could achieve
it with a team of 10 average programmers. But without being able to
see the internals of at least some examples, your guess would have a
very high margin of error.
As for your latter sentence, I've never met a programmer whose default
assumption was that they would NOT be able to write code just as good
as most anybody else's.

That only applies to languages they know or know about, and in many
cases also libraries (eg SQL, Persistence, Networking, Transactions).
Any programmer who believes they can write Yahoo Stores in Lisp just
as good as what Paul Graham and co did, when they don't know Lisp, have
no experience in that type of application, have never done credit
card processing etc is kidding themselves. They may be able to
do so eventually, but it will take much time to learn. They are
unlikely to be able to accurately estimate that time at the begining.

Using another example, do you think everyone who reads this group
could just go ahead and write Zope?

Roger
 
I

Israel Raj T

Roy Smith said:
mention that I know what the program drum is used for on an 029 card
punch. I am however typing this from my wireless PowerBook :)

I hate to say this, but my kids have had 802.11g on their notebooks since
the age of 12 ( ie : nearly 2 years now).
 
R

Roy Smith

Israel Raj T said:
I hate to say this, but my kids have had 802.11g on their notebooks since
the age of 12 ( ie : nearly 2 years now).

That's OK. I once went for a job interview that really made me feel
antiquated. The kid who was interviewing me looked at my resume, saw
that I had started using Unix in 1977, and said, "Wow, you've been doing
Unix longer than I've been alive!".

The key to avoiding becoming a dinosaur is to keep learning. If you
don't learn something major (a new language, operating system, etc)
every year, it won't be too long before you start to sink into the tar
pits.
 
I

Ian Bicking

Roger said:
We are talking about different things. Once you have written
some nice big Java "Enterprise" app, you'll see the difference.
I am not talking about the type system. It is more about how
Java applications are constructed. There are multiple JAR files,
many many properties files and the end user/adminstrator can
change what happens and plumb them together by changing the
properties files.

You could do *exactly* the same thing in Python, but people don't.
That doesn't make either language right or wrong, just that the
emphasis and what is considered "normal" is different.

FWIW, this is something they are doing in Zope 3. ZCML (an XML markup)
is used to define the relationship of various components, in a role that
is probably similar to Java properties, jars, etc.
 
G

G. S. Hayes

Yes, wxPython is typically quicker than swing. It's not fair to use
this in a generic Python-vs-Java speed shootout, though, since
wxPython is just a wrapper around wxWidgets' C++ code; you're really
comparing C++ vs. Java here.

But the ease of development of extensions in other languages and the
willingness of the community to do so IS one of the key differences
between Java and Python, especially with respect to performance.
Where Java tends to be platform independent, Python tends to be
platform agnostic. In real code, people use SciPy/NumPy, wxpython,
etc because those things work for them and get the job done.

Moreover, since the GUI developer in Python is writing only in Python
(never touching C++ or needing to know about it at all), you're
basically saying that this example is unfair because Python has a
faster implementation.
 
L

Lonnie Princehouse

Mike Meyer said:
Is it unfair to compare Python dictionaries to Java HashTables because
Python is using hand-tuned C where Java is using Java? I'd say that's
a perfectly fair comparison. If that's fair, what's unfair about
comparing graphics toolkits that happen to be written in C rather than
Python (is there a graphics toolkit written in Python?).

Perhaps instead of "unfair", I should have written "not meaningful".
Because wxPython is C++ code, you can't really infer that Python is
faster than Java just because wxPython is faster than Swing. The
best meaningful assertion that can be made is that Python can be
faster than Java for GUI operations.
That's *how* interpreted languages manage to compete with compiled
languages - they perform the common operations in hand-tuned compiled
code. That people tend to write Java libraries in Java rather than C
just means they're going to get slower libraries.

Sure, but it's possible to do the same thing with Java via the JNI.
Maybe what should be compared is the relative ease of writing native
extensions (considerably easier in Python IMHO)
Java is interpreted just like Python is...

Java bytecodes are interpreted, but javac has the opportunity to do
some optimizations when compiling source into bytecodes that
fundamentally can't be done for Python because it's interpreted.
There's no equivalent of "exec" in Java. Psyco makes a valiant
attempt to do this sort of optimization (and does an excellent job!),
but it's not hard to write perfectly legal Python code that makes
Psyco explode.

[someone correct me if I'm wrong; I'm not an expert Java programmer
:p]
 
T

tutu

Maurice LING said:
3. I am not interested in the amount of time needed to develop it. But
only interested execution speed.

-If you really don't care about it, spend time developing it in C or
asambler or machine code, is it going to be faster, maybe?
-If you really don't care about it, write it in both, Java and Python
and decide for yourself about the speed.
-Do you honestly believe someone here can make those valid statements
you are waiting for to validate your unspoken reasons of this dilemma?

Here is how it goes:
No matter what others are going to try and persuade you about, you
will only make it right and probably fast enough when you will have
the experience to do it. Do you know Java better? go ahead and write
it in Java. Are you willing to learn Python and see for yourself if
the decision was right, I suggest you do that, I believe it should be
worthy. Are you afraid you are going to make the wrong decision?. You
must be mature and assume your decisions.

So, if you do have time my suggestion is:
Go ahead and try a few things in python (it seams you have some
reasons to do that already) and see if they are going to perform the
way you would like and if yes, make a plan and do it.
Success!!!

If it is not what you expected, do it in Java.
Success!!!

If you want to see reactions like the one from that Pythongoras, go
ahead and wait some more.
 
D

Dave Cook

On Windows XP, Windows NT and Linux (Gentoo ), I have consistently
found python apps using wxpython to be far faster to load
and to be far more responsive than Java apps.

I've casually compared the jython/Swing tree widget with the pygtk tree
widget, both with the same large data set, and the Swing tree widget
responds quite a bit faster than the gtk tree widget (there have been a lot
of complaints about the gtk treeviews slowness). Obviously the Swing tree
widget is much better optimized.

wxpython runs on top of gtk on Linux, but I don't know if they use the gtk
treeview as a "peer" or work around it and implement their own tree widget.

Dave Cook
 
J

Jacek Generowicz

Yes, wxPython is typically quicker than swing. It's not fair to use
this in a generic Python-vs-Java speed shootout, though, since
wxPython is just a wrapper around wxWidgets' C++ code; you're really
comparing C++ vs. Java here.

And Python's list.sort is just a wrapper around C code. So I guess
that it's not fair to use that, or any other Python built-in function,
in a Python-vs-X speed shootout. This would mean you can't use Python
classes in such a shootout either, as type.__new__ and object.__new__
are both also merely wrappers around C code ... not to mention Python
dictionaries: wrappers around C code which are used in just about
every name lookup in Python.

So, the conclusion is that it's not fair to use Python in a
Python-vs-X speed shootout.

I rather like that conclusion :)
 
L

Lonnie Princehouse

Hehe. I'm really not trying to start a flame war, honest :p
I don't even like Java.

My point was just that it's sort of misleading to say "wxPython is
faster than Swing, therefore Python tends to be faster than Java for
all things".
 
J

Jeff Shannon

Dave said:
wxpython runs on top of gtk on Linux, but I don't know if they use the gtk
treeview as a "peer" or work around it and implement their own tree widget.

Given that the express goal of wxWidgets is to use native widgets
wherever possible (a goal which results in some cross-platform
inconsistencies, but is largely responsible for its "better" (i.e.
native) look and usually-better speed), I'd think it very likely that it
does use the GTK treeview.

Jeff Shannon
Technician/Programmer
Credit International
 
A

Alex Martelli

Lonnie Princehouse said:
Java bytecodes are interpreted, but javac has the opportunity to do
some optimizations when compiling source into bytecodes that
fundamentally can't be done for Python because it's interpreted.

This assertion is, simply, wrong. The Python compiler has the
opportunity to perform plenty of optimizations, too. If it only
performs a very few of them is most definitely _NOT_ "because it's
interpreted": Python is interpreted just as much as Java is.
There's no equivalent of "exec" in Java.

....so on any function containing an 'exec', even the few key
optimizations the Python compiler does are turned off. So what?
Doesn't mean they have to be turned off in normal, sane functions which
simply to NOT use 'exec'.

The fact that optimizations are turned off is one of many excellent
reasons to avoid USING Python's exec, btw.
Psyco makes a valiant
attempt to do this sort of optimization (and does an excellent job!),
but it's not hard to write perfectly legal Python code that makes
Psyco explode.

I wouldn't be surprised if psyco had bugs: so did the first several
generations of Java JITs, even with a couple more orders of magnitude of
people slaving away on them than have worked on psyco. That has nothing
to do with "being interpreted" somehow differing between Java and
Python.


Alex
 
S

Steve Holden

Jeremy said:
"emerge sync" mostly runs an rsync command (at least on standard
installs). The python contribution to that process is minimal.

The primary slowdown with "emerge" appears to be the way the database is
stored as many thousands of little files in /usr/portage. Again, not
really python; you want to accelerate emerge, speed up your hard drive.

(They have more cached metadata now at least in the cutting-edge release,
but as that is the one I use I don't know if that is now considered
stable. It has really sped things up. A couple more iterations and Python
might actually become the bottleneck.)

Jeremy:

I just noticed your date is set a month ahead.

regards
Steve
 
A

Asun Friere

[A]sking for tolerance and patience
against _rude_ newbies which barge in with shrill, mostly unjustified,
repetitious complaints, is, I think, a rather far-fetched request.

That request is in no way far-fetched if made to someone of great
personal maturity and character. Rudeness is hardly ever an effective
response to rudeness. So I'll make it: "Please be tolerant and
patient of newbies, _especially_ the rude ones." :p Remember it's the
audience, not merely the opponent you are addressing.

Be that as it may, Maurice's original question, while arguably founded
upon a false presumption, was in no way rude. Nor were his subsequent
posts (unless I missed one). The helpfulness of this newgroup should
be (and was) one of python's selling points. Certainly I learned much
here, and especially from your posts Alex, when I first started using
the language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top