I have written a simple program to test the performance of size() method in STL list and STL vector. For a STL list with 10M integers, it takes 0.17 sec. to get the size. However, for a STL vector with 10M integers, it takes 0.4 micro sec to get the size() in same machine.
There are some suggestions:
- Use vector instead of list.
- If the application need to check whether the list is empty or not, uses "list.emtpy()" instead of "list.size() != 0".
- use an extra counter variable to counting the size of a list.