python / python/cpython

Make timeit CLI function available from python

未关闭
#119,164 3 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

This is a summary of the discussion and conclusion from the discourse thread, as suggested by @terryjreedy, but you can read the entire discussion there if you want more info :)

Feature or enhancement

Proposal:

Currently, timeit's Python interface offers timeit.timeit and timeit.repeat, and timeit.autorange. timeit's command-line interface provides a lot of features over these functions, which are meant for interactive use.

These include:

  • Printing and formatting to human-readable results, including unit conversion
  • Verbose mode
  • Automatically determining a number, if not provided, using autorange.
  • Warning if the tests are unreliable

It makes sense for these features to not be included in most of timeit's Python interface, since those are APIs that aren't only meant for interactive use. Printing, formatting, and warning aren't always needed when you can just get the return value and calculate what you need.

However, there is a use case for these interactive features to be available from Python as-well. Many quick tests are easier to do from the REPL or a proper IDE where you have variables and statement editing, rather than just passing the code as a plain string. This is especially true when some of the arguments can be multi-line python code.

There should be a way for users to get the human-friendly features when using timeit from the Python, and not just from the CLI.

timeit.run

The proposed way to do this is by splitting timeit.main to roughly 2 parts: argument parsing, and actually running the tests. The former will stay in timeit.main, and the latter will move to a new timeit.run function which will be suited for interactive use from Python.

I have opened a draft PR for this here, and will continue working on it in the following days: #119165

Example usage (draft)

import random
import time
import timeit

def test_random():
     if random.random() > 0.9:
             time.sleep(1)

timeit.run(test_random, verbose=True, number=0, time_unit="nsec")
# 1 loop -> 1.84e-05 secs
# 2 loops -> 2.09e-05 secs
# 5 loops -> 1.001 secs
#
# raw times: 1e+09 nsec, 5.04e+04 nsec, 1e+09 nsec, 2.001e+09 nsec, 3.69e+04 nsec
# 
# 5 loops, best of 5: 7380 nsec per loop
# :0: UserWarning: The test results are likely unreliable. The worst time (4.001e+08 nsec) was more than four times slower than # # the best time (7380 nsec).
Has this already been discussed elsewhere?

I have already discussed this feature proposal on Discourse

Links to previous discussion of this feature:

https://discuss.python.org/t/timeit-make-cli-function-available-from-python/53339

Linked PRs
  • gh-119165

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读入口点 timeit.main 以及将参数解析与测试执行分离的提案。查看 PR #119165 的草稿,以了解已经在进行的工作;完成的标准是提供提议的交互式 Python API,同时保留此处描述的命令行行为和功能。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。