Python 为什么timeit.timeit在与变量一起使用时出现错误?

Python 为什么timeit.timeit在与变量一起使用时出现错误?,python,python-3.x,timeit,Python,Python 3.x,Timeit,我一直在尝试执行一个代码片段,以了解执行所需的时间。 我试过两种方法来做这件事。一种是在timeit.timeit函数中使用变量并检查。另一种是直接使用该值并进行检查。 第二种方法运行良好,但在使用第一种方法时,我面临一些范围问题。附件是两种场景的图像。 在这方面有人能帮我吗?非常感谢对这些问题的任何建议。问题在于s='Hello World'和s.endswith('d') 是两个独立的语句,因此它们必须在不同的行中,或者必须使用分号分隔 所以把它改成 timeit.timein("s =

我一直在尝试执行一个代码片段,以了解执行所需的时间。 我试过两种方法来做这件事。一种是在timeit.timeit函数中使用变量并检查。另一种是直接使用该值并进行检查。 第二种方法运行良好,但在使用第一种方法时,我面临一些范围问题。附件是两种场景的图像。


在这方面有人能帮我吗?非常感谢对这些问题的任何建议。

问题在于
s='Hello World'
s.endswith('d')

是两个独立的语句,因此它们必须在不同的行中,或者必须使用分号分隔

所以把它改成

timeit.timein("s = 'Hello World'; s.endswith('d')",number=10000)

问题是
s='Hello World'
s.endswith('d')

是两个独立的语句,因此它们必须在不同的行中,或者必须使用分号分隔

所以把它改成

timeit.timein("s = 'Hello World'; s.endswith('d')",number=10000)

我猜您没有太多的python编程经验,否则SyntaxError就足够清晰了。给出的例外说明语法(即代码行)无效

有效()代码。但更难阅读,因此不建议:

s='Hello world'; s.endswith('d')
s='Hello world'  s.endswith('d')
无效代码:

s='Hello world'; s.endswith('d')
s='Hello world'  s.endswith('d')
后者将引发一个异常,该异常将尝试突出显示由“^”引起的异常的确切位置

s='Hello world'  s.endswith('d')
  File "<stdin>", line 1
    s='Hello world'  s.endswith('d')
                     ^
SyntaxError: invalid syntax
将其放入名为mytest.py的文件中,并从命令行运行它:

python mytest.py

我猜您没有太多的python编程经验,否则SyntaxError就足够清晰了。给出的例外说明语法(即代码行)无效

有效()代码。但更难阅读,因此不建议:

s='Hello world'; s.endswith('d')
s='Hello world'  s.endswith('d')
无效代码:

s='Hello world'; s.endswith('d')
s='Hello world'  s.endswith('d')
后者将引发一个异常,该异常将尝试突出显示由“^”引起的异常的确切位置

s='Hello world'  s.endswith('d')
  File "<stdin>", line 1
    s='Hello world'  s.endswith('d')
                     ^
SyntaxError: invalid syntax
将其放入名为mytest.py的文件中,并从命令行运行它:

python mytest.py

如果要运行多条语句,只需在代码中使用三重引号即可。例如:

import timeit
code = """
s = 'Hello world'
s.endswith('d')
"""
timeit.timeit(code, number=10000)

如果要运行多条语句,只需在代码中使用三重引号即可。例如:

import timeit
code = """
s = 'Hello world'
s.endswith('d')
"""
timeit.timeit(code, number=10000)

其他人已经解决了主要问题(传递给
timeit()
的代码无效),我只想指出,使用常用的解决方案(在两条语句之间添加分号),您将最终对两条语句组合的总成本进行基准测试(创建文本字符串
“Hello world”
,将其分配给变量,并在此变量上调用
endswith('d')
)。假设您真正感兴趣的只是第二条语句的成本,您可能希望使用
timeit()

import timeit
timeit.timeit("s.endwith('d')", "s = 'Hello World'", number=10000)
它将执行两条语句,但只对第一条语句进行基准测试

如果要对从模块导入的函数进行基准测试,这也很有用:

timeit.timeit("re.search(r'42', 'hello world')", "import re")
或者从当前脚本或交互式shell会话:

$ python
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
>>>    return 42
>>> import timeit
>>> timeit.timeit("foo()", "from __main__ import foo")

其他人已经解决了主要问题(传递给
timeit()
的代码无效),我只想指出,使用常用的解决方案(在两条语句之间添加分号),您将最终对两条语句组合的总成本进行基准测试(创建文本字符串
“Hello world”
,将其分配给变量,并在此变量上调用
endswith('d')
)。假设您真正感兴趣的只是第二条语句的成本,您可能希望使用
timeit()

import timeit
timeit.timeit("s.endwith('d')", "s = 'Hello World'", number=10000)
它将执行两条语句,但只对第一条语句进行基准测试

如果要对从模块导入的函数进行基准测试,这也很有用:

timeit.timeit("re.search(r'42', 'hello world')", "import re")
或者从当前脚本或交互式shell会话:

$ python
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo():
>>>    return 42
>>> import timeit
>>> timeit.timeit("foo()", "from __main__ import foo")

. 这是一个错误,阻止了基于文本的搜索,并降低了文章的整体呈现。在
timeit
调用中有两条语句,您需要用
将它们分开
或换行符
\n
。因为您使用的是Jupyter笔记本,所以您可以使用cell magic。将
%%timeit
放在第一行将对该单元格进行基准测试。。这是一个错误,阻止了基于文本的搜索,并降低了文章的整体呈现。在
timeit
调用中有两条语句,您需要用
将它们分开
或换行符
\n
。因为您使用的是Jupyter笔记本,所以您可以使用cell magic。将
%%timeit
放在第一行将对该单元格进行基准测试。