android real设备上Rhino性能低
在Android real设备上执行Rhino代码时,我遇到了Rhino性能低下的问题 Rhino上下文由一个代表大JSON对象的字符串启动,总字符串大小约为120K,为了测试代码性能,我们编写了一些工具测试来检查代码性能,但是,对于相同的代码,我们没有得到明确的结果,在相同的参数下,测试和示例应用程序之间显示出完全不同的结果 测试性能比作为仪器测试执行的相同代码快10倍,然后在同一设备上作为示例应用程序的一部分运行(G5)。顺便说一句,android模拟器也显示了良好的性能结果 代码非常简单android real设备上Rhino性能低,android,performance,rhino,Android,Performance,Rhino,在Android real设备上执行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());
}
}
private void init(字符串jFfunctionsDeclaration)引发ScriptInitException{
StringBuilder ruleEngineContextBuffer=新StringBuilder();
//在字符串表示中创建JSON对象,稍后Rhino上下文将使用此字符串初始化
对于(Map.Entry e:scriptObjects.entrySet()){
String key=e.getKey();
字符串值=e.getValue();
ruleEngineContextBuffer.append(“\nvar”);
ruleEngineContextBuffer.append(键);
ruleEngineContextBuffer.append(“”;//append(“”=JSON.parse(“”);
ruleEngineContextBuffer.append(值);
}
//创建并输入安全执行上下文,以防止JS中出现无休止的循环或死锁
//因为Rhino从外部提供输入
SafeContextFactory SafeContextFactory=新的SafeContextFactory();
rhino=safeContextFactory.makeContext().enter();
试一试{
//第一个init步骤,init Rhino cotext和JS-utils方法
//函数输入是JS函数的列表
sharedScope=rhino.initStandardObjects();
evaluateString(sharedScope,函数,“”,1,null);
String str=ruleEngineContextBuffer.toString();
long startContextInit=System.currentTimeMillis();
evaluateString(sharedScope,str,“,1,null);
long totalContextInit=System.currentTimeMillis()-startContextInit;
Log.d(标记,“Rhino context init duration=“+totalContextInit”);
}捕获(可丢弃的e){
抛出新的ScriptInitException(“Javascript共享范围初始化错误:+e.getMessage());
}
}
有人能给我解释一下这个谜团吗,谢谢。这是在UI线程上运行的吗?在示例Android应用程序中,它在主线程上运行,将尝试在后台线程上运行,这是值得测试的,很好。当代码在UI线程或后台线程上执行时,没有区别。这是十次顺序执行的结果真实设备上的离子持续时间约为460毫秒,还有一个有趣的事实,在Emulator上,我得到了一个非常好的结果,性能越来越好,第一次从200毫秒开始,然后每次都下降到10毫秒。看起来像是一个真实的设备在某种程度上限制了Rhino,CPU切换线程不会优化代码本身,r这样你就可以拥有一个无阻塞的进程。模拟器不是一个很好的衡量标准,因为它使用你的系统资源进行计算,这比普通手机要快得多。使用javascript解释器是一个昂贵的操作,你有什么方法可以预处理它吗?我需要更多地了解Rhino及其预处理程序essing选项但让我困惑的是,在同一台真实设备上执行的仪器测试,同样的代码显示了比实际快10倍的惊人结果,这是如何解释的。