Python Elapsed Time Functions

Main.PythonTiming History

Hide minor edits - Show changes to output

June 21, 2020, at 04:17 AM by 136.36.211.159 -
Deleted lines 87-105:

----

(:html:)
 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)
January 13, 2020, at 10:57 AM by 147.46.252.163 -
Added line 20:
<iframe width="560" height="315" src="https://www.youtube.com/embed/nP9KBxW7o0o" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
January 13, 2020, at 10:02 AM by 147.46.252.163 -
Changed line 64 from:
x = np.arange(1,10002,500)
to:
x = np.arange(1,1000002,50000)
January 13, 2020, at 10:01 AM by 147.46.252.163 -
Deleted lines 18-19:
For more complex projects, a profiler such as '''profile''' or '''cProfile''' maybe a better solution to avoid the extra timer code and for a more comprehensive analysis.
Added lines 85-86:

For more complex projects, a profiler such as '''profile''' or '''cProfile''' maybe a better solution to avoid the extra timer code and for a more comprehensive analysis.
January 13, 2020, at 10:00 AM by 147.46.252.163 -
Changed lines 15-19 from:
Sometimes a certain section of code is a majority of the run-time so the code may be ported to a compiled language such as C++ or Fortran that is then called from Python, especially if improving a particular section can greatly reduce the total time. That one section may be converted to a langauge that is built for speed and then called by Python. The following tutorial shows to how to implement a simple timer on sections of code. For more complex projects, a profiler such as '''profile''' or '''cProfile''' maybe a better solution to avoid the extra timer code and for a more comprehensive analysis.
to:
Sometimes a certain section of code is a majority of the run-time so the code may be ported to a compiled language such as C++ or Fortran that is then called from Python, especially if improving a particular section can greatly reduce the total time. That one section may be converted to a langauge that is built for speed and then called by Python. The following tutorial shows to how to implement a simple timer on sections of code.

%width=550px%Attach:python_timers.png

For more complex projects, a profiler such as '''profile''' or '''cProfile''' maybe a better solution to avoid the extra timer code and for a more comprehensive analysis.
January 13, 2020, at 09:53 AM by 147.46.252.163 -
Deleted line 17:
<iframe width="560" height="315" src="//www.youtube.com/embed/tnoAro7whYw?rel=0" frameborder="0" allowfullscreen></iframe>
January 13, 2020, at 09:52 AM by 147.46.252.163 -
Added lines 1-102:
(:title Python Elapsed Time Functions:)
(:keywords time, python, timeit, default_timer, timer, cpu_time:)
(:description Python has a number of timing functions to show you how much time has elapsed between the start and end of a process.:)

Code in Python is not typically built for speed but it is improved with compilers such as Nuitka, PyPy, and Cython. It often helps to know how long certain sections of code require to run so that those sections can be optimized, parallelized, or compiled.

(:source lang=python:)
import time
start = time.time()
# some code to time
end = time.time()
elapsed = end-start
(:sourceend:)

Sometimes a certain section of code is a majority of the run-time so the code may be ported to a compiled language such as C++ or Fortran that is then called from Python, especially if improving a particular section can greatly reduce the total time. That one section may be converted to a langauge that is built for speed and then called by Python. The following tutorial shows to how to implement a simple timer on sections of code. For more complex projects, a profiler such as '''profile''' or '''cProfile''' maybe a better solution to avoid the extra timer code and for a more comprehensive analysis.

(:html:)
<iframe width="560" height="315" src="//www.youtube.com/embed/tnoAro7whYw?rel=0" frameborder="0" allowfullscreen></iframe>
(:htmlend:)

(:toggle hide timing button show="Show Source":)
(:div id=timing:)
(:source lang=python:)
import time
import timeit
import datetime

def concat1(n):
    start = time.time()
    time.sleep(0.02)
    s = "-".join(str(i) for i in range(n))
    end = time.time()
    return end-start

def concat2(n):
    start = timeit.default_timer()
    time.sleep(0.02)
    s = "-".join(str(i) for i in range(n))
    end = timeit.default_timer()
    return end-start

def concat3(n):
    start = datetime.datetime.now()
    time.sleep(0.02)
    s = "-".join(str(i) for i in range(n))
    end = datetime.datetime.now()
    return (end-start).total_seconds()

def concat4(n):
    start = time.process_time()
    time.sleep(0.02)
    s = "-".join(str(i) for i in range(n))
    end = time.process_time()
    return end-start

print(timeit.timeit('"-".join(str(i) for i in range(100000))',number=5))
print(concat1(100000))
print(concat2(100000))
print(concat3(100000)) 
print(concat4(100000))

import numpy as np
x = np.arange(1,10002,500)
y1 = np.empty(len(x))
y2 = np.empty(len(x))
y3 = np.empty(len(x))
y4 = np.empty(len(x))
for j,xj in enumerate(x):
    y1[j] = concat1(xj)
    y2[j] = concat2(xj)
    y3[j] = concat3(xj)
    y4[j] = concat4(xj)
    print(j,xj,y1[j],y2[j],y3[j])

import matplotlib.pyplot as plt
plt.plot(x,y1,label='time.time()')
plt.plot(x,y2,label='timeit.default_timer()')
plt.plot(x,y3,label='datetime.datetime.now()')
plt.plot(x,y4,label='time.process_time()')
plt.legend()
plt.show()
(:sourceend:)
(:divend:)

----

(:html:)
 <div id="disqus_thread"></div>
    <script type="text/javascript">
        /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
        var disqus_shortname = 'apmonitor'; // required: replace example with your forum shortname

        /* * * DON'T EDIT BELOW THIS LINE * * */
        (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = 'https://' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
        })();
    </script>
    <noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
    <a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
(:htmlend:)