Created
April 7, 2024 11:39
-
-
Save thospfuller/081f95047a7bf25f8898c634baf10dc0 to your computer and use it in GitHub Desktop.
Java Autoboxing Performance: No Unboxing Required Performance Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab(group='com.jamonapi', module='jamon', version='2.82') | |
import com.jamonapi.Monitor | |
import com.jamonapi.MonitorFactory | |
import static java.util.Arrays.asList | |
import java.util.stream.Collectors | |
import java.util.List | |
import java.util.Arrays | |
import java.util.Random | |
def random = new Random() | |
def arrayCapacity = 10000 | |
def numbers = new java.lang.Integer[arrayCapacity + 1] | |
for (int ctr in 0..arrayCapacity) { | |
numbers[ctr] = java.lang.Integer.valueOf (random.nextInt()) | |
} | |
def iterations = 100000 | |
def result = null | |
def tempResult = asList (numbers) | |
result = new java.util.ArrayList (arrayCapacity) as java.util.List | |
result.addAll (tempResult) | |
def integerArray = new java.lang.Integer[arrayCapacity + 1] | |
def monitor = MonitorFactory.start("Convert an ArrayList of Integer to an array of type java.lang.Integer performance monitor no unboxing required example.") | |
for (int outerCtr in 0..iterations) { | |
for (int ctr in 0..arrayCapacity) { | |
integerArray[ctr] = result.get(ctr) | |
} | |
} | |
monitor.stop () | |
println "No unboxing required performance: $monitor" | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unboxing is not required in this example on line #38.
See also the section entitled Java performance example where unboxing is not required in the article entitled Is autoboxing wrecking your Java application's performance?.