Last active
April 5, 2024 11:17
-
-
Save thospfuller/1bfaf33a225bc0679175095ae30cca05 to your computer and use it in GitHub Desktop.
Java Autoboxing Performance: Unboxing 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 int[arrayCapacity + 1] | |
def monitor = MonitorFactory.start("Convert an ArrayList of Integer to an int array (int[]) performance monitor unboxing example.") | |
for (int outerCtr in 0..iterations) { | |
for (int ctr in 0..arrayCapacity) { | |
integerArray[ctr] = result.get(ctr) | |
} | |
} | |
monitor.stop () | |
println "Unboxing performance: $monitor" | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unboxing takes place on line #38.
See also the section on Java Unboxing Performance in the article entitled Is autoboxing wrecking your Java application's performance?.