MCPcopy Index your code
hub / github.com/ipython/ipython / time

Method time

IPython/core/magics/execution.py:1290–1465  ·  view source on GitHub ↗

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 be used b

(self, line="", cell=None, local_ns=None)

Source from the content-addressed store, hash-verified

1288 @line_cell_magic
1289 @output_can_be_silenced
1290 def time(self, line="", cell=None, local_ns=None):
1291 """Time execution of a Python statement or expression.
1292
1293 The CPU and wall clock times are printed, and the value of the
1294 expression (if any) is returned. Note that under Win32, system time
1295 is always reported as 0, since it can not be measured.
1296
1297 This function can be used both as a line and cell magic:
1298
1299 - In line mode you can time a single-line statement (though multiple
1300 ones can be chained with using semicolons).
1301
1302 - In cell mode, you can time the cell body (a directly
1303 following statement raises an error).
1304
1305 This function provides very basic timing functionality. Use the timeit
1306 magic for more control over the measurement.
1307
1308 .. versionchanged:: 7.3
1309 User variables are no longer expanded,
1310 the magic line is always left unmodified.
1311
1312 .. versionchanged:: 8.3
1313 The time magic now correctly propagates system-exiting exceptions
1314 (such as ``KeyboardInterrupt`` invoked when interrupting execution)
1315 rather than just printing out the exception traceback.
1316 The non-system-exception will still be caught as before.
1317
1318 Examples
1319 --------
1320 ::
1321
1322 In [1]: %time 2**128
1323 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1324 Wall time: 0.00
1325 Out[1]: 340282366920938463463374607431768211456L
1326
1327 In [2]: n = 1000000
1328
1329 In [3]: %time sum(range(n))
1330 CPU times: user 1.20 s, sys: 0.05 s, total: 1.25 s
1331 Wall time: 1.37
1332 Out[3]: 499999500000L
1333
1334 In [4]: %time print('hello world')
1335 hello world
1336 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
1337 Wall time: 0.00
1338
1339 .. note::
1340 The time needed by Python to compile the given expression will be
1341 reported if it is more than 0.1s.
1342
1343 In the example below, the actual exponentiation is done by Python
1344 at compilation time, so while the expression can take a noticeable
1345 amount of time to compute, that time is purely due to the
1346 compilation::
1347

Callers 3

gilsleepFunction · 0.80
prepare_headerMethod · 0.80
assert_interruptsMethod · 0.80

Calls 8

UsageErrorClass · 0.90
clockFunction · 0.90
clock2Function · 0.90
_format_timeFunction · 0.85
parse_argstringMethod · 0.80
ast_parseMethod · 0.80
transform_astMethod · 0.80
transform_cellMethod · 0.45

Tested by 1

assert_interruptsMethod · 0.64