|
From: john c. <joh...@ya...> - 2001-08-08 18:52:01
|
Hello Michael.
What is a skeleton?
In much the same way a skeleton supports our body, a
skeleton java class will support the native python
class. Python is amorphous and highly dynamic. With
this flexibility there comes a penalty, lower speed.
For most things, speed is not important but for other
things is it.
A skeleton gives our Python class a backbone.
Even-though we can bend our bodies in many different
shapes, our skeleton prevents us from turning inside
out.
A skeleton is a set of attributes and or methods
implemented in the native java backbone. They should
in theory do exactly the same thing as they do in the
python class (ghosting). Of course this is up to the
programmer to verify by use of a good test suite.
Because of the way the Jython interpreter works (and
my implementation), Jython has absolutely no way of
knowing the difference between values stored in the
the java skeleton or the native Python object.
The benefit we get from this is that computationally
intensive algorithms could be ported to native java
while preserving the Python and Jython
interchangeability. We implement that functionality
in the true python class anyway but if a skeleton
class exists, search the skeleton first for that
attribute.
Things are faster because:
A) the skeleton class is native java.
B) data is already available in native java form
The nice thing about it is that the Python class
should still work without the backbone. The skeleton
has access to the data in native java form without
going through api layers. We use the skeleton to
address efficiency issues.
Speed boost is enormous. For a example, here are some
numbers for a simple program:
from time import time
def test():
start = time()
total = 0.0
for i in range(0,1000000):
total = total + i + i*2.0
print time()- start
This test on my computer:
python2.0: 2.77 seconds
jython2.1a1: 7.40 seconds
native java: 0.014 seconds
Native java is:
* 197 times faster than Python
* 528 times faster than Jython
# perhaps I should have used xrange (o:
A big light bulb went off. I needed a way to harness
this power without sacrificing too much python
flexibility.
Here is an example of how they might be used together.
#a python class
class Spam:
def __init__(self,aFloat,bInteger,cString):
self.a = aFloat
self.b = bInteger
self.c = cString
//java Skeleton class
public class SpamSkeleton extends PyObject {
private double a;
private int b;
private String c;
public void set_c(PyString ob) {
c=ob.internedString();
}
public String get_c() {
return c;
}
public void set_b (PyInteger ob) {
b=ob.getValue();
}
public int get_b () {
return b;
}
public void set_a (PyFloat ob) {
a = ob.getValue();
}
public double get_a() {
return a;
}
}
The concept is still in its conceptual stages.
from Spam import Spam
import SpamSkeleton
a=Spam(__skeleton__=SpamSkeleton())
I intercept the __skeleton__ keyword in the
constructor in PyInstance. So even though it is not
defined in the __init__ function of Spam it will work.
To make this code portable:
from Spam import Spam
import sys
if sys.platform == "java":
import SpamSkeleton
a=Spam(__skeleton__=SpamSkeleton())
else:
a=Spam()
Even though in the class self.a is defined, because
the skeleton has this attribute, it is never stored in
the PyInstance but instead the skeleton instance via
the getter and setter methods.
Thats all for now. To really utilize the power I must
implement functionality that will allow nesting. So a
list of objects with the restriction that they are of
the same class will becomes a list of skeletons in the
native java skeleton. That's where the real
performance boost will be.
Will have something whipped up by next week with some
hard numbers for performance comparisons.
have fun,
John Coppola
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/
|