Skip to content

Instantly share code, notes, and snippets.

@jongpie
Created January 30, 2024 00:23
Show Gist options
  • Save jongpie/d65bba24bc9c882cacc3c040479bfc7d to your computer and use it in GitHub Desktop.
Save jongpie/d65bba24bc9c882cacc3c040479bfc7d to your computer and use it in GitHub Desktop.
Apex - Unique ID Generation Benchmarks
@IsTest
private class UniqueIdBenchmarkingTests {
@IsTest
static void ulidBenchmark() {
Long ulidStartTime = System.now().getTime();
System.debug('ULID generation start: ' + System.now().getTime());
for (Integer i = 0; i < 1000; i++) {
ULID.generate();
}
Long ulidStopTime = System.now().getTime();
System.debug('ULID generation finished: ' + ulidStopTime + ', total time: ' + (ulidStopTime - ulidStartTime));
}
@IsTest
static void customUUIDBenchmark() {
Long customUUIDStartTime = System.now().getTime();
System.debug('Custom UUID generation start: ' + System.now().getTime());
for (Integer i = 0; i < 1000; i++) {
new UUID().getValue();
}
Long customUUIDStopTime = System.now().getTime();
System.debug('Custom UUID generation finished: ' + customUUIDStopTime + ', total time: ' + (customUUIDStopTime - customUUIDStartTime));
}
@IsTest
static void systemUUIDBenchmark() {
Long systemUUIDStartTime = System.now().getTime();
System.debug('System.UUID generation start: ' + systemUUIDStartTime);
for (Integer i = 0; i < 1000; i++) {
System.UUID.randomUUID();
}
Long systemUUIDStopTime = System.now().getTime();
System.debug('System.UUID generation finished: ' + systemUUIDStopTime + ', total time: ' + (systemUUIDStopTime - systemUUIDStartTime));
}
}
@jongpie
Copy link
Author

jongpie commented Jan 30, 2024

@jongpie
Copy link
Author

jongpie commented Mar 26, 2024

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