views:

62

answers:

2

Currently I am doing the profiling to a piece of code. During the profiling, I discovered that this very method call,

Class<T>.isAssignableFrom(Class<?> cls)

takes up to quite amount of the entire time.

Because this is a method from reflection, it takes a lot of time compared to normal keywords or method calls. I am wondering if there are some good alternatives for this method calls?

+2  A: 

"[I]t examines the Class type passed in through a method argument to see if the type matches certain qualifications."

To me, that implies that the method argument should be required to implement a particular interface or inherit from a particular class. Keep in mind, the interface could be a marker like RandomAccess. I realize changing your API may not be an option.

Matthew Flaschen
+2  A: 

If you have an object whose class you are retrieving, you can replace this with:

obj instanceof ClassName

but I wouldn't say its faster. Actually, I doubt this causes any problems with the program execution. Don't overoptimize.

Bozho
+1 for doubting if this makes a real difference. How much time are we talking here?
Thilo