Copy Source | Copy HTML
- class A {
- public int value = 0;
-
- public void add(int x) {
- value += x;
- }
- }
Copy Source | Copy HTML
- t.start("Direct field access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- a.value += i;
- }
- t.stop();
Copy Source | Copy HTML
- t.start("Preparing for reflective field access");
- Field f = A.class.getField("value");
- t.stop();
-
- t.start("Reflective field access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- f.set(a, ((Integer) f.get(a)) + i);
- }
- t.stop();
Copy Source | Copy HTML
- t.start("Direct method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- a.add(i);
- }
- t.stop();
-
- t.start("Preparing for reflective method access");
- Method m = A.class.getDeclaredMethod("add", int.class);
- t.stop();
-
- t.start("Reflective method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- m.invoke(a, i);
- }
- t.stop();
Copy Source | Copy HTML
- t.start("Preparing for fast reflective method access");
- FastClass fc = FastClass.create(A.class);
- FastMethod fm = fc.getMethod(m);
- t.stop();
-
- t.start("Fast reflective method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- fm.invoke(a, new Object[]{i});
- }
- t.stop();
Copy Source | Copy HTML
- class Ref {
- int value;
- }
-
- class A {
- public int value = 0;
-
- public void add(Ref ref) {
- value += ref.value;
- }
- }
-
- ...
-
- t.start("Preparing for fast reflective method access (2)");
- FastClass fc2 = FastClass.create(A.class);
- FastMethod fm2 = fc2.getMethod("add", new Class[]{Ref.class});
- Ref ref = new Ref();
- Object[] arguments = new Object[]{ref};
- t.stop();
-
- t.start("Fast reflective method access (2)");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- ref.value = i;
- fm2.invoke(a, arguments);
- }
- t.stop();
Copy Source | Copy HTML
- interface AIf {
- public void add(int x);
- }
-
- class A implements AIf {
- public int value = 0;
-
- public void add(int x) {
- value += x;
- }
- }
-
- class ADecorator implements AIf {
- private AIf a;
-
- public ADecorator(AIf a) {
- this.a = a;
- }
-
- public void add(int x) {
- a.add(x);
- }
- }
- ...
- t.start("Preparing decorator method access");
- AIf d = new ADecorator(a);
- t.stop();
-
- t.start("Decorator method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- d.add(i);
- }
- t.stop();
Copy Source | Copy HTML
- package reflection;
-
- import net.sf.cglib.reflect.FastClass;
- import net.sf.cglib.reflect.FastMethod;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.LinkedHashMap;
- import java.util.Map;
-
- class Timer {
- private long startTime = 0;
-
- private String msg = null;
-
- private Map<String, Long> map = new LinkedHashMap<String, Long>();
-
- public void start(String msg) {
- if (startTime != 0) {
- throw new IllegalStateException("Already started");
- }
- startTime = System.currentTimeMillis();
- this.msg = msg;
- }
-
- public void stop() {
- if (startTime == 0) {
- throw new IllegalStateException("Not started");
- }
- long now = System.currentTimeMillis();
- Long n = map.get(msg);
- if (n == null) {
- n = 0l;
- }
- n += (now - startTime);
- map.put(msg, n);
- startTime = 0;
- msg = null;
- }
-
- public void output() {
- for (String msg : map.keySet()) {
- System.out.println(msg + ": " + map.get(msg));
- }
- }
- }
-
- class Ref {
- int value;
- }
-
- interface AIf {
- public void add(int x);
- }
-
- class A implements AIf {
- public int value = 0;
-
- public void add(int x) {
- value += x;
- }
-
- public void add(Ref ref) {
- value += ref.value;
- }
- }
-
- class ADecorator implements AIf {
- private AIf a;
-
- public ADecorator(AIf a) {
- this.a = a;
- }
-
- public void add(int x) {
- a.add(x);
- }
- }
-
- public class Reflect {
- private static final int TOTAL_LOOP_COUNT = 5000000;
-
- /**
* How many loops to do in one step.
*/
- private static final int LOOPS_IN_STEP_COUNT = 100;
-
- /**
* How many steps to do - in each step there will be
* {@link #LOOPS_IN_STEP_COUNT} loops.
*/
- private static final int STEP_COUNT = TOTAL_LOOP_COUNT / LOOPS_IN_STEP_COUNT;
-
- public static void main(String[] args) throws SecurityException,
- NoSuchFieldException, IllegalArgumentException,
- IllegalAccessException, NoSuchMethodException,
- InvocationTargetException {
-
- Timer t = new Timer();
- A a = new A();
-
- for (int j = 0; j < STEP_COUNT; ++j) {
-
- t.start("Direct field access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- a.value += i;
- }
- t.stop();
-
- t.start("Preparing for reflective field access");
- Field f = A.class.getField("value");
- t.stop();
-
- t.start("Reflective field access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- f.set(a, ((Integer) f.get(a)) + i);
- }
- t.stop();
-
- t.start("Direct method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- a.add(i);
- }
- t.stop();
-
- t.start("Preparing for reflective method access");
- Method m = A.class.getDeclaredMethod("add", int.class);
- t.stop();
-
- t.start("Reflective method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- m.invoke(a, i);
- }
- t.stop();
-
- t.start("Preparing for fast reflective method access");
- FastClass fc = FastClass.create(A.class);
- FastMethod fm = fc.getMethod(m);
- t.stop();
-
- t.start("Fast reflective method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- fm.invoke(a, new Object[]{i});
- }
- t.stop();
-
- t.start("Preparing for fast reflective method access (2)");
- FastClass fc2 = FastClass.create(A.class);
- FastMethod fm2 = fc2.getMethod("add", new Class[]{
- Ref.class
- });
- Ref ref = new Ref();
- Object[] arguments = new Object[]{ref};
- t.stop();
-
- t.start("Fast reflective method access (2)");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- ref.value = i;
- fm2.invoke(a, arguments);
- }
- t.stop();
-
- t.start("Preparing decorator method access");
- AIf d = new ADecorator(a);
- t.stop();
-
- t.start("Decorator method access");
- for (int i = 0; i < LOOPS_IN_STEP_COUNT; ++i) {
- d.add(i);
- }
- t.stop();
-
- }
-
- t.output();
- }
- }
-
Только зарегистрированные пользователи могут оставлять комментарии. Войдите, пожалуйста.
комментарии (13)