1 37 package org.jfree.chart.demo; 38 39 import java.util.Date ; 40 41 46 public class Performance2 { 47 48 49 private double primitive = 42.0; 50 51 52 private Number object = new Double (42.0); 53 54 57 public Performance2() { 58 } 59 60 65 public double getPrimitive() { 66 return primitive; 67 } 68 69 74 public Number getPrimitiveAsObject() { 75 return new Double (primitive); 76 } 77 78 83 public Number getObject() { 84 return object; 85 } 86 87 93 public double getObjectAsPrimitive() { 94 return object.doubleValue(); 95 } 96 97 102 public void getPrimitiveLoop(int count) { 103 104 double d = 0.0; 105 for (int i = 0; i < count; i++) { 106 d = getPrimitive(); 107 } 108 System.out.println(d); 109 110 } 111 112 117 public void getPrimitiveAsObjectLoop(int count) { 118 119 double d = 0.0; 120 for (int i = 0; i < count; i++) { 121 d = getPrimitiveAsObject().doubleValue(); 122 } 123 System.out.println(d); 124 125 } 126 127 132 public void getObjectAsPrimitiveLoop(int count) { 133 134 double d = 0.0; 135 for (int i = 0; i < count; i++) { 136 d = getObjectAsPrimitive(); 137 } 138 System.out.println(d); 139 140 } 141 142 147 public void getObjectLoop(int count) { 148 149 double d = 0.0; 150 for (int i = 0; i < count; i++) { 151 d = getObject().doubleValue(); 152 } 153 System.out.println(d); 154 155 } 156 157 164 public void status(String label, Date start, Date end) { 165 long elapsed = end.getTime() - start.getTime(); 166 System.out.println(label + start.getTime() + "-->" + end.getTime() + " = " + elapsed); 167 } 168 169 174 public static void main(String [] args) { 175 176 Performance2 performance = new Performance2(); 177 int count = 10000000; 178 179 for (int repeat = 0; repeat < 3; repeat++) { Date s1 = new Date (); 182 performance.getPrimitiveLoop(count); 183 Date e1 = new Date (); 184 performance.status("getPrimitive() : ", s1, e1); 185 186 Date s2 = new Date (); 187 performance.getPrimitiveAsObjectLoop(count); 188 Date e2 = new Date (); 189 performance.status("getPrimitiveAsObject() : ", s2, e2); 190 191 Date s3 = new Date (); 192 performance.getObjectLoop(count); 193 Date e3 = new Date (); 194 performance.status("getObject() : ", s3, e3); 195 196 Date s4 = new Date (); 197 performance.getObjectAsPrimitiveLoop(count); 198 Date e4 = new Date (); 199 performance.status("getObjectAsPrimitive() : ", s4, e4); 200 201 System.out.println("-------------------"); 202 } 203 } 204 205 } 206 | Popular Tags |