Skip to content

Instantly share code, notes, and snippets.

@Andrew-William-Smith
Created September 4, 2018 19:03
Show Gist options
  • Save Andrew-William-Smith/e71e51155fbac1986e6069be4f8918ea to your computer and use it in GitHub Desktop.
Save Andrew-William-Smith/e71e51155fbac1986e6069be4f8918ea to your computer and use it in GitHub Desktop.
Allocation time measurement of simple Java data types
import java.io.FileWriter;
import java.io.IOException;
public class AllocationMeasurement {
public static void main(String[] args) {
try {
FileWriter csv = new FileWriter("mem.csv");
csv.write("Array length,Byte,Short,Int,Long,Object reference" + System.lineSeparator());
for (int i = 0; i < 30000; i += 10) {
long byteTime = System.nanoTime();
byte[] byteArr = new byte[i];
byteTime = System.nanoTime() - byteTime;
long shortTime = System.nanoTime();
short[] shortArr = new short[i];
shortTime = System.nanoTime() - shortTime;
long intTime = System.nanoTime();
int[] intArr = new int[i];
intTime = System.nanoTime() - intTime;
long longTime = System.nanoTime();
long[] longArr = new long[i];
longTime = System.nanoTime() - longTime;
long objectTime = System.nanoTime();
Object[] ObjectArr = new Object[i];
objectTime = System.nanoTime() - objectTime;
csv.write(String.format("%d,%d,%d,%d,%d,%d%s", i, byteTime, shortTime, intTime, longTime, objectTime, System.lineSeparator()));
}
csv.close();
}
catch (IOException e) {
System.out.println("Could not access file \"mem.csv\".");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment