Как посчитать время работы программы питон

In this article, we will learn to calculate the time taken by a program to execute in Python. We will use some built-in functions with some custom codes as well. Let’s first have a quick look over how the program’s execution affects the time in Python.

Programmers must have often suffered from «Time Limit Exceeded» error while building program scripts. In order to resolve this issue, we must optimize our programs to perform better. For that, we might need to know how much time the program is taking for its execution. Let us discuss different functions supported by Python to calculate the running time of a program in python.

The time of a Python program’s execution measure could be inconsistent depending on the following factors:

  1. The same program can be evaluated using different algorithms
  2. Running time varies between algorithms
  3. Running time varies between implementations
  4. Running time varies between computers
  5. Running time is not predictable based on small inputs

Calculate Execution Time using time() Function

We calculate the execution time of the program using time.time() function. It imports the time module which can be used to get the current time. The below example stores the starting time before the for loop executes, then it stores the ending time after the print line executes. The difference between the ending time and starting time will be the running time of the program. time.time() function is best used on *nix.

import time

#starting time
start = time.time()

for i in range(3):
    print("Hello")

# end time
end = time.time()

# total time taken
print("Execution time of the program is- ", end-start)

Hello
Hello
Hello
Execution time of the program is- 1.430511474609375e-05

Calculate execution time using timeit() function

We calculate the execution time of the program using timeit() function. It imports the timeit module. The result is the execution time in seconds. This assumes that your program takes at least a tenth of a second to run.

The below example creates a variable and wraps the entire code including imports inside triple quotes. The test code acts as a string. Now, we call the time.timeit() function. The timeit() function accepts the test code as an argument, executes it, and records the execution time. The value of the number argument is set to 100 cycles.

import timeit

test_code = """
a = range(100000)
b = []
for i in a:
    b.append(i+2)
"""

total_time = timeit.timeit(test_code, number=200)
print("Execution time of the program is-", total_time)

Execution time of the program is- 4.26646219700342

Calculate execution time using time.clock() Function

Another function of the time module to measure the time of a program’s execution is time.clock() function. time.clock() measures CPU time on Unix systems, not wall time. This function is mainly used for benchmarking purposes or timing algorithms. time.clock() may return slightly better accuracy than time.time(). It returns the processor time, which allows us to calculate only the time used by this process. It is best used on Windows.

import time

t0= time.clock()
print("Hello")

t1 = time.clock() - t0

print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)

Hello
Time elapsed: -0.02442

Note:

time.clock() is «Deprecated since version 3.3». The behavior of this function depends on the platform. Instead, we can use perf_counter() or process_time() depending on the requirements or have a well-defined behavior.

time.perf_counter() — It returns 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() — It returns 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. For example,

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

Calculate execution time using datetime.now() Function

We calculate the elapsed time using datetime.datetime.now() from the datetime module available in Python. It does not make the script a multi-line string like in timeit(). This solution is slower than the timeit() since calculating the difference in time is included in the execution time. The output is represented as days, hours, minutes, etc

The below example saves the current time before any execution in a variable. Then call datetime.datetime.now() after the program execution to find the difference between the end and start time of execution.

import datetime

start = datetime.datetime.now()

list1 = [4, 2, 3, 1, 5]
list1.sort()

end = datetime.datetime.now()
print(end-start)

0:00:00.000007

Calculate execution time using %%time

We use %%time command to calculate the time elapsed by the program. This command is basically for the users who are working on Jupyter Notebook. This will only capture the wall time of a particular cell.

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

Why is timeit() the best way to measure the execution time of Python code?

1. You can also use time.clock() on Windows and time.time() on Mac or Linux. However, timeit() will automatically use either time.clock() or time.time() in the background depending on the operating system.

2. timeit() disables the garbage collector which could otherwise skew the results.

3. timeit() repeats the test many times to minimize the influence of other tasks running on your operating system.

Conclusion

In this article, we learned to calculate the time of execution of any program by using functions such as time(), clock(), timeit(), %%time etc. We also discussed the optimization of the python script. We learned about various functions and their uniqueness.

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

50.6k8 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

74k20 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

61.6k16 gold badges92 silver badges129 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.7k12 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

3413 silver badges14 bronze badges

answered Oct 13, 2009 at 1:25

newacct's user avatar

newacctnewacct

119k29 gold badges161 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

11.8k7 gold badges65 silver badges72 bronze badges

answered Sep 10, 2012 at 2:03

Nicojo's user avatar

NicojoNicojo

1,0138 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,6472 gold badges21 silver badges42 bronze badges

2

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

7527 silver badges8 bronze badges

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,7472 gold badges39 silver badges23 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 badges96 silver badges103 bronze badges

answered Jun 20, 2021 at 9:11

Md. Imrul Kayes's user avatar

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,5761 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

27.9k3 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,3984 gold badges25 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,4231 gold badge15 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,0437 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

393k189 gold badges969 silver badges1654 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,00412 silver badges32 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,5651 gold badge14 silver badges21 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,92420 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

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,4698 gold badges36 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

91.8k30 gold badges244 silver badges265 bronze badges

3

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

11.8k7 gold badges65 silver badges72 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

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

6881 gold badge5 silver badges19 bronze badges

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,2601 gold badge18 silver badges21 bronze badges

The time of a Python program’s execution measure could be inconsistent depending on:

  • Same program can be evaluated using different algorithms
  • Running time varies between algorithms
  • Running time varies between implementations
  • Running time varies between computers
  • Running time is not predictable based on small inputs

This is because the most effective way is using the «Order of Growth» and learn the Big «O» notation to do it properly.

Anyway, you can try to evaluate the performance of any Python program in specific machine counting steps per second using this simple algorithm:
adapt this to the program you want to evaluate

import time

now = time.time()
future = now + 10
step = 4 # Why 4 steps? Because until here already four operations executed
while time.time() < future:
    step += 3 # Why 3 again? Because a while loop executes one comparison and one plus equal statement
step += 4 # Why 3 more? Because one comparison starting while when time is over plus the final assignment of step + 1 and print statement
print(str(int(step / 10)) + " steps per second")

Peter Mortensen's user avatar

answered Jul 31, 2017 at 15:13

Manu's user avatar

ManuManu

1124 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.

Чтобы измерить время выполнения программы, используйте функции time.clock() или time.time(). Документы Python утверждают, что эта функция должна использоваться для целей тестирования.

Пример

import time
t0= time.clock()
print("Hello")
t1 = time.clock() - t0
print("Time elapsed: ", t1 - t0) # CPU seconds elapsed (floating point)

Вывод

Это даст вывод —

Time elapsed:  0.0009403145040156798

Вы также можете использовать модуль timeit для правильного статистического анализа времени выполнения фрагмента кода. Он запускает фрагмент несколько раз, а затем сообщает, сколько времени занял самый короткий цикл. Вы можете использовать его следующим образом —

Пример

def f(x):
  return x * x
 
import timeit
timeit.repeat("for x in range(100): f(x)", "from __main__ import f", number=100000)

Вывод

Это даст вывод —

[2.0640320777893066, 2.0876040458679199, 2.0520210266113281]



отвечаю на ваши вопросы. Автор книг и разработчик.

Будет полезно знать

  • 👉 Как объединить два словаря Python?
  • 👉 Как поменять местами две переменные в Python
  • 👉 Как вывести текущую дату и время на Python?

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

Меню

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

import time

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

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

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

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

Понравилась статья? Поделить с друзьями:
  • Как понять что пришло время сменить работу
  • Как посчитать капитализацию своей компании
  • Как понять что ты лишний в компании друзей
  • Как посчитать кредитный лимит для компании
  • Как попасть в бизнес зал без приорити пасс