Closed Piasy closed 8 years ago
With v3.0.0, one of the generated bind method looked like below:
v3.0.0
bind
public void bind(Object target, Intent intent) { final String className = target.getClass().getCanonicalName(); if (className.equals(ProfileActivity.class.getName())) { ProfileActivityAutoBundle.bind((ProfileActivity)target, intent); return; } }
Why do you generate code to check their class name rather than use instanceof operator?
instanceof
I create two methods to profiling their performance like below:
private void testInstanceOf() { Application app = getApplication(); for (int i = 0; i < 1_000_000; i++) { if (app instanceof DialogInterface.OnClickListener) { Log.d("MainActivity", "can't be there"); } } } private void testNameEquals() { Application app = getApplication(); String name = app.getClass().getName(); for (int i = 0; i < 1_000_000; i++) { if (name.equals("android.content.DialogInterface$OnClickListener")) { Log.d("MainActivity", "can't be there"); } } }
Their performance are close, time unit is ns:
ns
D/MainActivity::testInstanceOf: 14030000 D/MainActivity::testNameEquals: 14689000
There is reason. As you said, I think it should improve performance by replacing name comparison to using instanceof. I' ll try it in a few days. Thanks!
3.1.0 has released with fix this issue. Thanks @Piasy !
3.1.0
With
v3.0.0
, one of the generatedbind
method looked like below:Why do you generate code to check their class name rather than use
instanceof
operator?I create two methods to profiling their performance like below:
Their performance are close, time unit is
ns
: