【问题标题】:Benchmark code in specific unit in Jupyter NotebookJupyter Notebook 中特定单元的基准代码
【发布时间】:2021-03-27 09:55:16
【问题描述】:

我之前在 Juypter-Notebooks 中使用了 %timeit- 和 %%timeit-magics 来对我的部分代码进行基准测试。我目前的问题是,我希望 timeit-output 在同一个单元中,但是 the available options 不允许这样做。

我做了一些研究,在 Python 标准库中找到了timeit-module。 There is a command-line interface 这个模块正好允许:

python -m timeit --unit=msec 'for i in range(1000000): i**10'  # Prepend '!' to run in Jupyter

但是,我想要进行基准测试的部分取决于 Jupyter Notebook 范围内存在的变量,并且不能直接传递给 timeit-call。

有没有办法使用 stdlib timeit 做到这一点,或者是否有另一种方法可以在 Jupyter Notebook 中对代码部分进行基准测试?

【问题讨论】:

    标签: python jupyter-notebook command-line-arguments benchmarking timeit


    【解决方案1】:

    根据文档,您不能在终端中执行此操作:

    python -m timeit [-n N] [-r N] [-u U] [-s S] [-h] [statement ...] - 缺少可以测试代码块的参数。而直接在您的工作文件(或 test_you_name_it_file.py)中可以这样做。

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2019-10-20
      • 2020-02-08
      • 2017-09-04
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 2022-11-08
      相关资源
      最近更新 更多