Last question of the day (This one's easy!)

Nolan Darilek nolan at ethereal.dhis.org
Thu Sep 30 19:13:22 EDT 1999


Ok. I'll try to refrain from asking any other newbiish questions for
awhile and let everyone recover. This one's much easier than my last
one. Promise! :)

As I'm sure several of you remember, I'm working on a MUD server in
Python. Yesterday, when I was somewhat naive about how difficult it
would be to implement a distributed server (That idea's been
scratched, BTW.) one of my concerns was the speed of an interpretted
language for this task. My initial argument for doing this project in
an interpretted language vs. a traditional language was flexibility; I
wanted the ability to drastically change the system without having to
recompile, messing with dynamic loading, etc. I also reasoned that
MUDs have been using interpretted languages for years. For example,
LPMUD uses LPC, in which most of the MUD environment is
written. (Though, admittedly, I know very little about this codebase,
since it usually isn't used to build the type of game which I want.)
Initially, I thought that if a MUD server could be written in Java
(http://www.twistedmatrix.com) a Python version would be smaller,
easily readable, etc.

Now, however, I'm becoming concerned about how possible/practical this
would be, and if I'm possibly using Python for something for which it
isn't intended. I began doing research into Python's speed vs. Java,
and had some concerns.

I don't like to pose questions to newsgroups without doing some small
level of research first. So, I started by reading the Java vs. Python
analysis at http://www.python.org/~rmasse/papers/java-python96/. I
read most of the paper, simply to have a little more firepower to
throw around in any Python vs. Java religious wars which I may be
involved in. :) What interested me, though, was the paper's comparison
between Java's and C's respective runtime speeds. The paper included
the following code to illustrate its point that Java code ran slower
than similar C code:

import java.util.Date;                                                   
                                                                         
class BenchMark {                                                        
     public static void main (String argv[]) {                           
         System.out.println("Start Time: " + new Date().toString());     
         int i;         Nothing bar;                                     
        bar = new Nothing();                                             
         for (i = 0; i < 100000000; i++)              bar.foo();         
         System.out.println("End Time: " + new Date().toString());      }
}                                                                        
                                                                         
class Nothing {                                                          
     public static void foo () {                                         
          return;     }                                                  
}                                                                        

I know that interpretted code is slower than compiled code; that isn't
my concern. I did, however, write the following (Equivilant?)
benchmark in Python:

#!/usr/bin/python
import time

def foo():
    return ""

print time.ctime(time.time())
x = 1
while x <= 100000000:
    foo()
    x = x+1
print time.ctime(time.time())

Running the compiled Java code produces the following on my PPro 200:

ethereal:~> java BenchMark
Start Time: Thu Sep 30 17:53:00 CDT 1999
End Time: Thu Sep 30 17:53:41 CDT 1999

So we see that it takes roughly 41 seconds to loop through 100000000
method calls. Not bad. I ran py_compile on the Python module though,
and here's what I get:

ethereal:~> python test.pyc
Thu Sep 30 17:56:53 1999

It is now almost 5:59, and the task is still running. So, it looks as
if Python is much slower at this particular benchmark than Java.

My first question is, should I even care? Since my program isn't going
to repeatedly call 100000000 methods in a loop, should the fact that
Python is slower at it concern me? I'm sure that there are other
benchmarks, and some are more accurate than this little "poor man's
benchmark" which I chose to run.

Second, is there a way to speed up the above Python benchmark script?
Am I doing something which is hindering its execution? Again, I did
compile to bytecode, which sped up earlier tests greatly.

Finally, and this is the question to which I'm almost afraid to see
the answer . . . Is Python appropriate for this task? I learned Python
almost a year ago, but have only recently worked with it extensively,
and I really enjoy it. When I saw the Twisted Reality MUD server (see
above URL) written in Java, I thought that it would be great to
implement my ideas in an interpretted language. There are some
features of Java which annoy me though, and I would very much like to
do this in Python. Should I just continue scaling up my work and
testing it, determining how the various components function and
optimizing them if necessary? Or, should I seriously consider
reimplementing my project in another language?

Whew! That was a long QOTD, but one of my last for awhile, unless I
need to comment on this one. :) Please understand; I'm not trying to
initiate a religious/flamewar. I have some experience with embedding
Python in other applications and extending it with additional
functionality, but I've never developed a serious server application,
and I've never really been concerned about runtime speed. So hopefully
I won't start World War III. :) And, just as an aside, it is now 6:09
and the Python benchmark is still running. Hopefully I'm just blowing
things way out of proportion, and I've gone about testing speeds in
the completely wrong way. If you've read this far, thanks a bunch! :)




More information about the Python-list mailing list