Python Quake 3 server rcon and query class

Once again, I couldn’t find a Quake 3 server query/rcon class for Python so I made one. This one is substantially better then the PHP Quake 3 rcon class that I made a few days ago. It’s called pyquake3. The features are:
* Simple interface
* Automatic retries
* Can access server variables
* Can send rcon commands
* Can collect player names, ping and frags.
* With an rcon password, can collect player ip addresses.

The Python Quake 3 project page is here.

A fast Python vector class

Since completing the first version of Roids, I decided to get right into optimisation. After running the Python profiler, I noticed my vector class was taking a long time in __getitem__, so I did some tests. It turns out that __getitem__ is extremely slow compared to direct attribute access. For example in some cases I was using pos[0], pos[1] instead of pos.x, pos.y. By using attribute access, it yielded approximately an 8-10 time improvement over __getitem__. I quickly removed any reference to this!

I became curious about what else I can improve, I tested the time difference between creating an old-style class and a new-style class. It turns out that new-style classes are created 1.5x faster then old-style classes.

An Old-style class looks like this:

class Vec:
    def __init__(self, x, y):
        self.x = x
        self.y = y

New-style classes look like:

class Vec(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

There was another test I did, which was using __slots__. By defining a list of attributes you will be using in your class, a new-style class will not create a __dict__ object for the class. This decreases the time it takes to create an instance of the class by a slight amount (13.6% in my tests). Here is an example:

class Vec(object):
    __slots__ = ('x','y')
    def __init__(self, x, y):
        self.x = x
        self.y = y

One final test was using a list as a base class instead of just using object. For example:

class Vec(list):
    def __mul__(self, o):
        return vec((self[0] * o, self[1] * o))
a = vec((1.0,2.5))

This was about 20% slower then my new-style class using __slot__.

Overall these changes improved the speed of Roids quite substantially. After all that, here is the fast Python vector class.

Note: These tests were run on a Pentium 4 running Windows XP with Python 2.4.2. Since I’m only testing on one platform these results may vary for you. I plan on testing these on other computers and operating systems soon.

40 hour python multiplayer game challenge

After 6 days and 40 hours I managed to make a simple multiplayer game called Roids. On the surface it looks like another Asteroids clone but it is a litte more then that. At the moment Roids has two teams with players that fly around just like the original Asteroids, but one player on each team can become a RTS player. It was written in Python so it runs a little slow and will be improved over time. The version released was 0.0.1 and I’ll be working on it for the next few weeks.