java惯用基本操作性能测试-反射
java常用基本操作性能测试-反射
性能测试:
由于软硬件及其复杂,本结果只具有参考意义。
代码:
https://github.com/zhang-xzhi/perftest
测试方法:
1个test运行loop次,为一个suite,算一个suite的平均时间。
可以跑多个suite,各个suite算各自的suite平均时间。
由各个suite的平均时间计算总体平均时间。
计算各个suite的平均时间和总体平均时间的误差是否在delta内。
如果满足,则本次测试有效。
如果不满足,则增大loop重新运行。
时间单位为ns。
反射
loop=20000 suite=5 delta=0.2
测试了构造函数,方法,field的3种操作方式。
S1:
ReflectionConstructor
ReflectionField
ReflectionMethod
为使用class.get得到Constructor,Field,Method对象。
S2:
ReflectionConstructorInvoke
ReflectionFieldInvoke
ReflectionMethodInvoke
为使用Constructor,Field,Method对象来构造对象,取值,调用方法。
S3:
ReflectionConstructor_DirectAccess
ReflectionField_DirectAccess
ReflectionMethod_DirectAccess
为使用java代码直接构造对象,取值,调用方法。
可以看到S1:S2:S3各自相差一个数量级。
ReflectionNewInstance
由于class的实现对Constructor对象做了缓存,因此,class.newInstance的时间比ReflectionConstructorInvoke稍慢一点。
性能测试:
由于软硬件及其复杂,本结果只具有参考意义。
代码:
https://github.com/zhang-xzhi/perftest
测试方法:
1个test运行loop次,为一个suite,算一个suite的平均时间。
可以跑多个suite,各个suite算各自的suite平均时间。
由各个suite的平均时间计算总体平均时间。
计算各个suite的平均时间和总体平均时间的误差是否在delta内。
如果满足,则本次测试有效。
如果不满足,则增大loop重新运行。
时间单位为ns。
反射
loop=20000 suite=5 delta=0.2
avg=512 name=ReflectionConstructor des=class.getConstructor avg=59 name=ReflectionConstructorInvoke des=constructor.newInstance avg=8 name=ReflectionConstructor_DirectAccess des=direct access constructor. avg=480 name=ReflectionField des=getDeclaredField and setAccessible. avg=63 name=ReflectionFieldInvoke des=field.get(). avg=5 name=ReflectionField_DirectAccess des=direct access field. avg=665 name=ReflectionMethod des=getDeclaredMethod and setAccessible. avg=15 name=ReflectionMethodInvoke des=method.invoke(). avg=5 name=ReflectionMethod_DirectAccess des=direct access method. avg=110 name=ReflectionNewInstance des=class.newInstance()
测试了构造函数,方法,field的3种操作方式。
S1:
ReflectionConstructor
ReflectionField
ReflectionMethod
为使用class.get得到Constructor,Field,Method对象。
S2:
ReflectionConstructorInvoke
ReflectionFieldInvoke
ReflectionMethodInvoke
为使用Constructor,Field,Method对象来构造对象,取值,调用方法。
S3:
ReflectionConstructor_DirectAccess
ReflectionField_DirectAccess
ReflectionMethod_DirectAccess
为使用java代码直接构造对象,取值,调用方法。
可以看到S1:S2:S3各自相差一个数量级。
ReflectionNewInstance
由于class的实现对Constructor对象做了缓存,因此,class.newInstance的时间比ReflectionConstructorInvoke稍慢一点。