Как найти время работы программы python

In this article, we will discuss how to check the execution time of a Python script.

There are many Python modules like time, timeit and datetime module in Python which can store the time at which a particular section of the program is being executed. By manipulating or getting the difference between times of beginning and ending at which a particular section is being executed, we can calculate the time it took to execute the section. 

The following methods can be used to compute time difference:

  • Python time module provides various time-related functions. This module comes under Python’s standard utility modules. time.time() method of the Time module is used to get the time in seconds since epoch. The handling of leap seconds is platform-dependent.
  • Python datetime module defines a function that can be primarily used to get the current time and date. now() function Return the current local date and time, which is defined under the datetime module.
  • Python timeit module runs your snippet of code n number of times (the default value is, 1000000) so that you get the statistically most relevant measurement of code execution time.

Using the time module check the execution time of Python 

Example 1: Measuring time taken for a code segment by recording start and end times

Computing the time using the time module and time.time() function. We have computed the time of the above program, which came out of the order 10^-3. We can check for the time by increasing the number of computations using the same algorithms.

Python3

import time

start = time.time()

a = 0

for i in range(1000):

    a += (i**100)

end = time.time()

print("The time of execution of above program is :",

      (end-start) * 10**3, "ms")

Output:

The time of execution of above program is : 0.77056884765625 ms

Example 2: Measuring time taken for a code segment by adding up the time required per iteration

Checking times for execution of the program for different numbers of computations. We see a general trend in the increase in time of computation for an increase in the number of execution. However, it may not show any linear trend or fixed increments.

Python3

import time

for j in range(100, 5501, 100):

    start = time.time()

    a = 0

    for i in range(j):

        a += (i**100)

    end = time.time()

    print(f"Iteration: {j}tTime taken: {(end-start)*10**3:.03f}ms")

Output:

Iteration: 100    Time taken: 0.105ms
Iteration: 200    Time taken: 0.191ms
Iteration: 300    Time taken: 0.291ms
Iteration: 400    Time taken: 0.398ms
Iteration: 500    Time taken: 0.504ms
Iteration: 600    Time taken: 0.613ms
Iteration: 700    Time taken: 0.791ms
...
Iteration: 5400    Time taken: 6.504ms
Iteration: 5500    Time taken: 6.630ms

Explanation: Here we have truncated the output for representation purpose. But if we compare the iterations from 100 to 700 they are less than 1ms. But towards the end of the loop, each iteration taking ~7ms. Thus, there is an increase in time taken as the number of iterations have increased. This is generally because, the inner loop iterate more number of time depending on each outer iteration.

Using the DateTime module check the execution time

Using the datetime module in Python and datetime.now() function to record timestamp of start and end instance and finding the difference to get the code execution time.

Python3

from datetime import datetime

start = datetime.now()

a = 0

for i in range(1000):

    a += (i**100)

end = datetime.now()

td = (end - start).total_seconds() * 10**3

print(f"The time of execution of above program is : {td:.03f}ms")

Output:

The time of execution of above program is : 0.766ms

Using timeit module check the execution time

This would give us the execution time of any program. This module provides a simple way to find the execution time of small bits of Python code. It provides the timeit() method to do the same. The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments:

  • stmt which is the statement you want to measure; it defaults to ‘pass’.
  • setup, which is the code that you run before running the stmt; it defaults to ‘pass’. We generally use this to import the required modules for our code.
  • timer, which is a timeit.Timer object; usually has a sensible default value, so you don’t have to worry about it.
  • The number, which is the number of executions you’d like to run the stmt.

Example 1: Using timeit inside Python code snippet to measure execution time

Python3

import timeit

mysetup = "from math import sqrt"

mycode =

exec_time = timeit.timeit(stmt=mycode,

                          setup=mysetup,

                          number=1000000) * 10**3

print(f"The time of execution of above program is : {exec_time:.03f}ms")

Output:

The time of execution of above program is : 71.161ms

Example 2: Using timeit from command line to measure execution time

We can measure time taken by simple code statements without the need to write new Python files, using timeit CLI interface.

timeit supports various command line inputs, Here we will note a few of the mos common arguments:

  • -s [–setup]: Setup code to run before running the code statement.
  • -n [–number]: Number of times to execute the statement.
  • p [–process]: Measure the process time of the code execution, instead of the wall-clock time.
  • Statement: The code statements to test the execution time, taken as a positional argument.

timeit CLI statement:

python -m timeit -s "import random" "l = [x**9 for x in range(random.randint(1000, 1500))]"

Output:

500 loops, best of 5: 503 used per loop

Last Updated :
26 Apr, 2023

Like Article

Save Article

The simplest way in Python:

import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

This assumes that your program takes at least a tenth of second to run.

Prints:

--- 0.764891862869 seconds ---

Peter Mortensen's user avatar

answered Oct 13, 2009 at 0:00

rogeriopvl's user avatar

rogeriopvlrogeriopvl

51k8 gold badges54 silver badges58 bronze badges

10

In Linux or Unix:

$ time python yourprogram.py

In Windows, see this StackOverflow question: How do I measure execution time of a command on the Windows command line?

For more verbose output,

$ time -v python yourprogram.py
    Command being timed: "python3 yourprogram.py"
    User time (seconds): 0.08
    System time (seconds): 0.02
    Percent of CPU this job got: 98%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.10
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 9480
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 1114
    Voluntary context switches: 0
    Involuntary context switches: 22
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

vipulgupta2048's user avatar

answered Oct 12, 2009 at 23:59

steveha's user avatar

stevehasteveha

74.3k20 gold badges90 silver badges116 bronze badges

4

I put this timing.py module into my own site-packages directory, and just insert import timing at the top of my module:

import atexit
from time import clock

def secondsToStr(t):
    return "%d:%02d:%02d.%03d" % 
        reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
            [(t*1000,),1000,60,60])

line = "="*40
def log(s, elapsed=None):
    print line
    print secondsToStr(clock()), '-', s
    if elapsed:
        print "Elapsed time:", elapsed
    print line
    print

def endlog():
    end = clock()
    elapsed = end-start
    log("End Program", secondsToStr(elapsed))

def now():
    return secondsToStr(clock())

start = clock()
atexit.register(endlog)
log("Start Program")

I can also call timing.log from within my program if there are significant stages within the program I want to show. But just including import timing will print the start and end times, and overall elapsed time. (Forgive my obscure secondsToStr function, it just formats a floating point number of seconds to hh:mm:ss.sss form.)

Note: A Python 3 version of the above code can be found here or here.

Community's user avatar

answered Oct 13, 2009 at 2:08

PaulMcG's user avatar

PaulMcGPaulMcG

62k16 gold badges93 silver badges130 bronze badges

7

I like the output the datetime module provides, where time delta objects show days, hours, minutes, etc. as necessary in a human-readable way.

For example:

from datetime import datetime
start_time = datetime.now()
# do your work here
end_time = datetime.now()
print('Duration: {}'.format(end_time - start_time))

Sample output e.g.

Duration: 0:00:08.309267

or

Duration: 1 day, 1:51:24.269711

As J.F. Sebastian mentioned, this approach might encounter some tricky cases with local time, so it’s safer to use:

import time
from datetime import timedelta
start_time = time.monotonic()
end_time = time.monotonic()
print(timedelta(seconds=end_time - start_time))

Peter Mortensen's user avatar

answered Sep 29, 2014 at 11:55

metakermit's user avatar

metakermitmetakermit

20.9k12 gold badges85 silver badges95 bronze badges

3

import time

start_time = time.clock()
main()
print(time.clock() - start_time, "seconds")

time.clock() returns the processor time, which allows us to calculate only the time used by this process (on Unix anyway). The documentation says “in any case, this is the function to use for benchmarking Python or timing algorithms”

Shidouuu's user avatar

Shidouuu

3493 silver badges14 bronze badges

answered Oct 13, 2009 at 1:25

newacct's user avatar

newacctnewacct

119k29 gold badges162 silver badges223 bronze badges

3

I really like Paul McGuire’s answer, but I use Python 3. So for those who are interested: here’s a modification of his answer that works with Python 3 on *nix (I imagine, under Windows, that clock() should be used instead of time()):

#python3
import atexit
from time import time, strftime, localtime
from datetime import timedelta

def secondsToStr(elapsed=None):
    if elapsed is None:
        return strftime("%Y-%m-%d %H:%M:%S", localtime())
    else:
        return str(timedelta(seconds=elapsed))

def log(s, elapsed=None):
    line = "="*40
    print(line)
    print(secondsToStr(), '-', s)
    if elapsed:
        print("Elapsed time:", elapsed)
    print(line)
    print()

def endlog():
    end = time()
    elapsed = end-start
    log("End Program", secondsToStr(elapsed))

start = time()
atexit.register(endlog)
log("Start Program")

If you find this useful, you should still up-vote his answer instead of this one, as he did most of the work ;).

Georgy's user avatar

Georgy

12.1k7 gold badges65 silver badges73 bronze badges

answered Sep 10, 2012 at 2:03

Nicojo's user avatar

NicojoNicojo

1,0338 silver badges8 bronze badges

7

You can use the Python profiler cProfile to measure CPU time and additionally how much time is spent inside each function and how many times each function is called. This is very useful if you want to improve performance of your script without knowing where to start. This answer to another Stack Overflow question is pretty good. It’s always good to have a look in the documentation too.

Here’s an example how to profile a script using cProfile from a command line:

$ python -m cProfile euler048.py

1007 function calls in 0.061 CPU seconds

Ordered by: standard name
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    0.061    0.061 <string>:1(<module>)
 1000    0.051    0.000    0.051    0.000 euler048.py:2(<lambda>)
    1    0.005    0.005    0.061    0.061 euler048.py:2(<module>)
    1    0.000    0.000    0.061    0.061 {execfile}
    1    0.002    0.002    0.053    0.053 {map}
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler objects}
    1    0.000    0.000    0.000    0.000 {range}
    1    0.003    0.003    0.003    0.003 {sum}

Peter Mortensen's user avatar

answered Jan 2, 2014 at 0:35

jacwah's user avatar

jacwahjacwah

2,6872 gold badges21 silver badges42 bronze badges

3

Just use the timeit module. It works with both Python 2 and Python 3.

import timeit

start = timeit.default_timer()

# All the program statements
stop = timeit.default_timer()
execution_time = stop - start

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

It returns in seconds and you can have your execution time. It is simple, but you should write these in thew main function which starts program execution. If you want to get the execution time even when you get an error then take your parameter “Start” to it and calculate there like:

def sample_function(start,**kwargs):
     try:
         # Your statements
     except:
         # except statements run when your statements raise an exception
         stop = timeit.default_timer()
         execution_time = stop - start
         print("Program executed in " + str(execution_time))

djamaile's user avatar

djamaile

6752 gold badges12 silver badges30 bronze badges

answered Sep 18, 2017 at 19:08

Ravi Kumar's user avatar

Ravi KumarRavi Kumar

7727 silver badges8 bronze badges

1

time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead

import time
start_time = time.perf_counter ()
for x in range(1, 100):
    print(x)
end_time = time.perf_counter ()
print(end_time - start_time, "seconds")

Suraj Rao's user avatar

Suraj Rao

29.3k11 gold badges94 silver badges103 bronze badges

answered Jun 20, 2021 at 9:11

Md. Imrul Kayes's user avatar

1

time.clock()

Deprecated since version 3.3: The behavior of this function depends
on the platform: use perf_counter() or process_time() instead,
depending on your requirements, to have a well-defined behavior.

time.perf_counter()

Return the value (in fractional seconds) of a performance counter,
i.e. a clock with the highest available resolution to measure a short
duration. It does include time elapsed during sleep and is
system-wide.

time.process_time()

Return the value (in fractional seconds) of the sum of the system and
user CPU time of the current process. It does not include time elapsed
during sleep.

start = time.process_time()
... do something
elapsed = (time.process_time() - start)

answered May 18, 2016 at 3:49

Yas's user avatar

YasYas

4,8372 gold badges40 silver badges23 bronze badges

1

For the data folks using Jupyter Notebook

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.

Peter Mortensen's user avatar

answered Jul 28, 2018 at 16:48

Matt's user avatar

MattMatt

5,6601 gold badge43 silver badges40 bronze badges

The following snippet prints elapsed time in a nice human readable <HH:MM:SS> format.

import time
from datetime import timedelta

start_time = time.time()

#
# Perform lots of computations.
#

elapsed_time_secs = time.time() - start_time

msg = "Execution took: %s secs (Wall clock time)" % timedelta(seconds=round(elapsed_time_secs))

print(msg)    

answered Jul 1, 2016 at 22:24

Sandeep's user avatar

SandeepSandeep

28.1k3 gold badges32 silver badges24 bronze badges

1

Similar to the response from @rogeriopvl I added a slight modification to convert to hour minute seconds using the same library for long running jobs.

import time
start_time = time.time()
main()
seconds = time.time() - start_time
print('Time Taken:', time.strftime("%H:%M:%S",time.gmtime(seconds)))

Sample Output

Time Taken: 00:00:08

answered Mar 12, 2020 at 5:27

user 923227's user avatar

user 923227user 923227

2,4584 gold badges26 silver badges46 bronze badges

1

For functions, I suggest using this simple decorator I created.

def timeit(method):
    def timed(*args, **kwargs):
        ts = time.time()
        result = method(*args, **kwargs)
        te = time.time()
        if 'log_time' in kwargs:
            name = kwargs.get('log_name', method.__name__.upper())
            kwargs['log_time'][name] = int((te - ts) * 1000)
        else:
            print('%r  %2.22f ms' % (method.__name__, (te - ts) * 1000))
        return result
    return timed

@timeit
def foo():
    do_some_work()

# foo()
# 'foo'  0.000953 ms

answered Oct 29, 2020 at 10:24

Nikita Tonkoskur's user avatar

Nikita TonkoskurNikita Tonkoskur

1,4211 gold badge16 silver badges28 bronze badges

3

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)

The6thSense's user avatar

The6thSense

8,0837 gold badges31 silver badges64 bronze badges

answered Apr 6, 2016 at 7:45

Qina Yan's user avatar

Qina YanQina Yan

1,1969 silver badges5 bronze badges

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 -mtimeit -n1 -r1 -t -s "from your_module import main" "main()"

It runs your_module.main() function one time and print the elapsed time using time.time() function as a timer.

To emulate /usr/bin/time in Python see Python subprocess with /usr/bin/time: how to capture timing info but ignore all other output?.

To measure CPU time (e.g., don’t include time during time.sleep()) for each function, you could use profile module (cProfile on Python 2):

$ python3 -mprofile your_module.py

You could pass -p to timeit command above if you want to use the same timer as profile module uses.

See How can you profile a Python script?

answered Mar 3, 2015 at 9:04

jfs's user avatar

jfsjfs

395k191 gold badges974 silver badges1665 bronze badges

I was having the same problem in many places, so I created a convenience package horology. You can install it with pip install horology and then do it in the elegant way:

from horology import Timing

with Timing(name='Important calculations: '):
    prepare()
    do_your_stuff()
    finish_sth()

will output:

Important calculations: 12.43 ms

Or even simpler (if you have one function):

from horology import timed

@timed
def main():
    ...

will output:

main: 7.12 h

It takes care of units and rounding. It works with python 3.6 or newer.

answered Dec 7, 2019 at 22:05

hans's user avatar

hanshans

1,00312 silver badges33 bronze badges

4

I liked Paul McGuire’s answer too and came up with a context manager form which suited my needs more.

import datetime as dt
import timeit

class TimingManager(object):
    """Context Manager used with the statement 'with' to time some execution.

    Example:

    with TimingManager() as t:
       # Code to time
    """

    clock = timeit.default_timer

    def __enter__(self):
        """
        """
        self.start = self.clock()
        self.log('n=> Start Timing: {}')

        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        """
        self.endlog()

        return False

    def log(self, s, elapsed=None):
        """Log current time and elapsed time if present.
        :param s: Text to display, use '{}' to format the text with
            the current time.
        :param elapsed: Elapsed time to display. Dafault: None, no display.
        """
        print s.format(self._secondsToStr(self.clock()))

        if(elapsed is not None):
            print 'Elapsed time: {}n'.format(elapsed)

    def endlog(self):
        """Log time for the end of execution with elapsed time.
        """
        self.log('=> End Timing: {}', self.now())

    def now(self):
        """Return current elapsed time as hh:mm:ss string.
        :return: String.
        """
        return str(dt.timedelta(seconds = self.clock() - self.start))

    def _secondsToStr(self, sec):
        """Convert timestamp to h:mm:ss string.
        :param sec: Timestamp.
        """
        return str(dt.datetime.fromtimestamp(sec))

Peter Mortensen's user avatar

answered Jan 29, 2015 at 15:42

Gall's user avatar

GallGall

1,5851 gold badge14 silver badges22 bronze badges

In IPython, “timeit” any script:

def foo():
    %run bar.py
timeit foo()

Peter Mortensen's user avatar

answered May 20, 2015 at 14:40

B.Kocis's user avatar

B.KocisB.Kocis

1,93420 silver badges19 bronze badges

1

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))]

Peter Mortensen's user avatar

answered Mar 28, 2018 at 5:43

Yu Jiaao's user avatar

Yu JiaaoYu Jiaao

4,4045 gold badges42 silver badges57 bronze badges

1

I used a very simple function to time 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 function timing, and then call the function after the code with comments. 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'

Peter Mortensen's user avatar

answered Aug 7, 2018 at 5:42

Tao Wang's user avatar

Tao WangTao Wang

7048 silver badges5 bronze badges

0

I tried and found time difference using the following scripts.

import time

start_time = time.perf_counter()
[main code here]
print (time.perf_counter() - start_time, "seconds")

answered May 8, 2020 at 4:44

Hafez Ahmad's user avatar

Hafez AhmadHafez Ahmad

1752 silver badges6 bronze badges

1

Timeit is a class in Python used to calculate the execution time of small blocks of code.

Default_timer is a method in this class which is used to measure the wall clock timing, not CPU execution time. Thus other process execution might interfere with this. Thus it is useful for small blocks of code.

A sample of the code is as follows:

from timeit import default_timer as timer

start= timer()

# Some logic

end = timer()

print("Time taken:", end-start)

Peter Mortensen's user avatar

answered Nov 16, 2017 at 2:16

Utkarsh Dhawan's user avatar

0

First, install humanfriendly package by opening Command Prompt (CMD) as administrator and type there –
pip install humanfriendly

Code:

from humanfriendly import format_timespan
import time
begin_time = time.time()
# Put your code here
end_time = time.time() - begin_time
print("Total execution time: ", format_timespan(end_time))

Output:

enter image description here

Georgy's user avatar

Georgy

12.1k7 gold badges65 silver badges73 bronze badges

answered Apr 16, 2020 at 10:40

Amar Kumar's user avatar

Amar KumarAmar Kumar

2,3042 gold badges25 silver badges33 bronze badges

You do this simply in Python. There is no need to make it complicated.

import time

start = time.localtime()
end = time.localtime()
"""Total execution time in minutes$ """
print(end.tm_min - start.tm_min)
"""Total execution time in seconds$ """
print(end.tm_sec - start.tm_sec)

swateek's user avatar

swateek

6,6098 gold badges34 silver badges48 bronze badges

answered Feb 16, 2019 at 5:18

Mitul Panchal's user avatar

1

Later answer, but I use the built-in timeit:

import timeit
code_to_test = """
a = range(100000)
b = []
for i in a:
    b.append(i*2)
"""
elapsed_time = timeit.timeit(code_to_test, number=500)
print(elapsed_time)
# 10.159821493085474

  • Wrap all your code, including any imports you may have, inside code_to_test.
  • number argument specifies the amount of times the code should repeat.
  • Demo

answered Feb 25, 2020 at 0:15

Pedro Lobito's user avatar

Pedro LobitoPedro Lobito

92.6k30 gold badges249 silver badges266 bronze badges

3

Following this answer created a simple but convenient instrument.

import time
from datetime import timedelta

def start_time_measure(message=None):
    if message:
        print(message)
    return time.monotonic()

def end_time_measure(start_time, print_prefix=None):
    end_time = time.monotonic()
    if print_prefix:
        print(print_prefix + str(timedelta(seconds=end_time - start_time)))
    return end_time

Usage:

total_start_time = start_time_measure()    
start_time = start_time_measure('Doing something...')
# Do something
end_time_measure(start_time, 'Done in: ')
start_time = start_time_measure('Doing something else...')
# Do something else
end_time_measure(start_time, 'Done in: ')
end_time_measure(total_start_time, 'Total time: ')

The output:

Doing something...
Done in: 0:00:01.218000
Doing something else...
Done in: 0:00:01.313000
Total time: 0:00:02.672000

answered Nov 26, 2020 at 13:05

Nick Legend's user avatar

Nick LegendNick Legend

7181 gold badge5 silver badges19 bronze badges

I use tic and toc from ttictoc.

pip install ttictoc

Then you can use in your script:

from ttictoc import tic,toc
tic()

# foo()

print(toc())

answered May 7, 2022 at 14:19

Sasha Vasserfirer's user avatar

This is Paul McGuire’s answer that works for me. Just in case someone was having trouble running that one.

import atexit
from time import clock

def reduce(function, iterable, initializer=None):
    it = iter(iterable)
    if initializer is None:
        value = next(it)
    else:
        value = initializer
    for element in it:
        value = function(value, element)
    return value

def secondsToStr(t):
    return "%d:%02d:%02d.%03d" % 
        reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
            [(t*1000,),1000,60,60])

line = "="*40
def log(s, elapsed=None):
    print (line)
    print (secondsToStr(clock()), '-', s)
    if elapsed:
        print ("Elapsed time:", elapsed)
    print (line)

def endlog():
    end = clock()
    elapsed = end-start
    log("End Program", secondsToStr(elapsed))

def now():
    return secondsToStr(clock())

def main():
    start = clock()
    atexit.register(endlog)
    log("Start Program")

Call timing.main() from your program after importing the file.

Peter Mortensen's user avatar

answered Apr 8, 2015 at 0:24

Saurabh Rana's user avatar

Saurabh RanaSaurabh Rana

3,3301 gold badge18 silver badges22 bronze badges

After reading this article, you’ll learn: –

  • How to calculate the program’s execution time in Python
  • Measure the total time elapsed to execute the code block in seconds, milliseconds, minutes, and hours
  • Also, get the execution time of functions and loops.

In this article, We will use the following four ways to measure the execution time in Python: –

  • time.time() function: measure the the total time elapsed to execute the script in seconds.
  • time.process_time(): measure the CPU execution time of a code
  • timeit module: measure the execution time of a small piece of a code including the single line of code as well as multiple lines of code
  • DateTime module: measure the execution time in the hours-minutes-seconds format.

To measure the code performance, we need to calculate the time taken by the script/program to execute. Measuring the execution time of a program or parts of it will depend on your operating system, Python version, and what you mean by ‘time’.

Before proceeding further, first, understand what time is.

Table of contents

  • Wall time vs. CPU time
  • How to Measure Execution Time in Python
    • Example: Get Program’s Execution Time in Seconds
    • Get Execution Time in Milliseconds
    • Get Execution Time in Minutes
  • Get Program’s CPU Execution Time using process_time()
  • timeit module to measure the execution time of a code
    • Example: Measure the execution time of a function
    • Measure the execution time of a single line of code
    • Measure the execution time of a multiple lines of code
  • DateTime Module to determine the script’s execution time
  • Conclusion

Wall time vs. CPU time

We often come across two terms to measure the execution time: Wall clock time and CPU time.

So it is essential to define and differentiate these two terms.

  • Wall time (also known as clock time or wall-clock time) is simply the total time elapsed during the measurement. It’s the time you can measure with a stopwatch. It is the difference between the time at which a program finished its execution and the time at which the program started. It also includes waiting time for resources.
  • CPU Time, on the other hand, refers to the time the CPU was busy processing the program’s instructions. The time spent waiting for other task to complete (like I/O operations) is not included in the CPU time. It does not include the waiting time for resources.

The difference between the Wall time and CPU time can occur from architecture and run-time dependency, e.g., programmed delays or waiting for system resources to become available.

For example, a program reports that it has used “CPU time 0m0.2s, Wall time 2m4s”. It means the program was active for 2 minutes and four seconds. Still, the computer’s processor spent only 0.2 seconds performing calculations for the program. May be program was waiting for some resources to become available.

At the beginning of each solution, I listed explicitly which kind of time each method measures.

So depending upon why you are measuring your program’s execution time, you can choose to calculate the Wall or CPU time.

The Python time module provides various time-related functions, such as getting the current time and suspending the calling thread’s execution for the given number of seconds. The below steps show how to use the time module to calculate the program’s execution time.

  1. Import time module

    The time module comes with Python’s standard library. First, Import it using the import statement.

  2. Store the start time

    Now, we need to get the start time before executing the first line of the program. To do this, we will use the time() function to get the current time and store it in a ‘start_time‘ variable before the first line of the program.
    The time() function of a time module is used to get the time in seconds since epoch. The handling of leap seconds is platform-dependent.

  3. Store the end time

    Next, we need to get the end time before executing the last line.
    Again, we will use the time() function to get the current time and store it in the ‘end_time‘ variable before the last line of the program.

  4. Calculate the execution time

    The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.

Example: Get Program’s Execution Time in Seconds

Use this solution in the following cases: –

  • Determine the execution time of a script
  • Measure the time taken between lines of code.

Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.

import time

# get the start time
st = time.time()

# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
    sum_x += i

# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)

# get the end time
et = time.time()

# get the execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')

Output:

Sum of first 1 million numbers is: 499999500000
Execution time: 3.125561475753784 seconds

Note: It will report more time if your computer is busy with other tasks. If your script was waiting for some resources, the execution time would increase because the waiting time will get added to the final result.

Get Execution Time in Milliseconds

Use the above example to get the execution time in seconds, then multiply it by 1000 to get the final result in milliseconds.

Example:

# get execution time in milliseconds
res = et - st
final_res = res * 1000
print('Execution time:', final_res, 'milliseconds')

Output:

Sum of first 1 million numbers is: 499999500000
Execution time: 3125.988006591797 milliseconds

Get Execution Time in Minutes

Use the above example to get the execution time in seconds, then divide it by 60 to get the final result in minutes.

Example:

# get execution time in minutes
res = et - st
final_res = res / 60
print('Execution time:', final_res, 'minutes')

Output:

Sum of first 1 million numbers is: 499999500000
Execution time: 0.05200800895690918 minutes

Do you want better formatting?

Use the strftime() to convert the time in a more readable format like (hh-mm-ss) hours-minutes-seconds.

import time

st = time.time()
# your code
sum_x = 0
for i in range(1000000):
    sum_x += i
time.sleep(3)
print('Sum:', sum_x)

elapsed_time = time.time() - st
print('Execution time:', time.strftime("%H:%M:%S", time.gmtime(elapsed_time)))

Output:

Sum: 499999500000
Execution time: 00:00:03

Get Program’s CPU Execution Time using process_time()

The time.time() will measure the wall clock time. If you want to measure the CPU execution time of a program use the time.process_time() instead of time.time().

Use this solution if you don’t want to include the waiting time for resources in the final result. Let’s see how to get the program’s CPU execution time.

import time

# get the start time
st = time.process_time()

# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
    sum_x += i

# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)

# get the end time
et = time.process_time()

# get execution time
res = et - st
print('CPU Execution time:', res, 'seconds')

Output:

Sum of first 1 million numbers is: 499999500000
CPU Execution time: 0.234375 seconds

Note:

Because we are calculating the CPU execution time of a program, as you can see, the program was active for more than 3 seconds. Still, those 3 seconds were not added in CPU time because the CPU was ideal, and the computer’s processor spent only 0.23 seconds performing calculations for the program.

timeit module to measure the execution time of a code

Python timeit module provides a simple way to time small piece of Python code. It has both a Command-Line Interface as well as a callable one. It avoids many common traps for measuring execution times.

timeit module is useful in the following cases: –

  • Determine the execution time of a small piece of code such as functions and loops
  • Measure the time taken between lines of code.

The timeit() function: –

The timeit.timeit() returns the time (in seconds) it took to execute the code number times.

timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000, globals=None)

Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.

The below steps show how to measure the execution time of a code using the timeit module.

  • First, create a Timer instance using the timeit() function
  • Next, Pass a code at the place of the stmt argument. stmt is the code for which we want to measure the time
  • Next, If you wish to execute a few statements before your actual code, pass them to the setup argument like import statements.
  • To set a timer value, we will use the default timer provided by Python.
  • Next, decide how many times you want to execute the code and pass it to the number argument. The default value of number is 1,000,000.
  • In the end, we will execute the timeit() function with the above values to measure the execution time of the code

Example: Measure the execution time of a function

Here we will calculate the execution time of an ‘addition()’ function. We will run the addition() function five-time to get the average execution time.

import timeit

# print addition of first 1 million numbers
def addition():
    print('Addition:', sum(range(1000000)))

# run same code 5 times to get measurable data
n = 5

# calculate total execution time
result = timeit.timeit(stmt='addition()', globals=globals(), number=n)

# calculate the execution time
# get the average execution time
print(f"Execution time is {result / n} seconds")

Output:

Addition: 499999500000
Addition: 499999500000
Addition: 499999500000
Addition: 499999500000
Addition: 499999500000

Execution time is 0.03770382 seconds

Note:

If you run time-consuming code with the default number value, it will take a lot of time. So assign less value to the number argument Or decide how many samples do you want to measure to get the accurate execution time of a code.

  • The timeit() functions disable the garbage collector, which results in accurate time capture.
  • Also, using the timeit() function, we can repeat the execution of the same code as many times as we want, which minimizes the influence of other tasks running on your operating system. Due to this, we can get the more accurate average execution time.

Measure the execution time of a single line of code

Run the %timeit command on a command-line or jupyter notebook to get the execution time of a single line of code.

Example: Use %timeit just before the line of code

%timeit [x for x in range(1000)]

# Output
2.08 µs ± 223 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Also, we can customize the command using the various options to enhance the profiling and capture a more accurate execution time.

  • Define the number of runs using the -r option. For example, %timeit -r10 your_code means run the code line 10 times.
  • Define the loops within each run using the -r and -n option.
  • If you ommit the options be default it is 7 runs with each run having 1 million loops

Example: Customize the time profile operation to 10 runs and 20 loops within each run.

# Customizing number of runs and loops in %timeit
%timeit -r10 -n20 [x for x in range(1000)]

# output
1.4 µs ± 12.34 ns per loop (mean ± std. dev. of 10 runs, 20 loops each)

Measure the execution time of a multiple lines of code

Using the %%timeit command, we can measure the execution time of multiple lines of code. The command options will remain the same.

Note: you need to replace the single percentage (%) with double percentage (%%) in the timeit command to get the execution time of multiple lines of a code

Example:

# Time profiling using %%timeit
%%timeit -r5 -n10
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
    sum_x += i

# Output
10.5 µs ± 226 ns per loop (mean ± std. dev. of 5 runs, 10 loops each)

DateTime Module to determine the script’s execution time

Also, you can use the Datetime module to measure the program’s running time. Use the below steps.
Import DateTime module

  • Next, store the start time using the datetime.now() function before the first line of a script
  • Next, save the end time before using the same function before the last line of a script
  • In the end, calculate the execution time by subtracting the start time from an end time

Note: This solution measures the Wall time, i.e., total elapsed time, not a CPU time.

Example:

import datetime
import time

# get the start datetime
st = datetime.datetime.now()

# main program
# find sum to first 1 million numbers
sum_x = 0
for i in range(1000000):
    sum_x += i

# wait for 3 seconds
time.sleep(3)
print('Sum of first 1 million numbers is:', sum_x)

# get the end datetime
et = datetime.datetime.now()

# get execution time
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')

Output:

Sum of first 1 million numbers is: 499999500000
Execution time: 0:00:03.115498 seconds

Conclusion

Python provides several functions to get the execution time of a code. Also, we learned the difference between Wall-clock time and CPU time to understand which execution time we need to measure.

Use the below functions to measure the program’s execution time in Python:

  • time.time(): Measure the the total time elapsed to execute the code in seconds.
  • timeit.timeit(): Simple way to time a small piece of Python code
  • %timeit and %%timeit: command to get the execution time of a single line of code and multiple lines of code.
  • datetime.datetime.now(): Get execution time in hours-minutes-seconds format

Also, use the time.process_time() function to get the program’s CPU execution time.

In Python, we can measure the elapsed time on executing a code segment or a Python script using some built-in Python modules. Here we will cover the usage of time, timeit and datetime module.

Using Python timeit Module to measure elapsed time in Python

Python timeit module is often used to measure the execution time of small code snippets. We can also use the timeit() function, which executes an anonymous function with a number of executions. It temporarily turns off garbage collection while calculating the time of execution.

Example 1: Analyze how to use the timeit module

In this example, we will analyze how to use the timeit module and use it to find the execution time of a lambda expression. The code starts with importing the timeit module, and then we use the timeit() function from the module to find the execution time of the function.

Python3

import timeit

exec_time = timeit.timeit("print('Hello World!')")

print(exec_time, "secs.")

Output:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!
...
1.632629341998836 secs.

Explanation: The above code segment, will print “Hello World” 1000000 times (since default value of number parameter is 1000000). At the end, the code will print the execution time of the given code segment, measured in seconds.

Example 2: Using timeit.timeit() with predefined parameters

In this example, we’ll write a Python code segment having a function, and it’s call as a string and pass it to timeit.timeit() function. We’ll use a predefined number of iterations to count the execution time.

Python3

import timeit

code_segment =

exec_time = timeit.timeit(code_segment, number=10**6)

print(f"{exec_time:.03f} secs.")

Output:

1.118 secs.

Explanations: In this example, we have defined a Python function and call to the function, everything written as a string representation. We test the running time of the code for 10**6 times and print out the time taken to execute the given function in seconds.

Example 3: How to measure elapsed time using timeit.repeat

We use the timeit.repeat() method instead of timeit.timeit() which takes a repeat parameter and saves you the trouble of creating a loop and storing the values in the array. This helps in getting an average elapsed time value from multiple execution of the same code segment.

Python3

import timeit

def print_square(x):

    return x ** 2

t_records = timeit.repeat(lambda: print_square(3), number=10, repeat=5)

for index, exec_time in enumerate(t_records, 1):

    m_secs = round(exec_time * 10 ** 6, 2)

    print(f"Case {index}: Time Taken: {m_secs}µs")

Output:

Case 1: Time Taken: 4.41µs
Case 2: Time Taken: 3.15µs
Case 3: Time Taken: 3.07µs
Case 4: Time Taken: 3.04µs
Case 5: Time Taken: 3.08µs

Example 4: How to measure elapsed time using timeit.default_timer()

timeit.default_timer() uses the timeit.perf_counter() to record the timestamp of an instance in nanoseconds, and we can subtract end time from start time to get the execution time duration in nanoseconds.

Python3

import timeit

def print_square(x):

    return x ** 2

t_0 = timeit.default_timer()

res = print_square(11111111)

t_1 = timeit.default_timer()

elapsed_time = round((t_1 - t_0) * 10 ** 6, 3)

print(f"Elapsed time: {elapsed_time} µs")

Output:

Elapsed time: 1.266 µs

Using Python time Module to measure elapsed time in Python

In Python time module, there are different methods to record and find the execution time of a given code segment, we covered the usage of the following methods: time.perf_counter(), time.time_ns(), time.process_time(), time.time()

Example 1: How to measure elapsed time using time.perf_counter()

time.perf_counter() method records the time in seconds time unit. Since our sample function is very simple, so, we need to convert it to micro seconds to get time difference value in readable format.

Python3

import time

def print_square(x):

    return x ** 2

start = time.perf_counter()

print_square(3)

end = time.perf_counter()

ms = (end-start) * 10**6

print(f"Elapsed {ms:.03f} micro secs.")

Output:

Elapsed 1.014 micro secs.

Example 2: How to measure elapsed time using time.time_ns()

To measure the elapsed time or execution time of a block of code in nanoseconds, we can use the time.time_ns() function. This follows the same syntax as the time.time() function, like recording the time before and after the lines of the code and then subtracting the values and then printing them to the screen, but it records in nanoseconds instead of seconds.

Python3

import time

def print_square(x):

    return (x**2)

start = time.time_ns()

print_square(3)

end = time.time_ns()

print("Time taken", end-start, "ns")

Output:

Time taken 2671 ns

Example 3: How to measure elapsed time using time.process_time()

time.process_time() function returns the sum of the system and the user CPU time. This follows the same syntax as the time.time() function, like recording the time before and after the lines of the code and then subtracting the values, and then printing them to the screen.

Python3

import time

def print_square(x):

    return x ** 76567

start = time.process_time()

print_square(125)

end = time.process_time()

print("Elapsed time using process_time()", (end - start) * 10**3, "ms.")

Elapsed time using process_time() 12.583209999999998 ms.

Using Python datetime Module to measure elapsed time in Python

we can also use Python datetime module, we can also record time and find the execution time of a block of code. The process is same as using time.time(), measuring start and end time and then calculating the difference.

Example: How to measure elapsed time using datetime.datetime.now()

Python3

from datetime import datetime

def print_square(x):

    return x ** 2

start = datetime.now()

print_square(3)

end = datetime.now()

print("Elapsed", (end - start).total_seconds() * 10**6, "µs")

Output:

Elapsed 12.0 µs

Last Updated :
31 Aug, 2022

Like Article

Save Article

Перейти к содержимому

Меню

Допустим, вам необходимо узнать, сколько времени занимает выполнение той или иной функции. Используя модуль time, вы можете рассчитать это время.

import time

startTime = time.time() # время начала замера

# здесь пишем код, время которого необходимо измерить

endTime = time.time() #время конца замера
totalTime = endTime - startTime #вычисляем затраченное время

print("Время, затраченное на выполнение данного кода = ", totalTime)

Добавить комментарий