<br><br><div class="gmail_quote">On Wed, Mar 16, 2011 at 9:40 PM, tee chwee liong <span dir="ltr">&lt;<a href="mailto:tcl76@hotmail.com">tcl76@hotmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">




<div>
hi, <br>
 <br>
i would like to know the time taken to execute a certain task in python. i used time.time and time.clock and i see the time taken is different? what is the right method to use? <br>
 <br>
import time<br>
def testtime(num):<br>    start=time.time()<br>    #print start<br>    for n in range(num):<br>        #print n<br>        print time.time()-start<br>      <br>testtime(101) <br>
<br>start = time.clock()<br>for x in range(101):<br>  y = x  # do something<br>end = time.clock()<br>print &quot;Time clock elapsed = &quot;, end - start, &quot;seconds&quot;<br>
 <br>
thanks<br>
tcl<br>
  <br></div></blockquote></div><br>First you have to start by testing things that are similar.  I&#39;ve rewritten your tests to look exactly the same except for the time.time() or time.clock().<br><br>In your first example, you are calculating an end time for each iteration of the for loop.  In your second example you are only calculating the end time once, after the for loop has finished.<br>
<br>Here&#39;s how I would compare them...<br><br>(I hope the indentation doesn&#39;t get hosed.  If it does I can repost.)<br><br>import time<br><br>def testtime(num):<br>  start = time.time()<br>  for n in range(num):<br>
    1 + 1 # do something<br>  print time.time() - start<br>     <br>testtime(101)<br>#===&gt; 3.50475311279e-05<br><br>def testclock(num):<br>  start = time.clock()<br>  for n in range(num):<br>    1 + 1  # do something<br>
  print time.clock() - start<br><br>testclock(101)<br>#===&gt; 0.0<br><br>Now that each test is exactly the same, you are in a better position to judge the differences.  And there are some obvious differences!  Why?  To determine that you need to find out just what each function is supposed to return.<br>
<br>The help() function will show you what each function (time or clock) is supposed to return.  It looks like this...<br><br><br>help(time.time)<br>time(...)<br>    time() -&gt; floating point number<br>    <br>    Return the current time in seconds since the Epoch.<br>
    Fractions of a second may be present if the system clock provides them.<br><br>help(time.clock)<br>clock(...)<br>    clock() -&gt; floating point number<br>    <br>    Return the CPU time or real time since the start of the process or since<br>
    the first call to clock().  This has as much precision as the system<br>    records.<br><br><br>As you can see, time.time() returns the current time in seconds from the Epoch, while time.clock() returns the time since the program started, or the time since the first call to time.clock().<br>
<br>The &quot;epoch&quot; is an arbitrary point in the past (don&#39;t rely on it always being the same).  So both time.time() and time.clock() return the number of seconds, but each of them starts at a different time.<br>
<br>As to which one&#39;s best?  I don&#39;t know.  I know there are some profiling tools that will give you a better idea about the performance of a function, but I rarely use them so I&#39;ll let someone else give you advice on this.<br>
<br>For a quick-n-dirty test, either will work just fine.  I usually use time.time(), but I only really ever do this &quot;just for kicks&quot;.<br><br>-- <br>Jack Trades<br><a href="http://pointlessprogramming.wordpress.com" target="_blank">Pointless Programming Blog</a><br>
<br>