.. but that was c# . What about Java ? No dynamics so we should use reflection.
Answers:
- Simple Counter
- Counter as long (miliseconds):
- 264
- Counter as object, and cast to long:
- 13484
- Difference:
- casting from object to long is 51 times slower .
- Simple class with int property
- No casting
- 256
- casting from object
- 252
- used as reflection
- 29360114
- Difference
- casting from object to class is just .. 1.0158 times ... faster ?
- but reflection ... is 114687.94 times slower !
package castingandgenerics; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Date; public class program { public static void main(String [ ] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { objectAndLongComparison(); objectAndClassComparison(); } private static void objectAndLongComparison() { long longCounter = 0; Object objectCounter = longCounter; Date timeLongStart, timeLongStop, timeObjectStart, timeObjectStop; System.out.println("Object and Long comparison:"); timeLongStart = new Date(); for (int i = 0; i < Integer.MAX_VALUE; i++) { longCounter = longCounter + 1; } timeLongStop = new Date(); System.out.println(timeLongStop.getTime() - timeLongStart.getTime()); timeObjectStart = new Date(); for (int i = 0; i < Integer.MAX_VALUE; i++) { objectCounter = ((long)objectCounter) + 1; } timeObjectStop = new Date(); System.out.println(timeObjectStop.getTime() - timeObjectStart.getTime()); } private static void objectAndClassComparison() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Date[] timeStart = new Date[3]; Date[] timeStop = new Date[3]; Class1 myClass = new Class1(); Object myObject = myClass; // dynamic myDynamic = myClass; System.out.println("Object and Class comparison:"); timeStart[0] = new Date(); for (int i = 0; i < Integer.MAX_VALUE; i++) { myClass.setMyProperty1(i); //myClass.MyProperty2 = string.Empty; } timeStop[0] = new Date(); System.out.println(timeStop[0].getTime() - timeStart[0].getTime()); timeStart[1] = new Date(); for (int i = 0; i < Integer.MAX_VALUE; i++) { ((Class1)myObject).setMyProperty1(i); } timeStop[1] = new Date(); System.out.println(timeStop[1].getTime() - timeStart[1].getTime()); timeStart[2] = new Date(); Class[] cArg = new Class[1]; cArg[0] = int.class; for (int i = 0; i < 1024 ; i++) { Method method = myObject.getClass().getMethod("setMyProperty1",cArg[0]); method.invoke(myObject, i); } timeStop[2] = new Date(); System.out.println((timeStop[2].getTime() - timeStart[2].getTime())*(Integer.MAX_VALUE/1024)); } }
Code is also here (GitHub).
No comments:
Post a Comment