Apex Performance - Basic Object Instantiation
Tue May 14 2024 15:56:57 GMT+0000 (Coordinated Universal Time)
Saved by @Justus
/**
* This is a test to see how much CPU time it takes to generate
* 100k Cell object instances in a referenceable way and find the most efficient
* way to do so.
* Also calculate how long it takes to then reference each Object instance based
* on the object x,y,z indexes.
* Note: the generation of 100 records and adding to the map takes about 240ms
* so that needs to be deducted from the instantiation, but is still part
* of the calculation required for the solution
*/
// Test Settings
Integer numberOfSheets = 10; // wsi
Integer numberOfRows = 100; // ri
Integer numberOfColumns = 100; // ci
// Multidimentional array for storing the cells
Cell[][][] cells = new Cell[][][]{};
// Register start time
Decimal st = Limits.getCpuTime();
// Iterate the number of worksheets
for(Integer wsi=0; wsi < numberOfSheets; wsi++){
// Add a column / row array for each worksheet
cells.add(new Cell[][]{});
// Iterate the number of columns
for(Integer ri=0; ri < numberOfRows; ri++){
// Add a column array for each row
cells[wsi].add(new Cell[]{});
// Add Cells to the row
for(Integer ci = 0; ci < numberOfColumns; ci++){
cells[wsi][ri].add(new Cell(wsi,ri,ci));
}
}
}
// Register end time
Decimal et = Limits.getCpuTime();
// Loop through xyz
for(Integer wsi = 0; wsi < numberOfSheets; wsi++){
for(Integer ri=0; ri < numberOfRows; ri++){
for(Integer ci = 0; ci < numberOfColumns; ci++){
getCell(wsi, ri,ci);
}
}
}
// Register final end time
Decimal fet = Limits.getCpuTime();
// Output metrics
System.debug('Generation Time: ' + (et-st) + ' (' + (numberOfSheets * numberOfRows * numberOfColumns) + ' cells)');
System.debug('Reference Time: ' + (fet-et) + ' (' + (numberOfSheets * numberOfRows * numberOfColumns) + ' cells)');
/**
* Method to reference a cell at a certain position
*/
Cell getCell(Integer wsi, Integer ci, Integer ri){
return cells[wsi][ci][ri];
}
/**
* Basic class example of a cell
*/
class Cell{
Integer wsi;
Integer ri;
Integer ci;
Cell(Integer wsi, Integer ri, Integer ci){
this.wsi = wsi;
this.ri = ri;
this.ci = ci;
}
}



Comments