Performance of list.index - how to speed up a silly algorithm?

Laszlo Nagy gandalf at shopzeus.com
Fri Apr 30 06:12:52 EDT 2010


>> The .index method does a linear search, checking on average 1/2 of the
>> items in the list. That's why it's so slow.
>>
>> In order to avoid that you could build a dict of each value in
>> dimension_values[col_idx] and its index in a single pass so that it
>> becomes a quick lookup.
>>     
>
> For example:
>
> from itertools import count, izip
>
>   
...
> def main():
>     # dimension_names = ["color", "size"]
>     dimension_cols = [0, 1]
>
>     # measure_names = ["weight", "value"]
>     measure_cols = [2, 3]
>
>     indexers = [Indexer() for _ in dimension_cols]
>
>     facts = pairs(iterator_factory(),
>                   dimension_cols, measure_cols, indexers)
>     facts = list(facts)
>
>     print facts
>     for i, indexer in enumerate(indexers):
>         print "%d: %s" % (i, indexer.to_list())
>
> if __name__ == "__main__":
>     main()
>   
Whew! :-) Thank you for taking the time. I'm not very familiar to 
itertools yet, so I need some time to understand this *beautiful* code. :-)

L




More information about the Python-list mailing list