print statements and profiling a function slowed performance

Chris Angelico rosuav at gmail.com
Sun Jun 29 03:14:01 EDT 2014


On Sun, Jun 29, 2014 at 4:14 PM, Dennis Lee Bieber
<wlfraed at ix.netcom.com> wrote:
> On Sat, 28 Jun 2014 23:50:26 +1000, Chris Angelico <rosuav at gmail.com>
> declaimed the following:
>
>>No, they were measured; but the exact difference will depend on Python
>>version, console, OS, etc, etc, etc, etc, etc, which is why I said
>>"maybe 100ms" - on one run it was about that (rounded aggressively to
>>make for convenient figures), but if I ran the test again, it might
>>come out at 50ms or 200ms. For what it's worth, I did the test on
>>Python 3.5(ish) on Linux; since you tested it on Python 2, a closer
>>comparison would be to use xrange rather than range, to avoid the RAM
>>overhead.
>>
>
>         I chose range() in order to reuse the list (generated outside the
> timing), rather than include the time of the iterator on both loops.
>
>         Ran it three times and only had differences in the noise.
>
>         The main incentive for my "subjective" comment is that I was getting a
> do-nothing loop that was ~half the time you reported, while the print #
> loop was ~twice your reported times.

My original point (printing to console can be quite slow) is supported
by your figures just as much as it is by mine; but the difference
between them is unsurprising, given the number of differences in the
test (list vs iterable range, Linux vs Windows, Py2 vs Py3). So, not
subjective, but still open to huge variation.

ChrisA



More information about the Python-list mailing list