簡體   English   中英

為什么我需要將這一行 %%timeit -n 100 作為 python 中文件的第一行?

[英]Why should I need to keep this line %%timeit -n 100 as the first line of the file in python?

我在%%timeit -n 100行之前輸入了此注釋行,但出現錯誤!

# using parallel programming
%%timeit -n 100  
summary = 0

for item in s:
    summary += item

Output:

UsageError: Line magic function %%timeit not found。

同樣,如果我將此行保留為 python 文件的第一個可執行行,那么就可以了。
像這樣:

%%timeit -n 100   
summary = 0
# using parallel programming
for item in s:
    summary += item

Output:

每個循環 2.47 毫秒 ± 239 微秒(平均值 ± 標准偏差。7 次運行,每次 100 次循環)

為什么會這樣? 為什么要保留在第一行?

你在使用 Jupyter 筆記本嗎?

%%timeit 將評估整個單元格的運行時間,而 %timeit 將僅評估當前行的運行時間。 如果您先添加注釋,則 %%timeit 不再適用,因為這個神奇的 function 不再是單元格的第一行。

每當您想使用魔術 function 時,請將其作為單元格的第一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM