Monday, February 3, 2014

Java - Reflection and instanceof

import java.math.BigDecimal;

public class Test {

public static void main(String[] args) {
final int LOOP_COUNT = 100;
String result;
Object src = new BigDecimal("7");

long startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
if (src.getClass().getName().equals("java.math.BigDecimal")) {
result = "BigDecimal";
}
}

long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);

startTime = System.nanoTime();

for (int i = 0; i < LOOP_COUNT; i++) {
if (src instanceof java.math.BigDecimal) {
result = "BigDecimal";
}
}

endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;

System.out.println(elaspedTime);
}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.