|
|
On previous page we saw a way to compare two objects but that way is fine when we don't know about the structure of the objects. If we know then it is better not to use reflection. Attached are the files to compare the ways of comparison. Below is the result of the execution of 'Reflections.java' -
Using Getters & Setters...... 52058ms
Using Reflection, only to access the fields...... 107539ms
Using Reflection, even creation of instances...... 445043ms
Using access of fields directly...... 49065ms
These may not be the exact results when you execute the code on your system but you will see that reflection is slower and Java optimization is getting skipped when using reflection. So use reflection judiciously as such usage can give a major performance hit to your application.
Using Getters & Setters...... 52058ms
Using Reflection, only to access the fields...... 107539ms
Using Reflection, even creation of instances...... 445043ms
Using access of fields directly...... 49065ms
These may not be the exact results when you execute the code on your system but you will see that reflection is slower and Java optimization is getting skipped when using reflection. So use reflection judiciously as such usage can give a major performance hit to your application.