java表达式运算性能比较:Jep与QLExpress
原创 2021-11-18 09:48 阅读(2246)次
前言
之前我有分享过java的表达式运算,由于我的业务要求表达式中要有业务变量,例如要根据id和orderNumber动态生成一个单号,表达式的定义如下所示:
id+orderNumber
所以我们需要一个支持替换变量的表达式计算引擎,开始我们选择了jep,但后面发现我们业务中的jep表达式计算秒级只能做到2300条左右,这太慢了,于是我再找了下其他引擎,发现阿里开源了一个,名叫QLExpress,地址:https://gitee.com/cuibo119/QLExpress
上手试一下,引入maven:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>QLExpress</artifactId>
<version>3.2.0</version>
</dependency>
表达式计算代码如下:
public static void main(String args[]) throws Exception {
ExpressRunner expressRunner = new ExpressRunner();
String expressValue = "a+b";
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
context.put("a",1);
context.put("b",10);
Object result = expressRunner.execute(expressValue, context, null, true, false);
String value = result.toString();
System.out.println(value);
}
上面代码输出如下:
11
于是我赶紧拿它与jep做性能比较:
运行jep请参考我之前的文章:http://www.classinstance.cn/detail/168.html
jep做30000次运算
代码如下:
public static void main(String args[]) throws Exception {
long t1 = System.currentTimeMillis();
String expressValue = "a+b";
for (int i = 0;i<30000;i++) {
JEP jep = new JEP();
jep.addVariableAsObject("a",1);
jep.addVariableAsObject("b",10);
jep.parseExpression(expressValue);
String value = jep.getValueAsObject().toString();
System.out.println(value);
}
long t2 = System.currentTimeMillis();
System.out.println("用时:"+(t2-t1));
}
用时:2736
QLExpress做30000次运算
public static void main(String args[]) throws Exception {
long t1 = System.currentTimeMillis();
ExpressRunner expressRunner = new ExpressRunner();
String expressValue = "a+b";
for (int i = 0;i<30000;i++) {
DefaultContext<String, Object> context = new DefaultContext<String, Object>();
context.put("a",1);
context.put("b",10);
Object result = expressRunner.execute(expressValue, context, null, true, false);
String value = result.toString();
System.out.println(value);
}
long t2 = System.currentTimeMillis();
System.out.println("用时:"+(t2-t1));
}
用时:879
结论
很明显示QLExpress比jep快了3倍多,为什么?因为QLExpress上面的代码开启了表达式缓存,不需要每次计算都解析表达式,并且ExpressRunner做成单例,减少对象的对象的创建,但Jep好像不能这么做。在我们的业务中QLExpress比jep快了10倍,所以我们果断把jep改成了QLExpress。其实QLExpress还有很多好的特性等着我们去发现。
上一篇:存算分离与存算一体的优缺点比较