How do I get time of a Python program's execut

2018-12-31 19:31发布

I have a command line program in Python that takes a while to finish. I want to know the exact time it takes to finish running.

I've looked at the timeit module, but it seems it's only for small snippets of code. I want to time the whole program.

标签: python time
25条回答
初与友歌
2楼-- · 2018-12-31 19:44

I used a very simple function to timing a part of code execution:

import time
def timing():
    start_time = time.time()
    return lambda x: print("[{:.2f}s] {}".format(time.time() - start_time, x))

And to use it, just call it before the code to measure to retrieve timing function, then call the function after the code with comments, and the time will appear in front of the comments, for example:

t = timing()
train = pd.read_csv('train.csv',
                        dtype={
                            'id': str,
                            'vendor_id': str,
                            'pickup_datetime': str,
                            'dropoff_datetime': str,
                            'passenger_count': int,
                            'pickup_longitude': np.float64,
                            'pickup_latitude': np.float64,
                            'dropoff_longitude': np.float64,
                            'dropoff_latitude': np.float64,
                            'store_and_fwd_flag': str,
                            'trip_duration': int,
                        },
                        parse_dates = ['pickup_datetime', 'dropoff_datetime'],
                   )
t("Loaded {} rows data from 'train'".format(len(train)))

Then the output will look like this:

[9.35s] Loaded 1458644 rows data from 'train'

I feel a little bit elegant this way.

查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 19:45

Just Use timeit module. It works with both Python 2 And Python 3

import timeit

start = timeit.default_timer()
#ALL THE PROGRAM STATEMETNS
stop = timeit.default_timer()
execution_time = stop - start

print("Program Executed in "+execution_time) #It returns time in sec

It returns in Seconds and you can have your Execution Time. Simple but you should write these in Main Function which starts program execution. If you want to get the Execution time even when you get error then take your parameter "Start" to it and calculate there like

def sample_function(start,**kwargs):
     try:
         #your statements
     Except:
         #Except Statements
         stop = timeit.default_timer()
         execution_time = stop - start
         print("Program Executed in "+execution_time)
查看更多
闭嘴吧你
4楼-- · 2018-12-31 19:45

Use line_profiler.

line_profiler will profile the time individual lines of code take to execute. The profiler is implemented in C via Cython in order to reduce the overhead of profiling.

from line_profiler import LineProfiler
import random

def do_stuff(numbers):
    s = sum(numbers)
    l = [numbers[i]/43 for i in range(len(numbers))]
    m = ['hello'+str(numbers[i]) for i in range(len(numbers))]

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()

The results will be:

Timer unit: 1e-06 s

Total time: 0.000649 s
File: <ipython-input-2-2e060b054fea>
Function: do_stuff at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def do_stuff(numbers):
     5         1           10     10.0      1.5      s = sum(numbers)
     6         1          186    186.0     28.7      l = [numbers[i]/43 for i in range(len(numbers))]
     7         1          453    453.0     69.8      m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
查看更多
若你有天会懂
5楼-- · 2018-12-31 19:46

The solution of rogeriopvl works fine, but if you want more specific info you can use the python built-in profiler. Check this page:

http://docs.python.org/library/profile.html

a profiler tells you a lot of useful information like the time spent in every function

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 19:46
from time import time
start_time = time()
...
end_time = time()
time_taken = end_time - start_time # time_taken is in seconds
hours, rest = divmod(time_taken,3600)
minutes, seconds = divmod(rest, 60)
查看更多
人气声优
7楼-- · 2018-12-31 19:46

For the data folks using Jupyter Notebooks

In a cell, you can use Jupyter's %%time magic command to measure the execution time:

%%time 
[ x**2 for x in range(10000)] 

Output
CPU times: user 4.54 ms, sys: 0 ns, total: 4.54 ms
Wall time: 4.12 ms

This will only capture the execution time of a particular cell. If you'd like to capture the execution time of the whole notebook (i.e. program), you can create a new notebook in the same directory and in the new notebook execute all cells:

Suppose the notebook above is called example_notebook.ipynb. In a new notebook within the same directory:

# Convert your notebook to a .py script:
!jupyter nbconvert --to script example_notebook.ipynb

# Run the example_notebook with -t flag for time
%run -t example_notebook

Output
IPython CPU timings (estimated): User : 0.00 s.
System : 0.00 s.
Wall time: 0.00 s.

查看更多
登录 后发表回答