List comprehension strangeness

Calvin Spealman cspealma at redhat.com
Mon Jul 22 09:48:01 EDT 2019


It is impossible to diagnose without seeing more context. Specifically,
you'll need to share the code around this line. The whole function,
preferably.

On Mon, Jul 22, 2019 at 9:31 AM Nicholas Cole <nicholas.cole at gmail.com>
wrote:

> I was profiling a slow function in an application last week, and came
> across something that I still can’t explain. Inside a loop that was being
> called 4 times, inside a for loop that ran for a few dozen times there was
> a list compression of the form:
>
> [x.id for x in some_function()]
>
> According to the profiler, some_function was being called 52,000 times —
> many, many times more than the few dozen it should have been. And sure
> enough, removing the comprehension did indeed speed up the function.
>
> It is very strange and feels like a possible bug in python. Has anyone ever
> encountered anything similar?
>
> Best wishes,
>
> Nicholas
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

cspealma at redhat.com  M: +1.336.210.5107
[image: https://red.ht/sig] <https://red.ht/sig>
TRIED. TESTED. TRUSTED. <https://redhat.com/trusted>



More information about the Python-list mailing list