lotrpy
1/13/2008 1:45:00 PM
On 1?13?, ??8?32?, Hrvoje Niksic <hnik...@xemacs.org> wrote:
> Use lambda when it works better for you, the speed difference is
> marginal in practice anyway. itemgetter is not (and was never
> intended to be) a general substitute for functions, as you've
> discovered.
>
> The marginal speed difference between itemgetter and an explicit
> lambda that does the subscripts is a consequence of itemgetter being
> written in C, meaning it avoids the creation of a Python stack frame,
> etc. Combining int(...) with this operation requires coding the key
> function in Python, which removes itemgetter's advantage. In other
> words, you cannot retain itemgetter's speed advantage with more
> complex keys. If the sorting performance is a problem for you, please
> give more details about what you're doing -- there might be better
> ways to speed up the code.
Fredrik and Hrvoje, thanks for the reply, here the sorting performance
is not a big problem for me. the text file is just several hundred
line, each line include several item separated by space. I'll run the
script each week or month, just 1 second to get the result,so maybe
stick to lambda here is just fine.