Performance of the compiled vs. interpreted javascript in java7 / Rhino

前端 未结 3 613
忘了有多久
忘了有多久 2020-12-29 13:45

I have a problem with performance of Rhino javascript engine in Java7, shortly - my script (that parses and compiles texts) runs in Chrome around 50-100 times quicker than t

相关标签:
3条回答
  • 2020-12-29 14:00

    The Rhino engine is actually capable of compiling scripts into bytecode 'in-process', so you don't need to run the tool to generate .class files first. You only need to set 'optimisation level' and the engine will automatically pre-compile the script before executing it. One way to override the optimisation level is with the VM argument -Drhino.opt.level. Set this to anything between 0 and 9 and run your original test program and you should see better performance.

    This is the same optimisation setting used by the compiling tool you mentioned, by the way. https://developer.mozilla.org/en-US/docs/Rhino/Optimization

    For total control of optimisation level and javascript version within your program, you can do the following. However you lose some of the trimmings that RhinoScriptEngine class (which is just a environment wrapper and not the javascript engine) provides. One such trimming is the 'print' function which is actually injected by said wrapper. For test purposes you can just replace 'print' with 'java.lang.System.out.print'.

        int optimisationLevel = 3;
        int languageVersion = Context.VERSION_1_7;
    
        try {
            Context cx = Context.enter();
            cx.setOptimizationLevel(optimisationLevel);
            cx.setLanguageVersion(languageVersion);
    
            ImporterTopLevel scope = new ImporterTopLevel(cx);
            cx.evaluateString(scope, TEST_SCRIPT1, "doTest", 1, null);
    
            for (int i = 0; i < 10; i++)
                cx.evaluateString(scope, "doTest();", "", 1, null);
    
        } finally {
            Context.exit();
        }
    

    You mentioned the following:

    Note: some sources mention that Rhino engine runs compiled script roughly 1.6 slower than the "same" code written directly in Java. Not sure if "compilation of script" used in this sample the is same one which is supposed there.

    I'd be interested in the source that reported this, My fibonacci function in java takes about 1/30 the time as the compiled js implementation. But perhaps I'm missing something.

    0 讨论(0)
  • 2020-12-29 14:05

    It looks like i've found what is wrong - compilation used in 'my' code (actually taken from internet samples) has nothing to do with 'compiled'.

    Finally i've got to this link - https://developer.mozilla.org/en-US/docs/Rhino_JavaScript_Compiler - the Rhino's tool to compile .js into .class. I've got following result with the same JS code running compiled from .class's bytecode:

     time: 202ms, chars: 38890, sum: 2046720
     time: 92ms, chars: 38890, sum: 2046720
     time: 73ms, chars: 38890, sum: 2046720
     ...
     time: 71ms, chars: 38890, sum: 2046720
     time: 66ms, chars: 38890, sum: 2046720
     time: 64ms, chars: 38890, sum: 2046720
      ... 1143ms (per 15 iterations)
    
    --- sleep 5 secs ---
    
     time: 64ms, chars: 38890, sum: 2046720
     time: 52ms, chars: 38890, sum: 2046720
     time: 64ms, chars: 38890, sum: 2046720
     ...
     time: 62ms, chars: 38890, sum: 2046720
     time: 67ms, chars: 38890, sum: 2046720
     time: 67ms, chars: 38890, sum: 2046720
      ... 962ms
    
    
    --- sleep 5 secs ---
    
     time: 66ms, chars: 38890, sum: 2046720
     time: 56ms, chars: 38890, sum: 2046720
     time: 59ms, chars: 38890, sum: 2046720
     ...
     time: 69ms, chars: 38890, sum: 2046720
     time: 67ms, chars: 38890, sum: 2046720
     time: 59ms, chars: 38890, sum: 2046720
      ... 966ms
    

    (this is roughly 10 time quicker)

    and this is the result from Chrome:

     time: 5ms, chars: 38890, sum: 2046720
     time: 3ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
     time: 5ms, chars: 38890, sum: 2046720
     time: 0ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
     time: 1ms, chars: 38890, sum: 2046720
    

    (average is 3-4 msec, ~15 quicker than compiled Java/Rhino, and ~200 times quicker than interpreted Java/Rhino).

    0 讨论(0)
  • 2020-12-29 14:06

    I've found that for simple programs at least, the extra time spent compiling your code can overshadow the time running it. Then don't forget, it takes a little while before HotSpot compiles the Java bytecode to native code.

    I think if you used a longer-running benchmark with more complex code (as opposed to a relatively simple program that's doing a lot of library calls) the compiled version would eventually win out, but the performance still won't be comparable to V8.

    Oracle is working on a newer EcmaScript engine for Java 8 that should be faster, but it's going to be awhile before that's available.

    0 讨论(0)
提交回复
热议问题