SpEL:当使用 SpEL Compilation 时,表达式评估的性能没有明显差异
SpEL:The performance of expression evaluation is not noticeably different, when use SpEL Compilation
我阅读了 SpEL 文档,了解到 SpEL 编译可以提高性能。但是我写了一个简单的测试用例,性能并没有明显提高。 为什么?
Class定义:
public class PersonContainer {
public List<Person> personList = new ArrayList<>();
}
class Person {
public Address address;
}
class Address {
public double code;
}
测试用例:
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
this.getClass().getClassLoader());
ExpressionParser parser = new SpelExpressionParser(config);
Expression expression = parser.parseExpression("personList[0].address.code < 0.2");
PersonContainer simple = new PersonContainer();
Address address1 = new Address();
address1.code = 0.1;
Person person = new Person();
person.address = address1;
simple.personList.add(person);
long begin = System.nanoTime();
for (int i = 0; i < 50000; i++) {
expression.getValue(simple, Boolean.class);
}
long end = System.nanoTime();
System.out.println(end - begin);
输出使用不同的 SpelCompilerMode:
OFF: 256158300
IMMEDIATE: 262268500
MIXED: 276793400
确保所有 class 都是 public
。 SpEL 可能会决定无法编译表达式的原因有多种。 class 在表达式中使用而不是 public 就是其中之一。您可以启用调试日志以查看表达式是否真正被编译,并跟踪 spring 编译代码以查看原因。
Person 和 Address class 更改为 public,您可以看到运行时的差异。
OFF : 505040500 ns
IMMEDIATE : 13948300 ns
MIXED : 24264200 ns
我阅读了 SpEL 文档,了解到 SpEL 编译可以提高性能。但是我写了一个简单的测试用例,性能并没有明显提高。 为什么?
Class定义:
public class PersonContainer {
public List<Person> personList = new ArrayList<>();
}
class Person {
public Address address;
}
class Address {
public double code;
}
测试用例:
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
this.getClass().getClassLoader());
ExpressionParser parser = new SpelExpressionParser(config);
Expression expression = parser.parseExpression("personList[0].address.code < 0.2");
PersonContainer simple = new PersonContainer();
Address address1 = new Address();
address1.code = 0.1;
Person person = new Person();
person.address = address1;
simple.personList.add(person);
long begin = System.nanoTime();
for (int i = 0; i < 50000; i++) {
expression.getValue(simple, Boolean.class);
}
long end = System.nanoTime();
System.out.println(end - begin);
输出使用不同的 SpelCompilerMode:
OFF: 256158300
IMMEDIATE: 262268500
MIXED: 276793400
确保所有 class 都是 public
。 SpEL 可能会决定无法编译表达式的原因有多种。 class 在表达式中使用而不是 public 就是其中之一。您可以启用调试日志以查看表达式是否真正被编译,并跟踪 spring 编译代码以查看原因。
Person 和 Address class 更改为 public,您可以看到运行时的差异。
OFF : 505040500 ns
IMMEDIATE : 13948300 ns
MIXED : 24264200 ns