执行效率
在本地简单测试这几种表达式的执行时间,在频繁执行中mvel表现较好,以下数据仅供参考,未取平均值。
次数 | ognl | mvel | spel | jexl | groovy | 效率排序 |
---|---|---|---|---|---|---|
10次耗时(毫秒) | 67 | 72 | 151 | 64 | 547 | jexl>ognl>mvel>spel>groovy |
100次耗时(毫秒) | 83 | 77 | 168 | 80 | 1487 | mvel>jexl>ognl>spel>groovy |
1000次耗时(毫秒) | 175 | 125 | 236 | 194 | 6334 | mvel>ognl>jexl>spel>groovy |
10000次耗时(毫秒) | 462 | 181 | 657 | 731 | 47489 | mvel>ognl>spel>jexl>groovy |
OGNL表达式
- 依赖
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.21</version>
</dependency>
- 示例
import ognl.Ognl;
import ognl.OgnlException;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class ExpressionTest {
@Test
public void ognlExpression() throws OgnlException {
String expressionString = "name.equals('李思')";
String expressionString2 = "age + 2";
Map map = new HashMap();
map.put("name", "李思");
map.put("age", 18);
Object expression = Ognl.parseExpression(expressionString);
Boolean resultBoolean = (Boolean) Ognl.getValue(expression, map);
Integer resultString = (Integer) Ognl.getValue(expressionString2, map);
System.out.println("OGNL:" + resultBoolean);
System.out.println("OGNL:" + resultString);
}
}
输出:
OGNL:true
OGNL:20
MVEL表达式
- 依赖
<!-- https://mvnrepository.com/artifact/org.mvel/mvel -->
<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel</artifactId>
<version>2.0beta1</version>
</dependency>
- 示例
import org.junit.Test;
import org.mvel.MVEL;
import java.util.HashMap;
import java.util.Map;
public class ExpressionTest {
@Test
public void mvelExpression(){
String expressionString = "name.equals('李思')";
String expressionString2 = "age + 2";
Map map = new HashMap();
map.put("name", "李思");
map.put("age", 18);
Boolean resultBoolean = MVEL.evalToBoolean(expressionString, map);
Integer resultInteger = MVEL.eval(expressionString2, map, Integer.class);
System.out.println("MVEL:" + resultBoolean);
System.out.println("MVEL:" + resultInteger);
}
}
输出:
MVEL:true
MVEL:20
SPEL表达式
- 依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.3.16</version>
</dependency>
- 示例
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.util.HashMap;
import java.util.Map;
public class ExpressionTest {
@Test
public void spelExpression(){
String expressionString = "#name.equals('李思')";
String expressionString2 = "#age + 2";
EvaluationContext evaluationContext = new StandardEvaluationContext();
evaluationContext.setVariable("name", "李思");
evaluationContext.setVariable("age", 18);
ExpressionParser expressionParser = new SpelExpressionParser();
Boolean resultBoolean = expressionParser.parseExpression(expressionString).getValue(evaluationContext, Boolean.class);
Integer resultInteger = expressionParser.parseExpression(expressionString2).getValue(evaluationContext, Integer.class);
System.out.println("SPEL:" + resultBoolean);
System.out.println("SPEL:" + resultInteger);
}
}
输出:
SPEL:true
SPEL:思
Jxel表达式
- 依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-jexl3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>3.1</version>
</dependency>
- 示例
import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.MapContext;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class ExpressionTest {
@Test
public void jexlExpression(){
String expressionString = "name.equals('李思')";
String expressionString2 = "age + 2";
JexlContext jc = new MapContext();
jc.set("name", "李思");
jc.set("age", 18);
JexlEngine jexl = new JexlBuilder().create();
Boolean resultBoolean = (Boolean) jexl.createExpression(expressionString).evaluate(jc);
Integer resultInteger = (Integer) jexl.createExpression(expressionString2).evaluate(jc);
System.out.println("Jexl:" + resultBoolean);
System.out.println("Jexl:" + resultInteger);
}
}
输出:
Jexl:true
Jexl:20
Groovy表达式
- 依赖
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.5.6</version>
</dependency>
- 示例
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
public class ExpressionTest {
@Test
public void groovyExpression(){
String expressionString = "name.equals('李思')";
String expressionString2 = "age + 2";
Binding binding = new Binding();
binding.setVariable("name", "李思");
binding.setVariable("age", 18);
GroovyShell shell = new GroovyShell(binding);
Boolean resultBoolean = (Boolean) shell.evaluate(expressionString);
Integer resultInteger = (Integer) shell.evaluate(expressionString2);
System.out.println("Groovy:" + resultBoolean);
System.out.println("Groovy:" + resultInteger);
}
}
输出:
Jexl:true
Jexl:20
标签:Java,name,util,引擎,李思,org,Test,import,表达式
From: https://www.cnblogs.com/gainer/p/17457934.html