EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Development::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=590)
-   -   list.IsEmpty() vs. list.Count()==0 (https://www.eqemulator.org/forums/showthread.php?t=35850)

Uleat 10-11-2012 11:11 PM

list.IsEmpty() vs. list.Count()==0
 
I'm all for not fixing something that isn't broke, so this is more of a muse than anything...


Would there be any realized benefits in converting 'empty list' checks to the 'list.isempty()' method?

There are a few in the code already, but there are a lot of 'list.count()==0' types in there as well.


It probably wouldn't do anything for small population servers, but it might save some cycles on heavy-use ones.

lerxst2112 10-12-2012 07:34 AM

It depends on the implementation of list. list::empty() is guaranteed to be a constant time check, where list::size() can be linear time. In practice most implementations of list are such that both are constant time operations since the size is cached.

The reason an implementation might choose to have list::size() be a linear time operation would be so list::splice() is a constant time operation rather than having to count how many nodes were spliced to update the count. The only implementations I know of that did it that way for that specific reason are the sgi / STLPort variations which we don't use.

For all standard library containers it is considered more correct to check for empty() rather than size() == 0, but I probably wouldn't refactor working code to change just that unless it was to fix an actual performance problem versus a theoretical one.

Uleat 10-12-2012 07:08 PM

Understood! Thanks lerxst!

KLS 10-17-2012 12:32 PM

He's probably referring to the fact that we use two different kinds of linked list classes in the code, STL and a linked list that we used pre-STL (because passable template support in MSVC is something that only came in 2003). In our homemade version LinkedList::Count is constant time because we cache the size and have no empty function.

Also interesting to know: while currently list::size can be in constant or linear time C++11 dictates that it is to be only in constant time.


All times are GMT -4. The time now is 02:39 PM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.