Skip to content

timeit documentation should describe caveats #76220

Open

Description

@warsaw
BPO 32039
Nosy @warsaw, @rhettinger, @vstinner, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/warsaw'
closed_at = None
created_at = <Date 2017-11-15.19:43:16.439>
labels = ['3.7', 'docs']
title = 'timeit documentation should describe caveats'
updated_at = <Date 2017-11-23.13:12:53.767>
user = 'https://github.com/warsaw'

bugs.python.org fields:

activity = <Date 2017-11-23.13:12:53.767>
actor = 'serhiy.storchaka'
assignee = 'barry'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2017-11-15.19:43:16.439>
creator = 'barry'
dependencies = []
files = []
hgrepos = []
issue_num = 32039
keywords = []
message_count = 6.0
messages = ['306303', '306310', '306314', '306315', '306327', '306815']
nosy_count = 4.0
nosy_names = ['barry', 'rhettinger', 'vstinner', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = None
url = 'https://bugs.python.org/issue32039'
versions = ['Python 3.6', 'Python 3.7']

Activity

  1. warsaw commented on Nov 15, 2017

    @warsaw
    MemberAuthor

    timeit is cool, but it's not appropriate for all performance testing. For example, since it uses only a single Python process, anything that's cached is not a good candidate for timeit profiling. The classic example is timing imports. E.g. these are not the stats you're looking for:

    $ python3 -mtimeit "import my.slow.module"

    The timeit documentation should describe this caveat (are there others?) and possibly point to both cProfile and the 3rd party perf package as alternative ways to gather performance information.

  2. self-assigned this
    on Nov 15, 2017
  3. added
    docs
    on Nov 15, 2017
  4. rhettinger commented on Nov 15, 2017

    @rhettinger
    Contributor

    Other caveats:

    • The tight, repeated loops tend to have perfect branch prediction rates and 100% L1 cache hit rates that are rarely present in real code.

      There should be links to why this matters:

    • Code timing list.append() or anything else that uses realloc() internally tends to be timing in clean environments where the realloc() extends in-place rather than recopying all the data. This is almost never true in real code.

    • Constant folding can produce misleading results.

      To time addition, use:

      python -m timeit -s "x=2" "x+3"
      

      rather than:

      python -m timeit "2+3"
      
    • Timings include global lookups, attribute lookups, and function call overhead in addition to the operation being timed.

      Use:

      python -m timeit -s "s=list(range(20))" -s "s_index=s.index" "s_index(5)"
      

      rather than:

      python -m timeit -s "s=list(range(20))"  "s.index(5)"
      
    • Comparative timings should be repeated and interleaved to show whether the timings are noisy or have been affected the processor switching to a lower clock speed to avoid overheating:

      time a
      time b
      time a         # expect this to be consistent with the first *a* timing
      time b         # expect this to be consistent with the first *b* timing
      
  5. serhiy-storchaka commented on Nov 15, 2017

    @serhiy-storchaka
    Member

    +1 to all said by Raymond. But this is rather the matter of a special article or blog post than a note in the documentation.

    Other example of caching is regular expressions.

    Mistakes:

    python3 -m timeit -s "it = iter(range(1000000000))" "next(it)"

    python3 -m timeit -s "a = []" "a.append(1)"

    I remember one non-obvious trap in which I got caught. The performance of the iteration of a dict was influenced by the fact that in new dicts the order of iteration coincided with the order of creating keys, which were allocated sequentially in memory. Thus refcounts were touched in adjacent objects. This usually is not happen in real programs.

  6. warsaw commented on Nov 15, 2017

    @warsaw
    MemberAuthor

    On Nov 15, 2017, at 17:00, Serhiy Storchaka <report@bugs.python.org> wrote:

    +1 to all said by Raymond. But this is rather the matter of a special article or blog post than a note in the documentation.

    Maybe a HOWTO on performance testing and analysis? We already have one on instrumenting with DTRACE/SystemTap, and we could point the timeit docs to this new HOWTO.

  7. vstinner commented on Nov 16, 2017

    @vstinner
    Member

    Barry: "... possibly point to both cProfile and the 3rd party perf package as alternative ways to gather performance information."

    FYI the timeit module of PyPy now emits a warning and points to the perf module: http://perf.readthedocs.io/

  8. serhiy-storchaka commented on Nov 23, 2017

    @serhiy-storchaka
    Member

    Yet one mistake: https://stackoverflow.com/questions/35014951/why-is-max-slower-than-sort

    $ python3 -m timeit -s 'import random;a=list(range(10000));random.shuffle(a)' 'a.sort();a[-1]'

    Here the list is sorted at first iteration, and all following iterations measure the speed of sorting a sorted list.

  9. transferred this issue fromon Apr 10, 2022
  10. removed their assignment
    on Apr 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions