From: Robert W. B. <rb...@di...> - 2001-08-09 19:26:19
|
Hello Siva, On Thu, 9 Aug 2001, Siva Subramanian Somu wrote: > Hi, > I know a zillion questions would have been asked about embedding jython in > java, > but I need to know how to do this. The problem is as follows > > I have a java application which will create some number of jython > interpreters and each one > of the created interpreters will be executing a same script over and over > again. So I don't > want the interpreter to waste time parsing the script again and again. So I > want to know > is there someway for me to avoid parsing. Is there someway I could save the > parsed code > in binary form in memory and get Jython to use that? Any help regarding this > question > would be greatly appreciated. > > thanks, > Siva Option 1- write it so that you can run the script once and only apply a callable object each subsequent need. Here's Pseudo-code: interp.execfile("myscriptfile"); for (int i=0; i < 1000; i++) { interp.exec("function_in_myscriptfile()"); } This execs the file once, and subsequently only invokes a callable object defined in the script. Care with module global vars is required. Option 2- compile the script once and use the compiled bytecode for each subsequent interp.exec(). To do this, use the built-in compile function shown in the following pseudocode: //this would really be file contents instead of a PyString PyString mycode = new PyString("print 'Hello World'"); PyCode code = __builtin__.compile(mycode, "<>", "exec"); for (int i=0; i<1000; i++) { interp.exec(code); } Executing the code object is lower overhead than the string. Let me know if larger examples would be helpful. Cheers, Robert |