Time execution of a Python statement or expression. The CPU and wall clock times are printed, and the value of the expression (if any) is returned. Note that under Win32, system time is always reported as 0, since it can not be measured. This function can b
(self,line='', cell=None, local_ns=None)
| 1202 | @needs_local_scope |
| 1203 | @line_cell_magic |
| 1204 | def time(self,line='', cell=None, local_ns=None): |
| 1205 | """Time execution of a Python statement or expression. |
| 1206 | |
| 1207 | The CPU and wall clock times are printed, and the value of the |
| 1208 | expression (if any) is returned. Note that under Win32, system time |
| 1209 | is always reported as 0, since it can not be measured. |
| 1210 | |
| 1211 | This function can be used both as a line and cell magic: |
| 1212 | |
| 1213 | - In line mode you can time a single-line statement (though multiple |
| 1214 | ones can be chained with using semicolons). |
| 1215 | |
| 1216 | - In cell mode, you can time the cell body (a directly |
| 1217 | following statement raises an error). |
| 1218 | |
| 1219 | This function provides very basic timing functionality. Use the timeit |
| 1220 | magic for more control over the measurement. |
| 1221 | |
| 1222 | .. versionchanged:: 7.3 |
| 1223 | User variables are no longer expanded, |
| 1224 | the magic line is always left unmodified. |
| 1225 | |
| 1226 | Examples |
| 1227 | -------- |
| 1228 | :: |
| 1229 | |
| 1230 | In [1]: %time 2**128 |
| 1231 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 1232 | Wall time: 0.00 |
| 1233 | Out[1]: 340282366920938463463374607431768211456L |
| 1234 | |
| 1235 | In [2]: n = 1000000 |
| 1236 | |
| 1237 | In [3]: %time sum(range(n)) |
| 1238 | CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s |
| 1239 | Wall time: 1.37 |
| 1240 | Out[3]: 499999500000L |
| 1241 | |
| 1242 | In [4]: %time print 'hello world' |
| 1243 | hello world |
| 1244 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 1245 | Wall time: 0.00 |
| 1246 | |
| 1247 | Note that the time needed by Python to compile the given expression |
| 1248 | will be reported if it is more than 0.1s. In this example, the |
| 1249 | actual exponentiation is done by Python at compilation time, so while |
| 1250 | the expression can take a noticeable amount of time to compute, that |
| 1251 | time is purely due to the compilation: |
| 1252 | |
| 1253 | In [5]: %time 3**9999; |
| 1254 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 1255 | Wall time: 0.00 s |
| 1256 | |
| 1257 | In [6]: %time 3**999999; |
| 1258 | CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s |
| 1259 | Wall time: 0.00 s |
| 1260 | Compiler : 0.78 s |
| 1261 | """ |