سؤال

I read many times that reflection will slow down the phone performance. How true is this?

For example in my case, I get some parameters from a web service which have the same name as the parameters of a class I have in my android app. so I just set the values of these parameters using java fields and reflection... it doesn't seem to slow down the performance..

can anybody explain to me the fact behind this idea about reflection slowing down performance?

هل كانت مفيدة؟

المحلول

Take a look at this question. Basically, you are getting outside of the optimizations that the compiler can perform because reflection happens dynamically.

If you're not making a lot of reflection calls (e.g., it would be bad to do inside the getView of a ListView), you can probably get away with it. It's there to be used, just be judicious about it.

نصائح أخرى

how true is this?

It is slower than not using reflection. It is definitely something you want to avoid in loops or during rapid UI processing (e.g., scrolling a ListView).

i get some parameters from a web service which have the same name as the parameters of a class i have in my android app. so i just set the values of these parameters using java fields and reflection... it doesn't seem to slow down the performance..

It does, though it may not be noticeable to the user in this case.

can anybody explain to me the fact behind this idea about reflection slowing down performance?

See the link provided by @Brian Cooley in his answer. Bear in mind that reflection on Dalvik (the virtual machine in Android) may be slower than reflection on the Java VM -- I am really rather certain it is not faster, at any rate.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top