在Android真实设备上执行Rhino代码时,我的Rhino性能问题很少。
Rhino上下文是用一个表示大JSON对象的字符串启动的,总字符串大小约为120K,为了测试代码性能,我们编写了一些工具测试来检查代码性能,但是,我们得到的结果不明确相同的代码,使用相同的参数显示测试和示例应用程序之间的结果完全不同。
测试性能比相同代码快10倍,作为工具测试然后在同一设备(G5)上作为示例应用程序的一部分运行。顺便说一句,Android模拟器也显示出良好的性能结果。
代码非常简单
private void init(String jFfunctionsDeclaration) throws ScriptInitException {
StringBuilder ruleEngineContextBuffer = new StringBuilder();
//create a JSON object in the string representation, later Rhino context will be initialized with this string
for (Map.Entry<String, String> e : scriptObjects.entrySet()) {
String key = e.getKey();
String value = e.getValue();
ruleEngineContextBuffer.append("\nvar ");
ruleEngineContextBuffer.append(key);
ruleEngineContextBuffer.append(" = "); // append(" = JSON.parse(");
ruleEngineContextBuffer.append(value);
}
// create and enter safe execution context to prevent endless loop or deadlock in JS
// because Rhino input it provided from outside
SafeContextFactory safeContextFactory = new SafeContextFactory();
rhino = safeContextFactory.makeContext().enter();
try {
// the fisrt init step, init Rhino cotext with JS utils methods
// functions input is the list of JS functions
sharedScope = rhino.initStandardObjects();
rhino.evaluateString(sharedScope, functions, "<init1>", 1, null);
String str = ruleEngineContextBuffer.toString();
long startContextInit = System.currentTimeMillis();
rhino.evaluateString(sharedScope, str, "<init2>", 1, null);
long totalContextInit = System.currentTimeMillis() - startContextInit;
Log.d(TAG, "Rhino context init duration = " + totalContextInit);
} catch (Throwable e) {
throw new ScriptInitException("Javascript shared scope initialization error: " + e.getMessage());
}
}
有人可以解释我这个谜,谢谢。