Skip to content

Instantly share code, notes, and snippets.

@c0rp-aubakirov
Created February 9, 2016 11:17
Show Gist options
  • Save c0rp-aubakirov/fd797307ff5668d618ec to your computer and use it in GitHub Desktop.
Save c0rp-aubakirov/fd797307ff5668d618ec to your computer and use it in GitHub Desktop.
Test what is faster: comparing big decimals or cast to int and compare
@Test
public void testBigDecimal() throws Exception {
final SecureRandom random = new SecureRandom();
for (int j = 0; j < 30; j++) {
Timer.executeFunctionWithTimer(() -> {
int value = 0;
for (int i = 0; i < 1000000; i++) {
value = new BigDecimal(random.nextInt()).intValueExact();
}
return value > 10;
});
Timer.executeFunctionWithTimer(() -> {
BigDecimal value = null;
for (int i = 0; i < 1000000; i++) {
value = new BigDecimal(random.nextInt());
}
return value.compareTo(BigDecimal.TEN);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment