I need to maintain last 4 recent ids (It may not be in sequence). So I have added below code.
long timeStamp = System.currentTimeMillis();
Queue<Integer> autoIds = new LinkedList<Integer>(Arrays.asList(-1,-1,-1,-1));
for(int i =1;i<2147483647;i++){
autoIds.add(i);
autoIds.poll();
}
System.out.println(System.currentTimeMillis()-timeStamp);
// 25436 or 26771... little different numbers but more then 24500
System.out.println(recentUnregisteredModuleIds);
Later I thought removing size check in if condition, may increase performance so I removed and initialize queue with some dummy 4 values.
long timeStamp = System.currentTimeMillis();
Queue<Integer> autoIds = new LinkedList<Integer>();
for(int i =1;i<2147483647;i++){
autoIds.add(i);
if(autoIds.size()>4){
autoIds.poll();
}
}
System.out.println(System.currentTimeMillis()-timeStamp);
// 19949 or 20425 ... less then 21000 in different times
System.out.println(recentUnregisteredModuleIds);
But when I compare the execution time adding additional if condition to check queue size before poll is better in performance. I thought adding one additional condition will decrease the performance, here that's not true how ?