python, Jupyter run time

1. the Python Time Time () method

import time

time_start=time.time()
time_end=time.time()
print('totally cost',time_end-time_start)

 

import time

print "time.time(): %f " %  time.time()
print time.localtime( time.time() )
print time.asctime( time.localtime(time.time()) )

Examples of the above output is:

time.time(): 1234892919.655932
(2009, 2, 17, 10, 48, 39, 1, 48, 0)
Tue Feb 17 10:48:39 2009

Python time time () returns the current time timestamp (after the 1970 era through the floating-point number of seconds )

Parameters: NA.

Returns: Returns the timestamp of the current time (post-1970 era through the floating-point number of seconds).

2.Jupyter Magic - Timing(%%time %time %timeit)

There are two very useful for timing the magic command: %%time and  %timeitif you have some code that runs to very slow, and you want to determine whether the problem lies here, these two commands will be very convenient.

(1).%%time We will give a time code running cell once it takes.

%%time
import time
for _ in range(1000):
    time.sleep(0.01)# sleep for 0.01 seconds
 
output:
CPU times: user 196 ms, sys: 21.4 ms, total: 217 ms
Wall time: 11.6 s

(2).%time It will give a time line of the current code to run once it takes.

import numpy
%time numpy.random.normal(size=1000)
 
output:
Wall time: 1e+03 µs

(3) %timeit using Python timeit module, it will execute a statement 100,000 (by default), and then gives the average run the fastest three times.

import numpy
%timeit numpy.random.normal(size=100)
 
output:
12.8 µs ± 1.25 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

 

Guess you like

Origin www.cnblogs.com/yuehouse/p/11493901.html