In[1]:%%time
1
CPU times: user 4µs, sys:0 ns, total:4µs
Wall time:5.96µs
Out[1]:1In[2]:%%time
# Notice there is no out result in this case.
x =1
x
CPU times: user 3µs, sys:0 ns, total:3µs
Wall time:5.96µs
I would like to get the time spent on the cell execution in addition to the original output from cell.
To this end, I tried %%timeit -r1 -n1 but it doesn’t expose the variable defined within cell.
%%time works for cell which only contains 1 statement.
In[1]: %%time
1
CPU times: user 4 µs, sys: 0 ns, total: 4 µs
Wall time: 5.96 µs
Out[1]: 1
In[2]: %%time
# Notice there is no out result in this case.
x = 1
x
CPU times: user 3 µs, sys: 0 ns, total: 3 µs
Wall time: 5.96 µs
The only way I found to overcome this problem is by executing the last statement with print.
Do not forget that cell magic starts with %% and line magic starts with %.
%%time
clf = tree.DecisionTreeRegressor().fit(X_train, y_train)
res = clf.predict(X_test)
print(res)
Notice that any changes performed inside the cell are not taken into consideration in the next cells, something that is counter intuitive when there is a pipeline:
[1]%%time
import pandas as pd
from pyspark.ml importPipelinefrom pyspark.ml.classification importLogisticRegressionimport numpy as np
.... code ....Output:-
CPU times: user 59.8 s, sys:4.97 s, total:1min4sWall time:1min18s
I simply added %%time at the beginning of the cell and got the time. You may use the same on Jupyter Spark cluster/ Virtual environment using the same. Just add %%time at the top of the cell and you will get the output. On spark cluster using Jupyter, I added to the top of the cell and I got output like below:-
[1] %%time
import pandas as pd
from pyspark.ml import Pipeline
from pyspark.ml.classification import LogisticRegression
import numpy as np
.... code ....
Output :-
CPU times: user 59.8 s, sys: 4.97 s, total: 1min 4s
Wall time: 1min 18s
回答 5
import time
start = time.time()"the code you want to test stays here"
end = time.time()print(end - start)
Sometimes the formatting is different in a cell when using print(res), but jupyter/ipython comes with a display. See an example of the formatting difference using pandas below.
%%time
import pandas as pd
from IPython.display import display
df = pd.DataFrame({"col0":{"a":0,"b":0}
,"col1":{"a":1,"b":1}
,"col2":{"a":2,"b":2}
})
#compare the following
print(df)
display(df)
The display statement can preserve the formatting.
回答 9
您可能还需要查看python的分析魔术命令%prun,该命令给出类似以下内容的信息-
def sum_of_lists(N):
total =0for i in range(5):
L =[j ^(j >> i)for j in range(N)]
total += sum(L)return total
然后
%prun sum_of_lists(1000000)
将返回
14 function calls in0.714 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)50.5990.1200.5990.120<ipython-input-19>:4(<listcomp>)50.0640.0130.0640.013{built-in method sum}10.0360.0360.6990.699<ipython-input-19>:1(sum_of_lists)10.0140.0140.7140.714<string>:1(<module>)10.0000.0000.7140.714{built-in method exec}
I find it useful when working with large chunks of code.
回答 10
遇到麻烦时,意味着什么:
?%timeit 要么 ??timeit
要获取详细信息:
Usage,in line mode:%timeit [-n<N>-r<R>[-t|-c]-q -p<P>-o] statement
orin cell mode:%%timeit [-n<N>-r<R>[-t|-c]-q -p<P>-o] setup_code
code
code...Time execution of a Python statement or expression using the timeit
module.This function can be used both as a line and cell magic:-In line mode you can time a single-line statement (though multiple
ones can be chained with using semicolons).-In cell mode, the statement in the first line is used as setup code
(executed but not timed)and the body of the cell is timed.The cell
body has access to any variables created in the setup code.
Usage, in line mode:
%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
or in cell mode:
%%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code
code
code...
Time execution of a Python statement or expression using the timeit
module. This function can be used both as a line and cell magic:
- In line mode you can time a single-line statement (though multiple
ones can be chained with using semicolons).
- In cell mode, the statement in the first line is used as setup code
(executed but not timed) and the body of the cell is timed. The cell
body has access to any variables created in the setup code.