Accollativo
Accollativo

Reputation: 1559

java: is it more efficient a switch or a reflection?

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

Answers (7)

bNd
bNd

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

gaborsch
gaborsch

Reputation: 15758

If you have fixed number of methods and you are just lazy to type 1000 different cases, 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 Classes and Objects.

Upvotes: 2

shreyansh jogi
shreyansh jogi

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

AllTooSir
AllTooSir

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

Devolus
Devolus

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

Juned Ahsan
Juned Ahsan

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

Suresh Atta
Suresh Atta

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

Related Questions