作者:张明云
反射具体是怎么影响性能的?这引起了我的反思 。
是啊 , 在阐述某个观点时确实有必要说明原因 , 并且证明这个观点是对的 , 虽然反射影响性能人尽皆知 , 我曾经也真的研究过反射是否存在性能问题 , 但并没有在写文章的时候详细说明 。
这让我想到网上很多信息只会告诉你结论 , 并不会说明原因 , 导致很多学到的东西都是死记硬背 , 而不是真正掌握 , 别人一问或者自己亲身遇到同样的问题时 , 傻眼了 。
反射真的存在性能问题吗?还是使用上篇文章的demo , 为了放大问题 , 找到共性 , 采用逐渐扩大测试次数、每次测试多次取平均值的方式 , 针对同一个方法分别就直接调用该方法、反射调用该方法、直接调用该方法对应的实例、反射调用该方法对应的实例分别从1-1000000 , 每隔一个数量级测试一次:
测试代码如下(Person、ICompany、ProgramMonkey这三个类已在之前的文章中贴出):
public class ReflectionPerformanceActivity extends Activity{ private TextView mExecuteResultTxtView = null; private EditText mExecuteCountEditTxt = null; private Executor mPerformanceExecutor = Executors.newSingleThreadExecutor(); private static final int AVERAGE_COUNT = 10; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_reflection_performance_layout); mExecuteResultTxtView = (TextView)findViewById(R.id.executeResultTxtId); mExecuteCountEditTxt = (EditText)findViewById(R.id.executeCountEditTxtId); } public void onClick(View v){ switch(v.getId()){ case R.id.executeBtnId:{ execute(); } break; default:{ } break; } } private void execute(){ mExecuteResultTxtView.setText(""); mPerformanceExecutor.execute(new Runnable(){ @Override public void run(){ long costTime = 0; int executeCount = Integer.parseInt(mExecuteCountEditTxt.getText().toString()); long reflectMethodCostTime=0,normalMethodCostTime=0,reflectFieldCostTime=0,normalFieldCostTime=0; updateResultTextView(executeCount + "毫秒耗时情况测试"); for(int index = 0; index < AVERAGE_COUNT; index++){ updateResultTextView("第 " + (index+1) + " 次"); costTime = getNormalCallCostTime(executeCount); reflectMethodCostTime += costTime; updateResultTextView("执行直接调用方法耗时:" + costTime + " 毫秒"); costTime = getReflectCallMethodCostTime(executeCount); normalMethodCostTime += costTime; updateResultTextView("执行反射调用方法耗时:" + costTime + " 毫秒"); costTime = getNormalFieldCostTime(executeCount); reflectFieldCostTime += costTime; updateResultTextView("执行普通调用实例耗时:" + costTime + " 毫秒"); costTime = getReflectCallFieldCostTime(executeCount); normalFieldCostTime += costTime; updateResultTextView("执行反射调用实例耗时:" + costTime + " 毫秒"); } updateResultTextView("执行直接调用方法平均耗时:" + reflectMethodCostTime/AVERAGE_COUNT + " 毫秒"); updateResultTextView("执行反射调用方法平均耗时:" + normalMethodCostTime/AVERAGE_COUNT + " 毫秒"); updateResultTextView("执行普通调用实例平均耗时:" + reflectFieldCostTime/AVERAGE_COUNT + " 毫秒"); updateResultTextView("执行反射调用实例平均耗时:" + normalFieldCostTime/AVERAGE_COUNT + " 毫秒"); } }); } private long getReflectCallMethodCostTime(int count){ long startTime = System.currentTimeMillis(); for(int index = 0 ; index < count; index++){ ProgramMonkey programMonkey = new ProgramMonkey("小明", "男", 12); try{ Method setmLanguageMethod = programMonkey.getClass().getMethod("setmLanguage", String.class); setmLanguageMethod.setAccessible(true); setmLanguageMethod.invoke(programMonkey, "JAVA"); }catch(IllegalAccessException e){ e.printStackTrace(); }catch(InvocationTargetException e){ e.printStackTrace(); }catch(NoSuchMethodException e){ e.printStackTrace(); } } return System.currentTimeMillis()-startTime; } private long getReflectCallFieldCostTime(int count){ long startTime = System.currentTimeMillis(); for(int index = 0 ; index < count; index++){ ProgramMonkey programMonkey = new ProgramMonkey("小明", "男", 12); try{ Field ageField = programMonkey.getClass().getDeclaredField("mLanguage"); ageField.set(programMonkey, "Java"); }catch(NoSuchFieldException e){ e.printStackTrace(); }catch(IllegalAccessException e){ e.printStackTrace(); } } return System.currentTimeMillis()-startTime; } private long getNormalCallCostTime(int count){ long startTime = System.currentTimeMillis(); for(int index = 0 ; index < count; index++){ ProgramMonkey programMonkey = new ProgramMonkey("小明", "男", 12); programMonkey.setmLanguage("Java"); } return System.currentTimeMillis()-startTime; } private long getNormalFieldCostTime(int count){ long startTime = System.currentTimeMillis(); for(int index = 0 ; index < count; index++){ ProgramMonkey programMonkey = new ProgramMonkey("小明", "男", 12); programMonkey.mLanguage = "Java"; } return System.currentTimeMillis()-startTime; } private void updateResultTextView(final String content){ ReflectionPerformanceActivity.this.runOnUiThread(new Runnable(){ @Override public void run(){ mExecuteResultTxtView.Append(content); mExecuteResultTxtView.append("n"); } }); }}
推荐阅读
- 拒做PB Boy!教你巧用 Protobuf 反射来优化代码
- Win10的兼容性到底有多强,你甚至可以运行16位的上古程序
- 如何在JavaScript中对对象数组进行排序
- Java内存泄漏、性能优化、宕机死锁的N种姿势
- 排名前5的JavaScript框架
- Linux 上部署 Java 应用绕不开的命令,撒花啦
- 买茶到底去哪里买,买就送玻璃杯萧氏毛尖自饮绿茶茶叶高山云雾茶日照原产嫩芽栗香味浓120g
- java 实现 SelectSort 选择排序算法详解
- 六安瓜片到底怎么样,安徽六安六安瓜片
- 凉水泡茶有什么危害吗,茶烟到底对身体有害吗
