list.size()>0
जावा में list.isEmpty()
से धीमा क्यों है? दूसरे शब्दों पर isEmpty()
size()>0
से अधिक बेहतर है?जावा में list.isEmpty() से list.size()> 0 धीमी क्यों है?
जब मैं ArrayList
में कार्यान्वयन को देखो, तो यह गति की तरह दिखता है एक ही होना चाहिए:
ArrayList.size()
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
public int size() {
return size;
}
ArrayList.isEmpty()
/**
* Returns <tt>true</tt> if this list contains no elements.
*
* @return <tt>true</tt> if this list contains no elements
*/
public boolean isEmpty() {
return size == 0;
}
यदि हम समय लेने के लिए बस एक साधारण कार्यक्रम लिखते हैं वाई दोनों विधियों, उस मामले size()
सभी मामलों में isEmpty()
ले जाएगा, ऐसा क्यों?
यहां मेरा टेस्टकोड है;
import java.util.List;
import java.util.Vector;
public class Main {
public static void main(String[] args) {
List l=new Vector();
int i=0;
for(i=0;i<10000;i++){
l.add(new Integer(i).toString());
}
System.out.println(i);
Long sTime=System.nanoTime();
l.size();
Long eTime=System.nanoTime();
l.isEmpty();
Long eeTime=System.nanoTime();
System.out.println(eTime-sTime);
System.out.println(eeTime-eTime);
}
}
यहां सभी मामलों में eTime-sTime>eeTime-eTime
। क्यूं कर?
@JLR: हां मैं आपके बिंदु को स्वीकार करता हूं, जैसा कि दोष था 2 अलग-अलग परियोजनाओं में दोनों तरीकों का परीक्षण किया गया और दोनों बहुत ही समान परिणाम दे रहे हैं। मेरी संदेह –