@@ -52,6 +52,7 @@ void measureClientCreate() throws Exception {
5252 for (int i = 0 ; i < 10_000 ; i ++) {
5353 defaultClient .create (UserService .class , userToken );
5454 defaultClient .create2 (UserService .class , userToken );
55+ defaultClient .create3 (UserService .class , userToken );
5556 }
5657
5758 // Get ThreadMXBean for accurate memory allocation tracking
@@ -92,22 +93,45 @@ void measureClientCreate() throws Exception {
9293 System .out .println ("> Second loop avg time per call: " + (elapsedTimeInNs2 / (double ) iterations ) + " ns" );
9394 System .out .println ("> Second loop avg memory per call: " + (allocated2 / (double ) iterations ) + " bytes" );
9495
95- // Performance comparison
96- if (elapsedTimeInNs1 < elapsedTimeInNs2 ) {
97- double timesFaster = (double ) elapsedTimeInNs2 / elapsedTimeInNs1 ;
98- System .out .println ("> create is " + String .format ("%.2fx" , timesFaster ) + " faster than create2" );
99- } else {
100- double timesFaster = (double ) elapsedTimeInNs1 / elapsedTimeInNs2 ;
101- System .out .println ("> create2 is " + String .format ("%.2fx" , timesFaster ) + " faster than create" );
96+ // Measure third test
97+ long allocatedBefore3 = threadBean .getCurrentThreadAllocatedBytes ();
98+ startTime = System .nanoTime ();
99+ for (int i = 0 ; i < iterations ; i ++) {
100+ defaultClient .create3 (UserService .class , userToken );
102101 }
102+ endTime = System .nanoTime ();
103+ long allocatedAfter3 = threadBean .getCurrentThreadAllocatedBytes ();
104+ long elapsedTimeInNs3 = endTime - startTime ;
105+ long allocated3 = allocatedAfter3 - allocatedBefore3 ;
106+
107+ System .out .println ("> Third loop elapsed time: " + TimeUnit .NANOSECONDS .toMillis (elapsedTimeInNs3 ) + " ms" );
108+ System .out .println ("> Third loop memory allocated: " + (allocated3 / 1024 / 1024 ) + " MB" );
109+ System .out .println ("> Third loop avg time per call: " + (elapsedTimeInNs3 / (double ) iterations ) + " ns" );
110+ System .out .println ("> Third loop avg memory per call: " + (allocated3 / (double ) iterations ) + " bytes" );
111+
112+ // Performance comparison - Time
113+ long fastestTime = Math .min (elapsedTimeInNs1 , Math .min (elapsedTimeInNs2 , elapsedTimeInNs3 ));
114+ String fastestMethod = "" ;
115+ if (fastestTime == elapsedTimeInNs1 ) fastestMethod = "create" ;
116+ else if (fastestTime == elapsedTimeInNs2 ) fastestMethod = "create2" ;
117+ else fastestMethod = "create3" ;
103118
104- if (allocated1 < allocated2 ) {
105- double timesLess = (double ) allocated2 / allocated1 ;
106- System .out .println ("> create allocates " + String .format ("%.2fx" , timesLess ) + " less memory than create2" );
107- } else {
108- double timesLess = (double ) allocated1 / allocated2 ;
109- System .out .println ("> create2 allocates " + String .format ("%.2fx" , timesLess ) + " less memory than create" );
110- }
119+ System .out .println ("> Time comparison (fastest: " + fastestMethod + "):" );
120+ System .out .println (" - create: " + String .format ("%.2fx" , (double ) elapsedTimeInNs1 / fastestTime ));
121+ System .out .println (" - create2: " + String .format ("%.2fx" , (double ) elapsedTimeInNs2 / fastestTime ));
122+ System .out .println (" - create3: " + String .format ("%.2fx" , (double ) elapsedTimeInNs3 / fastestTime ));
123+
124+ // Performance comparison - Memory
125+ long leastMemory = Math .min (allocated1 , Math .min (allocated2 , allocated3 ));
126+ String mostEfficientMethod = "" ;
127+ if (leastMemory == allocated1 ) mostEfficientMethod = "create" ;
128+ else if (leastMemory == allocated2 ) mostEfficientMethod = "create2" ;
129+ else mostEfficientMethod = "create3" ;
130+
131+ System .out .println ("> Memory comparison (least: " + mostEfficientMethod + "):" );
132+ System .out .println (" - create: " + String .format ("%.2fx" , (double ) allocated1 / leastMemory ));
133+ System .out .println (" - create2: " + String .format ("%.2fx" , (double ) allocated2 / leastMemory ));
134+ System .out .println (" - create3: " + String .format ("%.2fx" , (double ) allocated3 / leastMemory ));
111135
112136 System .out .println ("=========================================================" );
113137 }
0 commit comments