Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Fastest Scripting Engine  (Read 3788 times)
0 Members and 1 Guest are viewing this topic.
Offline roland
« Posted 2011-11-02 03:27:02 »

Hi, I've read some posts about fast scripting in java, but I'm still confused about which one I should use.
Right now I'm using javax.script, and it is really slow.

My game heavily relies on scripting. Each player, object, map has atleast 1 script if not more executing each frame.
When I put in 200 players, (I know it's a lot, but it is what I'm aiming for), switching between scripting and not makes my game go from 200fps to 10fps. (nothing else is changed)

I want to be able to have scripts that pretty much run as fast as java does.
Is this possible, and which one should I use?

I also heard about janino. Could I use this?

Thanks very much,
roland
Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2011-11-02 04:52:20 »

javax.script is a generic interface to any number of scripting languages.  Are you using javascript?  The JS that java comes with is Rhino, which isn't stunningly fast, but it shouldn't be THAT slow unless either your scripts are overly complex or you're invoking it wrong.

If you want code to run as fast as java, it's hard to beat java itself.  Janino can compile .class files that can be loaded normally and JIT'd like any other java, but it's not as fast as current versions of javac, and not even close to eclipse's ejc.

Another alternative is Groovy++, which is supposed to be extremely fast, and has a javax.script interface.
Offline roland
« Reply #2 - Posted 2011-11-02 06:26:57 »

Thanks for the reply, sproingie.

I am using javascript, and my scripts aren't that complex. I could be invoking it wrong, I'm pretty new to javax.script.
I followed this tutorial: http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
and made a simple script class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
package util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
//import javax.swing.JOptionPane;

import file.FileManager;

public class Script
{
   ScriptEngineManager mgr;
   ScriptEngine jsEngine;
   Invocable invocableEngine;
   String m_script;
   
   public Script(String _strFile)
   {
      mgr = new ScriptEngineManager();
      jsEngine = mgr.getEngineByName("JavaScript");
      m_script = "";
     
      invocableEngine = (Invocable)jsEngine;
     

      m_script = FileManager.LoadFileToString(_strFile);
   }
   
   public void ExecuteFunction(String _strName, Object... args)
   {
      try
      {
         invocableEngine.invokeFunction(_strName,args);
      }
      catch(Exception e)
      {
         e.printStackTrace();
         //JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
      }
   }
   
   public void ExecuteScript()
   {
      try
      {
         jsEngine.eval(m_script);
      }
      catch (ScriptException ex)
      {
         ex.printStackTrace();
      }  
   }

   public void AddObject(String _strName, Object object)
   {
      jsEngine.put(_strName, object);
   }
}


Every single player has a Script object (Is this bad? I have to load the same file 200 times which is not good, although I want the variables inside the script to be separate for each player)
Also I have ScriptEngineManagers for each script. Can I just have one? and one engine?

I could have a look at Groovy++. I don't especially want to learn a whole new language, JS was nice.
Thanks again
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2011-11-02 06:34:54 »

Janino is easy to use if you want to use Java as your scripting language.  It's hard to give advice on scripting language questions without knowing how you want to use it.
Offline roland
« Reply #4 - Posted 2011-11-02 08:45:24 »

Janino is easy to use if you want to use Java as your scripting language.  It's hard to give advice on scripting language questions without knowing how you want to use it.

I tried out Janino and it seems to be a lot faster. And I would like to use java as my scripting language.  Smiley
Is this the best solution?

How I want to use it? It's hard to explain. I need the script so that I can change how my player moves and responds to things, and scripts for AI calculation, etc.
Offline roland
« Reply #5 - Posted 2011-11-02 09:11:45 »

Just a test I did:

javax.script "JavaScript":

1  
2  
3  
4  
5  
function process()
{
   for (var i = 0; i < 1000000; i++)
      Math.sin(i);
}


 754 ms average

Janino:

1  
2  
for (int i = 0; i < 1000000; i++) 
      Math.sin(i);


0.75 ms average (1000 times faster)


Is this accurate?
Offline princec

« JGO Spiffy Duke »


Medals: 429
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #6 - Posted 2011-11-02 09:59:20 »

No, the JVM probably just deleted the entire code as it doesn't do anything. Try summing the results and outputting them at the end, which will give you a more accurate view*

Cas Smiley

* of a microbenchmark, so largely meaningless

Offline Roquen
« Reply #7 - Posted 2011-11-02 10:16:29 »

I didn't word my question very clearly.  By "how" I really meant, does java's programming model fit what you want to do?

WRT: The micro-benchmark.  This is poorly formed even if you take princec's suggestion and make it sum and use the result.  The cost of a trig function vastly outweight the relatively cheap induction variable updating/summation operations.  Janino will result in code that will run as fast as if not scripted, once the set-up cost (converting to bytecode) has completed.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #8 - Posted 2011-11-03 00:45:59 »

That Script class looks about right.  I take it you're only calling ExecuteScript once, then invoking a known entry point every frame through ExecuteFunction?  If so, I'm not sure about the slowdown -- you might try a profiler and see if it's really taking all the time in ExecuteFunction or not.

No matter how fast you get it, executing scripts every frame is going to be a potential drain.  You might want to only execute scripts if they're triggered by some event, so you only have to check the trigger every frame.

I know you don't want to switch languages, but if you don't find some obvious bottleneck, it may very well be Rhino that's the problem.  Groovy (and especially groovy++) has made really impressive strides in performance lately, meanwhile the Rhino engine in java has quite literally not changed at all.
Offline roland
« Reply #9 - Posted 2011-11-03 02:56:53 »

Thanks for the replies everyone.
That Script class looks about right.  I take it you're only calling ExecuteScript once, then invoking a known entry point every frame through ExecuteFunction?  If so, I'm not sure about the slowdown -- you might try a profiler and see if it's really taking all the time in ExecuteFunction or not.

No matter how fast you get it, executing scripts every frame is going to be a potential drain.  You might want to only execute scripts if they're triggered by some event, so you only have to check the trigger every frame.

I know you don't want to switch languages, but if you don't find some obvious bottleneck, it may very well be Rhino that's the problem.  Groovy (and especially groovy++) has made really impressive strides in performance lately, meanwhile the Rhino engine in java has quite literally not changed at all.

That's true, I only call executeScript once, then use executeFunction each frame.
the executeFunction takes about 0.3ms (which is not much), but does add up when there are 200 players each executing their scripts each frame. Maybe I should have 1 script that controls all the players, instead of one for each. And Janino is quite fast so I am going to see how it goes, even though groovy++ looks really good too.




Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roland
« Reply #10 - Posted 2011-11-03 03:06:00 »

No, the JVM probably just deleted the entire code as it doesn't do anything. Try summing the results and outputting them at the end, which will give you a more accurate view*

Cas Smiley

* of a microbenchmark, so largely meaningless

1  
2  
3  
4  
5  
6  
7  
function process()
{
   var a = 0.0;
   for (var i = 0; i < 1000000; i++)
      a += Math.sin(i);
   print(a + "\n");
}


i outputted a sum of the sin(i)'s getting a result of 0.23288397807310357 for each.

janino took on average 83ms and javascript took 835ms. (so only 10 times faster this time)

although you are right, its just a sin function so doesn't really mean much.



Offline roland
« Reply #11 - Posted 2011-11-03 03:09:09 »

I didn't word my question very clearly.  By "how" I really meant, does java's programming model fit what you want to do?
I'm still not sure what you mean. Do you mean why Java would be a good scripting language for my game?
I don't really care what language it is as long as it's fast enough, and I would rather use a language I knew well than having to learn a new one  Smiley
Offline sproingie

JGO Kernel


Medals: 202



« Reply #12 - Posted 2011-11-03 03:29:24 »

One script engine with one entry point that runs all the players' contributed scripts is likely to be immensely faster than invoking 200 separate script calls, yes.  Even then, when you have 200 possible iterations, making the scripts triggered if possible (obviously not the case if they're controlling animation) will make it all that much faster.  After all, the fastest code is the code you don't run.
Offline roland
« Reply #13 - Posted 2011-11-03 03:56:39 »

ok  Smiley Thanks sproingie
Offline Nate

« JGO Bitwise Duke »


Medals: 158
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #14 - Posted 2011-11-03 06:37:02 »

Pnuts compiles to byte code. It still isn't as fast as Java, since it needs to support additional language features, but it is fast. You might also just use Java for everything.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!