This library is obsolete! Python 3.5's collections.OrderedDict was rewritten in C, and is now significantly faster than cyordereddict.OrderedDict for almost all operations.
The Python standard library's OrderedDict ported to Cython. A drop-in
replacement that is 2-6x faster.
- Install:
pip install cyordereddict- Dependencies:
- CPython (2.6, 2.7, 3.3 or 3.4) and a C compiler. Cython is only required for the dev version.
- Use:
from cyordereddict import OrderedDict
- Benchmarks:
Python 2.7:
Test Code Ratio (stdlib / cython) __init__emptyOrderedDict()1.8 __init__listOrderedDict(list_data)4.8 __init__dictOrderedDict(dict_data)4.6 __setitem__ordereddict[0] = 08.6 __getitem__ordereddict[0]3 updateordereddict.update(dict_data)5.5 __iter__list(ordereddict)5.6 itemsordereddict.items()5.9 __contains__0 in ordereddict2.3 Python 3.4:
Test Code Ratio (stdlib / cython) __init__emptyOrderedDict()1.5 __init__listOrderedDict(list_data)3.9 __init__dictOrderedDict(dict_data)4.2 __setitem__ordereddict[0] = 08.4 __getitem__ordereddict[0]2.9 updateordereddict.update(dict_data)6.5 __iter__list(ordereddict)2.3 itemslist(ordereddict.items())2.1 __contains__0 in ordereddict2.3 To run these yourself, use
cyordereddict.benchmark()- Cavaets:
cyorderedddict.OrderedDictis an extension type (similar to the built-indict) instead of a Python class. This is necessary for speed, but means that in a few pathological cases its behavior will differ fromcollections.OrderedDict:- The
inspectmodule does not work oncyorderedddict.OrderedDictmethods. - Extension types use slots instead of dictionaries, so you cannot add
custom attributes without making a subclass (e.g.,
OrderedDict.foo = 'bar'will fail).
You can do anything else you might do with an OrderedDict, including subclassing: everything else passes the
collections.OrderedDicttest suite. We based the Cython code directly on the Python standard library, and thus use separate code bases for Python 2 and 3, specifically to reduce the potential for introducing new bugs or performance regressions.- The
- License:
- MIT. Based on the Python standard library, which is under the Python Software Foundation License.