2009年2月23日星期一

instanceof vs class ==

class uses 224ms
instanceof uses 339ms
class uses 254ms
instanceof uses 339ms
class uses 214ms
instanceof uses 297ms
class uses 213ms
instanceof uses 298ms
class uses 212ms
instanceof uses 298ms
class uses 213ms

Testing code:

Object l = 1L;
Timer timer = new Timer();
int times = 100000000;

timer.start("class");
for(int i=0; i<times; i++){
boolean x = l.getClass() == Long.class;
x = l.getClass() == String.class;
}
timer.stop();

timer.start("instanceof");
for(int i=0; i<times; i++){
boolean x = l instanceof Long;
x = l instanceof String;
}
timer.stop();

沒有留言: