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

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.6k12 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,7372 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,5661 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.8k3 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,3945 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,2942 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

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 usec per loop

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)

25 ответов

Самый простой способ в Python:

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

Это предполагает, что для выполнения вашей программы требуется не менее десятой секунды.

Печать

--- 0.764891862869 seconds ---

rogeriopvl
13 окт. 2009, в 00:31

Поделиться

Я помещаю этот модуль timing.py в свой собственный каталог site-packages и просто вставляю import timing в начало моего модуля:

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

Я также могу вызвать timing.log из моей программы, если есть значительные этапы внутри программы, которую я хочу показать. Но только в том числе import timing будет печатать время начала и окончания, а общая истекшая time. (пропустите мою непонятную secondsToStr функцию, она просто форматирует число с плавающей точкой в ​​секундах до hh: mm: ss.sss form.)

Примечание. Версия Python 3 приведенного выше кода может быть найдена здесь или здесь.

PaulMcG
13 окт. 2009, в 03:59

Поделиться

import time

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

time.clock() возвращает процессор time,, который позволяет рассчитать только time , используемые этим процессом (в любом случае, Unix). В документации говорится: «В любом случае, это функция, используемая для бенчмаркинга Python или алгоритмов синхронизации»

newacct
13 окт. 2009, в 01:44

Поделиться

Мне очень нравится ответ Пола МакГира, но я использую Python3. Итак, для тех, кому это интересно: вот модификация его ответа, которая работает с Python 3 на * nix (я полагаю, под Windows, что вместо time() следует использовать clock()):

#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")

Если вы сочтете это полезным, вы должны продолжить голосование за свой ответ вместо этого, так как он выполнил большую часть работы;).

Nicojo
10 сен. 2012, в 03:47

Поделиться

Вы можете использовать Python Profiler cProfile для измерения времени процессора и, кроме того, сколько времени затрачивается внутри каждой функции и сколько раз вызывается каждая функция. Это очень полезно, если вы хотите улучшить производительность вашего скрипта, не зная, с чего начать. Этот ответ на другой вопрос довольно хорош. Всегда хорошо заглядывать в документы.

Вот пример того, как профилировать скрипт, используя cProfile из командной строки:

$ 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}

jacwah
02 янв. 2014, в 00:56

Поделиться

Мне нравится вывод, предоставляемый модулем datetime, где time дельта-объекты показывают дни, часы, минуты и т.д., если необходимо, с точки зрения человека.

Например:

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

Пример вывода, например

Duration: 0:00:08.309267

или

Duration: 1 day, 1:51:24.269711

Обновление: Как отметил Дж. Ф. Себастьян, этот подход может столкнуться с некоторыми сложными случаями с локальным time,, поэтому безопаснее использовать:

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

metakermit
29 сен. 2014, в 13:05

Поделиться

Еще лучше для Linux: /usr/bin/time

$ /usr/bin/time -v python rhtest2.py

    Command being timed: "python rhtest2.py"
    User time (seconds): 4.13
    System time (seconds): 0.07
    Percent of CPU this job got: 91%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.58
    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): 0
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 15
    Minor (reclaiming a frame) page faults: 5095
    Voluntary context switches: 27
    Involuntary context switches: 279
    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

Обычно просто time является более простой оболочкой, которая затеняет более способную /usr/bin/time.

u0b34a0f6ae
13 окт. 2009, в 07:28

Поделиться

Решение rogeriopvl отлично работает, но если вы хотите получить более конкретную информацию, вы можете использовать встроенный профилировщик python. Проверьте эту страницу:

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

профилировщик сообщает вам много полезной информации, такой как time , затраченная на каждую функцию

wezzy
13 окт. 2009, в 01:17

Поделиться

Следующий фрагмент распечатывает прошедшее время в удобном для человека формате <HH:MM:SS>.

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)    

Sandeep
02 июль 2016, в 00:08

Поделиться

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)

Qina Yan
06 апр. 2016, в 09:04

Поделиться

<сильный > time.clock()

Устаревший с версии 3.3: поведение этой функции зависит на платформе: вместо этого используйте perf_counter() или process_time()в зависимости от ваших требований, чтобы иметь четко определенное поведение.

<сильный > time. perf_counter()

Возвращает значение (в дробных секундах) счетчика производительности, то есть часы с наивысшим доступным разрешением для измерения короткого продолжительность. включает time , прошедший во время сна, и в масштабе всей системы.

<сильный > time. process_time()

Возвращает значение (в дробных секундах) суммы системы и пользовательский CPU time текущего процесса. Он не включает time истекший во время сна.

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

Yas
18 май 2016, в 04:26

Поделиться

Ipython «timeit» любой script:

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

ab-user
20 май 2015, в 15:42

Поделиться

Я просмотрел модуль timeit , но, похоже, это только для небольших фрагментов кода. Я хочу time всю программу.

$ python -mtimeit -n1 -r1 -t -s "from your_module import main" "main()"

Он запускает your_module.main() функцию one time и печатает прошедший time с помощью функции time.time() в качестве таймера.

Чтобы эмулировать /usr/bin/time в Python, см. подпроцесс Python с /usr/bin/time:, как записывать информацию о времени, но игнорировать все остальные выходные?.

Чтобы измерить CPU time (например, не включать time в течение time.sleep()) для каждой функции, вы можете использовать модуль profile (cProfile на Python 2):

$ python3 -mprofile your_module.py

Вы можете передать команду -p в timeit выше, если вы хотите использовать тот же таймер, что и модуль profile.

См. Как вы можете profile Python script?

jfs
03 март 2015, в 09:31

Поделиться

Просто используйте timeit модуль. Он работает как с Python 2, так и с Python 3

import timeit

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

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

Он возвращается в секундах, и вы можете выполнить свое выполнение Time. Simple, но вы должны написать их в главной функции, которая запускает выполнение программы. Если вы хотите получить Execution time , даже когда вы получите ошибку, тогда возьмите свой параметр «Начать» и подсчитайте там, как

def sample_function(start,**kwargs):
     try:
         #your statements
     Except:
         #Except Statements
         stop = timeit.default_timer()
         execution_time = stop - start
         print("Program Executed in "+execution_time)

Ravi Kumar
18 сен. 2017, в 20:17

Поделиться

Мне также нравится ответ Пола МакГира и придумал форму менеджера контекста, которая соответствовала моим потребностям.

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

Gall
29 янв. 2015, в 15:51

Поделиться

Существует модуль timeit который можно использовать для определения времени выполнения кодов Python. Он содержит подробную документацию и примеры в документации по python (https://docs.python.org/2/library/timeit.html).

Alfie
20 окт. 2014, в 15:44

Поделиться

Используйте line_profiler.

line_profiler будет профилировать время выполнения отдельных строк кода. Профилировщик реализован в C через Cython, чтобы уменьшить накладные расходы на профилирование.

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

Результаты будут:

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

Yu Jiaao
28 март 2018, в 07:15

Поделиться

Для данных людей, использующих ноутбуки Jupyter

В ячейке вы можете использовать магическую команду Jupyter %%time для измерения времени выполнения:

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

Выход
Время процессора: пользовательский 4,54 мс, sys: 0 нс, всего: 4,54 мс
Время стены: 4,12 мс

Это будет захватывать только время выполнения конкретной ячейки. Если вы хотите зафиксировать время выполнения всей записной книжки (т.е. программы), вы можете создать новую записную книжку в том же каталоге и в новой записной книжке выполнить все ячейки:

Предположим, что записная книжка выше называется example_notebook.ipynb. В новой записной книжке в том же каталоге:

# 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

Выход
Время загрузки процессора IPython (по оценкам): Пользователь: 0,00 с.
Система: 0,00 с.
Время стены: 0,00 с.

Matt
28 июль 2018, в 17:00

Поделиться

Timeit — это класс в python, используемый для вычисления выполнения time небольших блоков кода.

Default_timer — это метод в этом классе, который используется для измерения времени настенных часов, а не выполнения ЦП time.. Таким образом, другое выполнение процесса может помешать этому. Таким образом, это полезно для небольших блоков кода.

Образец кода выглядит следующим образом:

from timeit import default_timer as timer

start= timer()

#some logic 

end = timer() 

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

Utkarsh Dhawan
16 нояб. 2017, в 03:09

Поделиться

Это ответ Paul McGuire, который работает для меня. На всякий случай у кого-то возникли проблемы с запуском этого.

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

вызывать timing.main() из вашей программы после импорта файла.

Saurabh Rana
08 апр. 2015, в 00:47

Поделиться

Чтобы использовать обновленный ответ metakermit для python 2.7, вам потребуется monotonic.

Тогда код будет выглядеть следующим образом:

from datetime import timedelta
from monotonic import monotonic

start_time = monotonic()
end_time = monotonic()
print(timedelta(seconds=end_time - start_time))

H0R5E
16 июнь 2017, в 13:59

Поделиться

Если вы хотите измерять время в микросекундах, то вы можете использовать следующую версию, полностью основанную на ответах Пола Макгуайра и Никохо, — это код Python3. Я также добавил немного цвета к нему:

import atexit
from time import time
from datetime import timedelta, datetime


def seconds_to_str(elapsed=None):
    if elapsed is None:
        return datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
    else:
        return str(timedelta(seconds=elapsed))


def log(txt, elapsed=None):
    colour_cyan = '33[36m'
    colour_reset = '33[0;0;39m'
    colour_red = '33[31m'
    print('n ' + colour_cyan + '  [TIMING]> [' + seconds_to_str() + '] ----> ' + txt + 'n' + colour_reset)
    if elapsed:
        print("n " + colour_red + " [TIMING]> Elapsed time ==> " + elapsed + "n" + colour_reset)


def end_log():
    end = time()
    elapsed = end-start
    log("End Program", seconds_to_str(elapsed))


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

log() => функция, которая печатает информацию о времени.

txt ==> первый аргумент в логе и строка для отметки времени.

atexit ==> модуль python для регистрации функций, которые вы можете вызывать при выходе из программы.

Rui Carvalho
20 дек. 2018, в 02:13

Поделиться

Я использовал очень простую функцию для определения времени выполнения кода:

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

И чтобы использовать его, просто вызовите его перед кодом для измерения, чтобы получить функцию синхронизации, затем вызовите функцию после кода с комментариями, и перед комментариями появится время, например:

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

Тогда вывод будет выглядеть так:

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

Я чувствую себя немного элегантно в этом смысле.

Tao Wang
07 авг. 2018, в 06:23

Поделиться

time меры выполнения программы Python может быть непоследовательной в зависимости от:

  • Та же программа может быть оценена с использованием разных алгоритмов
  • Запуск time изменяется между алгоритмами
  • Запуск time зависит от реализации
  • Запуск time варьируется между компьютерами
  • Запуск time не предсказуем на основе небольших входов

Это связано с тем, что наиболее эффективным способом является использование «Ордена роста» и изучение записи «Большой буквы», чтобы сделать это правильно, https://en.wikipedia.org/wiki/Big_O_notation

В любом случае вы можете попытаться оценить производительность любой программы Python на определенных этапах машинного счета в секунду, используя этот простой алгоритм:
адаптируйте это к программе, которую хотите оценить

import time

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

Надеюсь, что это поможет вам.

Manu
31 июль 2017, в 16:51

Поделиться

Ещё вопросы

  • 0jQuery: плагин проверки формы с помощью мастера выбора и шага
  • 0Странный разрыв появляется на веб-странице при просмотре с Firefox
  • 1параметры предварительного просмотра камеры Nexus один?
  • 1Этапы миграции с OC4J на Weblogic
  • 0Как создать текстовое поле после выбора опции из выпадающего списка?
  • 0Как определить, есть ли точки на фигуре и в форме?
  • 0Ajax-вызов Jquery всегда возвращает ошибку в веб-службе ASP.NET
  • 0str replace / preg заменяет еврейские слова на еврейские слова с использованием массива
  • 1Получить два разных экземпляра класса и сравнить их (Java)
  • 1Пользовательский кодировщик scikit выдает ошибку преобразования
  • 0Автоматическое цитирование элементов массива в Postgres
  • 1чтение массива Java-файла
  • 0MySQL игнорирует (почти) дубликаты на основе значения поля
  • 0вставка элемента в базу данных с использованием внешнего ключа
  • 1java.lang.OutOfMemoryError Невозможно создать новый собственный поток
  • 1countr = = 0 показывает нет pi2go
  • 1UTF-8 селеновая ошибка
  • 1Новичок не может программировать приложение Hello world
  • 0Данные POST в URL
  • 0вернуть array_chunk в группы
  • 0MySQL (Windows10) FULLTEXT поиск с таблицами MyISAM не работает
  • 0перевести функцию C ++ в Android
  • 0Angular $ location как перенаправить на абсолютный URL (не просто хеш)
  • 1как вернуть массив и его длину в Java
  • 1Regex Pattern Matching для похожих URL
  • 0Заголовок столбца данных Jquery, перекрывающийся с заголовком таблицы
  • 0альтернативный тег jquery details
  • 0Есть ли какое-то значение в PHP, уникальное для каждой системы
  • 1Если для цикла возвращает нулевой JavaScript
  • 0Хранение таблиц и имен столбцов в html
  • 0Как отправить 1000 push-уведомлений одновременно на устройства ios без использования цикла в php
  • 0Структура базы данных для системы тикетов
  • 0Вставка файла изображения в базу данных с последующим отображением с помощью php
  • 0Как преобразовать объект Google_Service_Calendar_EventDateTime в строку?
  • 1Объявление переменной в цикле не сбрасывается по умолчанию
  • 1Петлевая печать слишком много раз
  • 0Mysql — Авто создание, обновление, удаление таблицы 2 из таблицы 1
  • 1Иерархия модулей JavaScript?
  • 1Android: ошибка при использовании onCreateOptionsMenu, недоступная в суперклассе?
  • 1Сильно типизированный элемент атрибута C # для описания свойства
  • 0img onclick не работают, когда в кавычках
  • 1Как я могу параметризовать распознавание речи Android? android.speech.action.RECOGNIZE_SPEECH ничего не делает
  • 1Создать серию дат с помощью Pandas
  • 0Как расширить JQuery UI диалоговое окно с параметром?
  • 1Прослушиватель сокета на клиенте (Android)
  • 0Как вы показываете скрытые div, когда мышь находится над нижней частью экрана?
  • 0Как избежать косой черты при чтении Excel
  • 0JQuery включить раскрывающийся список на основе первого варианта
  • 1Невозможно создать мьютекс ReportingServices
  • 0Masonry: eventie.bind не нажимает на изображение

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