Skip to content

Instantly share code, notes, and snippets.

@petebankhead
Last active July 1, 2025 20:08
Show Gist options
  • Save petebankhead/ea559265bb5e87ede3eb9c700b26c584 to your computer and use it in GitHub Desktop.
Save petebankhead/ea559265bb5e87ede3eb9c700b26c584 to your computer and use it in GitHub Desktop.
QuPath script to benchmark tile reading performance
/**
* QuPath script to time how quickly tiles can be read from the current image.
*
* Written for https://forum.image.sc/t/issue-with-qupath-and-image-with-20-channels/110900/12
* to explore the effect of internal and external storage.
*
* Written for QuPath v0.6.0
*/
// Get the current image
var server = getCurrentServer()
// Get all available tiles
var tiles = server.getTileRequestManager().getAllTileRequests()
// Request tiles in parallel (here, just using the default common thread pool)
boolean doParallel = true
// Impatient... limit the number of tiles
int maxTiles = 10000
if (tiles.size() > maxTiles)
tiles = tiles[0..maxTiles]
// Ensure we aren't relying on the cache
getQuPath().getImageRegionStore().clearCache()
// Request each tile & count the number of pixels it contains (treating bands separately)
var stream = tiles.stream()
if (doParallel)
stream = stream.parallel()
long startTime = System.currentTimeMillis()
long nPixels = stream.mapToLong {
var img = server.readRegion(it.getRegionRequest())
return img.getWidth() * img.getHeight() * img.getRaster().getNumBands() as long
}.sum()
long endTime = System.currentTimeMillis()
long timeMillis = (endTime - startTime)
double timeSeconds = timeMillis/1000.0
// Print results
println "Total time: ${GeneralTools.formatNumber(timeSeconds, 1)} seconds"
println "Tiles per second: ${GeneralTools.formatNumber(tiles.size()/timeSeconds, 1)}"
println "Pixels per ms: ${GeneralTools.formatNumber(nPixels/timeMillis, 1)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment