From: <bc...@wo...> - 2001-03-17 18:37:38
|
[Moved to jython-users] [John A. Tenney] >We have developed a machine controller written in Java (with real-time >portions in C++), and are looking for a good language that allows scripting >our application. We have found Jython to be great for this, in general, but >have some performance concerns. Currently, we call >"InterativeConsole.push(String)" to download our code (including functions), >line by line to the Jython environment. That's strange. And by strange, I don't mean the performance problem you are seing, but your use of the interpreter <wink>. The push() method is only intended for interactive use where the user are actually typing each line. If you already have your entire script, the PythonInterpreter.exec() and PythonInterpreter.execfile() are much faster. >Then we can invoke the functions >just by calling the function name. When I load a Jython function, does it >get interpreted to some intermediate form that executes faster, or is every >line always parsed every time it is executed? Each call to push() will attempt to compile the entire script. Many times the compilation will fail because the script isn't complete yet. The exec and execfile methods will compile the string/file only once. An example: --------------- si4.java --------------- import java.util.*; import org.python.core.*; import org.python.util.*; public class si4 { public static void main(String[] args) { PythonInterpreter interp = new PythonInterpreter(); interp.execfile("si4.py"); interp.exec("foo(42)"); interp.get("foo").__call__(new PyString("hello world")); } } --------------- si4.py --------------- def foo(value): print "now in foo. Value is", value The java program loads the script "si4.py" and all the module symbols that the script defines will be available in the PythonInterpreter namespace. Then the "foo" function is called twice. The first call uses exec which is the simplest to use but it is also the slowest. The second call to "foo" uses an explicit lookup and call and is a lot faster. >Also, we find that our functions can have no full line comments; if they do, >when we push the comment line, it interprets it as an "end of function". An artifact of using push(). >Is there any list that we can subscribe to for Jython users? The jython-users list: http://lists.sourceforge.net/lists/listinfo/jython-users regards, finn |