java groovyshell_java – 时间执行Groovy与Beanshell的巨大差异

我在Groovy和Beanshell中解释相同的脚本.

Groovy需要很长时间(26分25秒),而Beanshell只需要20秒.

我对这种差异感到非常惊讶,我不明白为什么,Beanshell会更好吗?我使用Groovy是错的吗?

Groovy代码:

public void calcule_irg(double salaire) throws Throwable{

Binding binding = new Binding();

binding.setVariable("MNT_943", 0);

String script="double formule_irg(Double salaireSoumis) {\n" +

" int salaire = salaireSoumis.intValue();\n" +

" salaire = salaire / 10 * 10;\n" +

" \n" +

" Double impots = 0.0;\n" +

" \n" +

" if (salaire >= 15000 && salaire <= 22500) {\n" +

" impots = ((salaire - 10000) * 0.2) - 1000.0;\n" +

" } else if (salaire > 22500 && salaire <= 30000) {\n" +

" impots = ((salaire - 10000) * 0.2);\n" +

" impots = impots - (impots * 0.4);\n" +

" } else if (salaire >= 30001 && salaire <= 120000) {\n" +

" impots = 2500 + ((salaire - 30000) * 0.3);\n" +

" } else if (salaire >= 120001) {\n" +

" impots = 29500 + ((salaire - 120000) * 0.35);\n" +

" } else {\n" +

" impots = 0.0;\n" +

" }\n" +

" \n" +

" return impots.intValue();\n" +

"} \n" +

"\n" +

"MNT_943 = formule_irg("+salaire+") ;\n";

GroovyShell shell = new GroovyShell(binding);

shell.evaluate(script);

Double value =(Double) shell.getVariable("MNT_943");

}

Beanshell代码:

public void calcule_irg(double salaire) throws EvalError {

Interpreter i = new Interpreter(); // Construct an interpreter

// Eval a statement and get the result

String script="double formule_irg(Double salaireSoumis) {\n" +

" int salaire = salaireSoumis.intValue();\n" +

" salaire = salaire / 10 * 10;\n" +

" \n" +

" Double impots = 0.0;\n" +

" \n" +

" if (salaire >= 15000 && salaire <= 22500) {\n" +

" impots = ((salaire - 10000) * 0.2) - 1000.0;\n" +

" } else if (salaire > 22500 && salaire <= 30000) {\n" +

" impots = ((salaire - 10000) * 0.2);\n" +

" impots = impots - (impots * 0.4);\n" +

" } else if (salaire >= 30001 && salaire <= 120000) {\n" +

" impots = 2500 + ((salaire - 30000) * 0.3);\n" +

" } else if (salaire >= 120001) {\n" +

" impots = 29500 + ((salaire - 120000) * 0.35);\n" +

" } else {\n" +

" impots = 0.0;\n" +

" }\n" +

" \n" +

" return impots.intValue();\n" +

"} \n" +

"\n" +

"MNT_943 = formule_irg("+salaire+") ;\n";

i.eval(script);

}

主要:

public static void main(String[] args) throws Throwable{

Groovy g=new Groovy();// The function mentioned below is defined in a class named Groovy

Beanshell s=new Beanshell();// The function mentioned below is defined in a class named Beanshell

String fileName = "c://salaires100K.txt";

//read file into stream, try-with-resources

try (Stream stream = Files.lines(Paths.get(fileName))) {//This file contains 100'000 Salary, means 100'000 lines

stream.forEach( n->{

try {

g.calcule_irg(Double.parseDouble(n));//I use either this

//s.calcule_irg(Double.parseDouble(n));//Or this

} catch (Throwable ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}

});

} catch (IOException e) {

e.printStackTrace();

}

}

解决方法:

你最好使用Groovy 2编译的静态功能

Since version 2, Groovy can also be compiled statically, offering type inference, and performance near that of Java

在您的情况下,每次迭代调用循环外的相同脚本:

compiledScript = ((Compilable) scriptEngine).compile(script);

compiledScript.eval(binding);

import groovy.transform.CompileStatic

@CompileStatic

int squarePlusOne(int num) {

num * num + 1

}

assert squarePlusOne(3) == 10

标签:java,groovy,performance,runtimeexception,beanshell

来源: https://codeday.me/bug/20190701/1348501.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值