1 9 package javolution; 10 11 import javolution.realtime.ObjectFactory; 12 import javolution.realtime.PoolContext; 13 import javolution.realtime.RealtimeObject; 14 15 21 final class Perf_Realtime extends Javolution implements Runnable { 22 23 private volatile Object _object; 24 25 private volatile Object[] _objects = new Object[1000]; 26 27 30 public void run() { 31 println("//////////////////////////////////"); 32 println("// Package: javolution.realtime //"); 33 println("//////////////////////////////////"); 34 println(""); 35 36 println("-- Heap versus Stack Allocation (Pool-Context) --"); 37 print("Small standard object heap creation: "); 38 startTime(); 39 for (int i = 0; i < 10000; i++) { 40 for (int j = 0; j < _objects.length;) { 41 _objects[j++] = new SmallObjectStandard(); 42 } 43 } 44 println(endTime(10000 * _objects.length)); 45 46 print("Small real-time object stack creation: "); 47 startTime(); 48 for (int i = 0; i < 10000; i++) { 49 PoolContext.enter(); 50 for (int j = 0; j < _objects.length;) { 51 _objects[j++] = SmallObjectRealtime.FACTORY.object(); 52 } 53 PoolContext.exit(); 54 } 55 println(endTime(10000 * _objects.length)); 56 57 print("char[128] heap creation: "); 58 startTime(); 59 for (int i = 0; i < 1000; i++) { 60 for (int j = 0; j < _objects.length;) { 61 _objects[j++] = new char[128]; 62 } 63 } 64 println(endTime(1000 * _objects.length)); 65 66 print("char[128] stack creation: "); 67 startTime(); 68 for (int i = 0; i < 1000; i++) { 69 PoolContext.enter(); 70 for (int j = 0; j < _objects.length;) { 71 _objects[j++] = CHAR128_FACTORY.object(); 72 } 73 PoolContext.exit(); 74 } 75 println(endTime(1000 * _objects.length)); 76 77 print("char[256] heap creation: "); 78 startTime(); 79 for (int i = 0; i < 1000; i++) { 80 for (int j = 0; j < _objects.length;) { 81 _objects[j++] = new char[256]; 82 } 83 } 84 println(endTime(1000 * _objects.length)); 85 86 print("char[256] stack creation: "); 87 startTime(); 88 for (int i = 0; i < 1000; i++) { 89 PoolContext.enter(); 90 for (int j = 0; j < _objects.length;) { 91 _objects[j++] = CHAR256_FACTORY.object(); 92 } 93 PoolContext.exit(); 94 } 95 println(endTime(1000 * _objects.length)); 96 println(""); 97 } 98 99 private static final class SmallObjectStandard { 100 long longValue; 101 int intValue; 102 SmallObjectStandard refValue; 103 } 104 private static final class SmallObjectRealtime extends RealtimeObject { 105 long longValue; 106 int intValue; 107 SmallObjectRealtime refValue; 108 static final Factory FACTORY = new Factory() { 109 public Object create() { 110 return new SmallObjectRealtime(); 111 } 112 }; 113 } 114 115 private static final ObjectFactory CHAR128_FACTORY = new ObjectFactory() { 116 public Object create() { 117 return new char[128]; 118 } 119 }; 120 121 private static final ObjectFactory CHAR256_FACTORY = new ObjectFactory() { 122 public Object create() { 123 return new char[256]; 124 } 125 }; 126 } | Popular Tags |