<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Zero Mu Tech Articles &#187; MySQLdb</title>
	<atom:link href="http://www.techarticles.zeromu.net/tag/mysqldb/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techarticles.zeromu.net</link>
	<description>Solutions to computer problems that were in my way.</description>
	<lastBuildDate>Sun, 09 May 2010 03:48:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Keeping Track of Elapsed Time in Python</title>
		<link>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/</link>
		<comments>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/#comments</comments>
		<pubDate>Wed, 21 May 2008 03:20:49 +0000</pubDate>
		<dc:creator>Jake D</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[MySQLdb]]></category>

		<guid isPermaLink="false">http://www.techarticles.zeromu.net/?p=32</guid>
		<description><![CDATA[This article shows how to put a few lines of python code into your python script so you can tell how long the script has been running, or how long a certain part of the task took to run. The Quick Answer: For the most accurrate time elapsed, use the time module and make 2<br/><br/><a href="http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/">Read more...</a>]]></description>
			<content:encoded><![CDATA[<p>This article shows how to put a few lines of python code into your python script so you can tell how long the script has been running, or how long a certain part of the task took to run.</p>
<p><strong>The Quick Answer</strong>: For the most accurrate time elapsed, use the <code>time</code> module and make 2 <code>time.time()</code> objects.  The difference between these objects is the time elapsed. Do not use <code>time.clock()</code>.</p>
<h1>The Whole Story</h1>
<p>Its pretty basic: I have certain parts of a large python script (that happen to access a MySQL database) that I would like to keep track of how long it took them to execute.</p>
<h2>Wrong Answers</h2>
<p>Initially, I read on a <a href="http://pleac.sourceforge.net/pleac_python/datesandtimes.html" target="_blank">PLEAC-Python article, Dates and Times</a> (which really is a <strong>great</strong> overview of Python&#8217;s <code>time</code> module), about some ways to use Python&#8217;s <code>time</code> module.  That article suggests that all you need to do is:</p>
<blockquote><pre class="brush: python;">
#-----------------------------
# High Resolution Timers

t1 = time.clock()
# Do Stuff Here
t2 = time.clock()
print t2 - t1

# 2.27236813618
# Accuracy will depend on platform and OS,
# but time.clock() uses the most accurate timer it can
</pre>
</blockquote>
<p>For one of my projects, that worked fine.  But then I had a bigger script that used a lot of MySQL via the Python module <a href="http://mysql-python.sourceforge.net/MySQLdb.html" target="_blank">MySQLdb</a>, and I would look at my script&#8217;s run time after leaving, and the times looked short&#8230; but not too short. Eventually after running a script that took a 7 hour sleep and then some to run &#8212; but the script reported only taking an hour or so &#8212; I knew something was wrong.</p>
<h2>Only timing the work of Python?</h2>
<p>It seemed as if using the <code>time.clock()</code> approach was only timing what Python (as opposed to MySQL?) was doing.  I created this test script</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

for x in range(0, 1000000):
    z = x + 6

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min
</pre>
<p>And I didn&#8217;t find a problem, but my script still did.  I was wanting to time an event of a known length, and so I found out about the <code>time.sleep()</code> function from the aforementioned article. I incorporated the <code>sleep()</code> function:</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

time.sleep(3)

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min
</pre>
<p>and ran the script.  You would expect to see 3 seconds as a result, or at least something close, but my output was:</p>
<pre>0.03
0.03
0.0 0.0</pre>
<p>That was funny (although I didn&#8217;t laugh) because <code>yearend</code> was not supposed to sample the <code>clock()</code> until after the sleep.  I still don&#8217;t know why this does this, but it does.  I am sure this has its uses, but this was not the use I was wanting.</p>
<h2>Use <code>time.time()</code></h2>
<p>Still referencing the PLEAC-Python article, I tried using <code>time.time()</code>, which is supposed to just be the seconds since that day in 1970 and compared it to the <code>time.clock()</code> approach:</p>
<pre class="brush: python;">
import time

yearstart = time.clock()
print yearstart

time.sleep(3)

yearend = time.clock()
print yearend
elapsed= yearend-yearstart
min = elapsed/60

print elapsed, min

yearstart = time.time()
print yearstart

time.sleep(3)

yearend = time.time()
print yearend

elapsed= yearend-yearstart

min = elapsed/60

print elapsed, min
</pre>
<p>And got the output:</p>
<pre>0.03
0.03
0.0 0.0
1211338788.69
1211338791.69
3.00004386902 0.0500007311503</pre>
<h1>No Fluff Answer:</h1>
<p>Read nothing else (on this page). This code will get you going:</p>
<pre class="brush: python;">
import time

start = time.time()

# whatever you want to time, put between these two statements

end = time.time()

elapsed= end - start

#if you want to convert to minutes, just divide
min = elapsed/60

print &quot;Your stuff took&quot;, elapsed, &quot;seconds to run, which is the same as&quot;, min, &quot;minutes&quot;
</pre>
<p>So, this works for me.  I supose that <code>time.clock()</code> does not reference absolute time, which was a problem for me. Hope this helps, or at least saves you some head-scratching.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techarticles.zeromu.net/programming/keeping-track-of-elapsed-time-in-python/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
