Re: [Tutor] time taken to execute certain task
tee chwee liong wrote: hi, 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? Neither, or either. For timing small code snippets, the right tool is the timeit module, rather than trying to re-invent the wheel. For large, time consuming functions, the difference between the two is irrelevant. time.time() and time.clock() have different behaviour and different performance characteristics. They are platform dependent -- they are different from each other, and different depending on your operating system. On Windows: time.clock() measures wall clock time and is accurate to better than one microsecond; time.time() is only accurate to 1/60th of a second. On POSIX systems such as Unix, Linux, Apple Macintosh: time.clock() measures CPU time, but is only accurate to 1/100th of a second; time.time() is much more accurate but measures wall clock time. The timeit module automatically chooses the best timer for your operating system, although you can choose another if you prefer. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] time taken to execute certain task
On 3/16/11, tee chwee liong tc...@hotmail.com wrote: hi, 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? import time def testtime(num): start=time.time() #print start for n in range(num): #print n print time.time()-start testtime(101) start = time.clock() for x in range(101): y = x # do something end = time.clock() print Time clock elapsed = , end - start, seconds thanks tcl Also look into the builtin module 'timeit' if you're trying to benchmark things. -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] time taken to execute certain task
tee chwee liong tc...@hotmail.com wrote 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? Neither of those, either use timeit() or the Python profiler. They are both specifically provided for timing things in your code. Search the documentation for help with both. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] time taken to execute certain task
hi, i used profiler. but it gives 0 sec? is this expected? tq profile.run('import math') 3 function calls in 0.000 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0000.0000.0000.000 :0(setprofile) 10.0000.0000.0000.000 string:1(module) 10.0000.0000.0000.000 profile:0(import math) 00.000 0.000 profile:0(profiler) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] time taken to execute certain task
Profiling is really intended to profile a complex set of operations not a single statement, for that timeit() would be a better choice. However in this case, it may be that the time is so low it simply doesn't register. Have you already imported math for example? In which case you are effectively profiling a single if statement... Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ From: tee chwee liong tc...@hotmail.com To: alan.ga...@btinternet.com; tutor@python.org Sent: Thursday, 17 March, 2011 9:06:56 Subject: RE: [Tutor] time taken to execute certain task hi, i used profiler. but it gives 0 sec? is this expected? tq profile.run('import math') 3 function calls in 0.000 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0000.0000.0000.000 :0(setprofile) 10.0000.0000.0000.000 string:1(module) 10.0000.0000.0000.000 profile:0(import math) 00.000 0.000 profile:0(profiler)___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] time taken to execute certain task
On Wed, Mar 16, 2011 at 9:40 PM, tee chwee liong tc...@hotmail.com wrote: hi, 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? import time def testtime(num): start=time.time() #print start for n in range(num): #print n print time.time()-start testtime(101) start = time.clock() for x in range(101): y = x # do something end = time.clock() print Time clock elapsed = , end - start, seconds thanks tcl First you have to start by testing things that are similar. I've rewritten your tests to look exactly the same except for the time.time() or time.clock(). 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. Here's how I would compare them... (I hope the indentation doesn't get hosed. If it does I can repost.) import time def testtime(num): start = time.time() for n in range(num): 1 + 1 # do something print time.time() - start testtime(101) #=== 3.50475311279e-05 def testclock(num): start = time.clock() for n in range(num): 1 + 1 # do something print time.clock() - start testclock(101) #=== 0.0 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. The help() function will show you what each function (time or clock) is supposed to return. It looks like this... help(time.time) time(...) time() - floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. help(time.clock) clock(...) clock() - floating point number Return the CPU time or real time since the start of the process or since the first call to clock(). This has as much precision as the system records. 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(). The epoch is an arbitrary point in the past (don'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. As to which one's best? I don'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'll let someone else give you advice on this. For a quick-n-dirty test, either will work just fine. I usually use time.time(), but I only really ever do this just for kicks. -- Jack Trades Pointless Programming Blog http://pointlessprogramming.wordpress.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor