Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thospfuller/1bfaf33a225bc0679175095ae30cca05 to your computer and use it in GitHub Desktop.
Save thospfuller/1bfaf33a225bc0679175095ae30cca05 to your computer and use it in GitHub Desktop.
Java Autoboxing Performance: Unboxing Performance Example
@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;
@thospfuller
Copy link
Author

thospfuller commented Apr 5, 2024

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?.

java-unboxing-performance-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment