Advertisement
Guest User

Untitled

a guest
Sep 15th, 2009
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package com.micro.bench;
  2.  
  3. import javassist.ClassPool;
  4. import javassist.CtClass;
  5. import javassist.CtNewMethod;
  6.  
  7. interface Evaluator {
  8.     public double eval(double x);
  9. }
  10.  
  11. abstract class Fight {
  12.  
  13.     private int steps;
  14.     private int loops;
  15.  
  16.     public Fight(int loops, int steps) {
  17.         this.loops = loops;
  18.         this.steps = steps;
  19.     }
  20.  
  21.     public abstract Object step(Timer timer);
  22.  
  23.     public void loop(Timer timer) {
  24.         for (int j = 0; j < steps; ++j) {
  25.             step(timer);
  26.         }
  27.     }
  28.  
  29.     public void fight() {
  30.         Timer statistics = new Timer();
  31.         for (int i = 0; i < loops; i++) {
  32.             loop(statistics);
  33.             statistics.flush();
  34.             System.out.println(statistics);
  35.         }
  36.     }
  37. }
  38.  
  39. public class Evaluation extends Fight {
  40.  
  41.     private static final int MAX = 1000000;
  42.  
  43.     private Evaluator runtime;
  44.     private Evaluator compileTime;
  45.  
  46.     public Evaluation(int loops, int steps) {
  47.         super(loops, steps);
  48.         init();
  49.     }
  50.  
  51.     public void init() {
  52.  
  53.         try {
  54.  
  55.             ClassPool pool = ClassPool.getDefault();
  56.  
  57.             CtClass evalClass = pool.makeClass("Formula");
  58.             evalClass.setInterfaces(
  59.                     new CtClass[]{
  60.                             pool.makeClass("com.micro.bench.Evaluator")
  61.                     });
  62.  
  63.             String expession = "x * x + x / 2";
  64.  
  65.             evalClass.addMethod(
  66.                     CtNewMethod.make(
  67.                             "public double eval (double x) { return (" + expession + ") ; }",
  68.                             evalClass));
  69.  
  70.             Class clazz = evalClass.toClass();
  71.             runtime = (Evaluator) clazz.newInstance();
  72.         } catch (Exception e) {
  73.             e.printStackTrace();
  74.         }
  75.  
  76.         compileTime = new Evaluator() {
  77.             public double eval(double x) {
  78.                 return x * x + x / 2;
  79.             }
  80.         };
  81.     }
  82.  
  83.     public Object step(Timer timer) {
  84.  
  85.         timer.start("runtime calc");
  86.         double result = 0;
  87.         for (int i1 = 0; i1 < MAX; i1++) {
  88.             result += runtime.eval(i1);
  89.         }
  90.         timer.stop();
  91.  
  92.         double i = result;
  93.  
  94.         timer.start("compile-time calc");
  95.         double result11 = 0;
  96.         for (int i1 = 0; i1 < MAX; i1++) {
  97.             result11 += compileTime.eval(i1);
  98.         }
  99.         timer.stop();
  100.  
  101.         return i + result11;
  102.     }
  103.  
  104.     public static void main(String[] args) throws Exception {
  105.         new Evaluation(20, 100).fight();
  106.     }
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement