Reputation: 1559
From a jsp I get a string that I could use for a switch
switch(value)
case 0: method0(); break;
case 1: method1(); break;
...
or for a reflection:
c.getMethod("method"+value, parameter);
...
Which approach is more efficient?
Upvotes: 3
Views: 2440
Reputation: 7630
Basically first understand that when to use reflection ? it is mostly used for reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can then invoke the type's methods or access its fields and properties.
But here you don't have such case so better to use switch instead of reflection.
And avoid unnecessary burden of your process.
Upvotes: -1
Reputation: 15758
If you have fixed number of methods and you are just lazy to type 1000 different case
s, then you'd definitely use switch
, because that statement is highly optimized on JVM bytecode level.
If you have an indefinite number of methods, you could use reflection (probably you don't have any other choices). Still you can speed up the process by caching the Method
instances you get from getMethod()
.
Note that passing arguments via reflection always creates extra arrays of Class
es and Object
s.
Upvotes: 2
Reputation: 2102
better to use switch option as reflection is heavy as compared to memory. and what if the case that is not available so that you can handle in switch statements while in reflection you have example of input * so it look for the method method* which is not available may be.
Upvotes: 0
Reputation: 49372
Reflection has a big overhead if you're looking for quicker performance. According to the Oracle Java tutorials:
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Java VM has special bytecodes which could be used for switch-case like lookupswitch
and tableswitch
.
The best approach if you are able to implement is Polymorphism , the Object Oriented approach.
Upvotes: 1
Reputation: 22084
Reflection is definitely not faster, as it has to go through additional layers.
However, using reflection for such a task would be the wrong way, as it makes the code harder to maintain and doesn't serve a real purpose which reflection is designed for.
Upvotes: 5
Reputation: 68715
Reflection will always have some overheads
From javadoc
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
Upvotes: 1
Reputation: 121998
Use Switch
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it.
http://docs.oracle.com/javase/tutorial/reflect/index.html
Upvotes: 0