473,594 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

time.clock() or time.time()

What's the difference between time.clock() and time.time()
(and please don't say clock() is the CPU clock and time() is the actual
time because that doesn't help me at all :)

I'm trying to benchmark some function calls for Zope project and when I
use t0=time.clock() ; foo(); print time.clock()-t0
I get much smaller values than when I use time.clock() (most of them
0.0 but some 0.01)
When I use time.time() I get values like 0.0133562088013 ,
0.0066900253295 9 etc.
To me it looks like time.time() gives a better measure (at least from a
statistical practical point of view).

Aug 2 '05 #1
8 3402
pe*****@gmail.c om wrote:
What's the difference between time.clock() and time.time()
(and please don't say clock() is the CPU clock and time() is the actual
time because that doesn't help me at all :)

I'm trying to benchmark some function calls for Zope project and when I
use t0=time.clock() ; foo(); print time.clock()-t0
I get much smaller values than when I use time.clock() (most of them
0.0 but some 0.01)
When I use time.time() I get values like 0.0133562088013 ,
0.0066900253295 9 etc.
To me it looks like time.time() gives a better measure (at least from a
statistical practical point of view).


time.time() is very likely what you want.

time.time() measures real time, while time.clock() measures the time the
CPU dedicates to your program. For example, if your function spends 2
seconds doing pure I/O and 3 seconds doing pure computation, you should
expect time.time() to show 5 that seconds elapsed, and time.clock() to
show that the CPU was consumed by your program for 3 seconds.

Shane
Aug 2 '05 #2
pe*****@gmail.c om schrieb:
What's the difference between time.clock() and time.time()
(and please don't say clock() is the CPU clock and time() is the actual
time because that doesn't help me at all :)
clock (depending on the platform ?) measures the time spent by your program.

Time gives you the system time, so the difference is the time between
your calls, your program could have been idle (waiting for input or
sharing processor due to multitasking)
I'm trying to benchmark some function calls for Zope project and when I
use t0=time.clock() ; foo(); print time.clock()-t0
I get much smaller values than when I use time.clock() (most of them
0.0 but some 0.01)


Run your code 100 times and divide the result by 100.

Leonhard
Aug 2 '05 #3
Shane Hathaway wrote:
time.time() measures real time, while time.clock() measures the time the
CPU dedicates to your program.


I suppose that varies with the platform... "help(time.cloc k)" says:

Help on built-in function 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.

Another thing to notice is that depending on OS, either time.time() or
time.clock() might have much higher precision than the other.

For Linux on Intel at least, you'll probably want to always use
time.time(). On Windows, you're likely to prefer time.clock(), to
measure relative times, since time.time() will have too low resolution
for measuring short thingies. Be aware that time.clock() will restart
at 0 now and then though. I.e. t1=time.clock() ;f();t2=time.cl ock() will
have t1>t2 now and then. Using time.time(), that won't happen until
after I retire... :)

For Linux, see "man 2 time" and "man 3 clock".

Aug 2 '05 #4
Magnus Lycka wrote:
On Windows, you're likely to prefer time.clock(), to
measure relative times, since time.time() will have too low resolution
for measuring short thingies.


Specifically, using the NT/XP family of Windows operating systems
time.time() has a resolution of approximately 0.015 seconds (I believe
it might be exactly 1/64 second, but it should be easy to confirm with a
google search).
Aug 2 '05 #5
Magnus Lycka wrote:
Shane Hathaway wrote:
time.time() measures real time, while time.clock() measures the time the
CPU dedicates to your program.

I suppose that varies with the platform... "help(time.cloc k)" says:

Help on built-in function 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.

Another thing to notice is that depending on OS, either time.time() or
time.clock() might have much higher precision than the other.


I didn't notice that. Thanks.

However, isn't this thoroughly un-Pythonic? No wonder people have to
ask. Wouldn't it be better to have:

time.time() -> real time, with as much precision as the platform
provides. Does not wrap around.

time.cputime() -> CPU time, or real time on platforms that don't
measure CPU time separately from real time. May wrap around in
long-running processes.

Shane
Aug 3 '05 #6
"pe*****@gmail. com" <pe*****@gmail. com> writes:
I'm trying to benchmark some function calls for Zope project


Other folks have explained time() vs. clock(), so I'll leave that.

But rather than roll your own timer functions, consider using timeit.

Aug 3 '05 #7
On Wed, 03 Aug 2005 04:11:36 -0600, Shane Hathaway
<sh***@hathaway mix.org> declaimed the following in comp.lang.pytho n:

However, isn't this thoroughly un-Pythonic? No wonder people have to
ask. Wouldn't it be better to have:
The problem is that these are just bindings to the C (for the
CPython) runtime library -- and that is outside of Python control.

-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Aug 4 '05 #8
Shane Hathaway wrote:
However, isn't this thoroughly un-Pythonic? No wonder people have to
ask.


Only if they've never read K&R, and I thought that was compulsory! ;^)

There are a few python libraries, such as time and math, that are
basically just thin wrappers around standard C libraries. In some
cases, they are complemented with more modern libraries, such as
datetime.

You might argue on how pythonic (or useful, or beginner friendly)
they are, but they cost very little to maintain across platforms.
It's important to keep the maintenance costs down for any project,
certainly for a project such as Python, and as far as I know, there
is no cross platform standard API to access CPU time, so a
time.cputime() function would require a lot of work to maintain,
and I don't know if anyone really had a strong need for it.

There is a benefit to maintain (standard or not) API's that
programmers have experience from when programming in other languages.
Of course, I think it's a good thing to properly adapt API's to Python
ways of working. I.e. if a C++ library returning an iterator is wrapped,
I want to be able to do...

for item in container:
print item

....rather than...

it = container.start ()
while it != container.end() :
item = it.deref()
print item

Still, it's often a Good Thing if a Python API is as similar as
possible to e.g. a C++ API that it wraps.

One of the great aspects of Python is that it's a good team player.
It doesn't try to create a world of its own, like Smalltalk and
ABC does, but blends in well in a mixed language environment.

We work extensively with a mix of C++ and Python (as well as some
other languages) here at Carmen, and it would be painful if I had
to remember two different API's for each library that I access from
both C++ and Python.
Aug 5 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3741
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to speed things up implementing some of the functions in C. So I need profiling. I first tried to use the default python profiler, but profiling my application multiplies the execution time by a factor between 10 and 100 ! So I decided to give a...
9
7260
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null; Let's declare a constant Int32 m_TimePeriod = 10000;
17
6404
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
5
4112
by: raybakk | last post by:
Hi there. If I make a function in c (I acually use gnu right now), is there any way to find out how many clocksycluses that function takes? If I divide some numbers etc Var1 = Var2/Var3, is it a fix amount of clocksycluses that is been used for that division, or does it varies? Raymond
7
7668
by: nono909 | last post by:
I wrote the following time class for the following assignment.i need help in completing this program pleasee. Write a class to hold time. Time is the hour, minute and seconds. Write a constructor that will allow the user of your class to initialize a time or set the time to all zeros if not initialized. Include all the operators listed above. Your program will need to read from a file of times and commands. There will be a number at the top...
5
5638
by: yinglcs | last post by:
Hi, I am following this python example trying to time how long does an operation takes, like this: My question is why the content of the file (dataFile) is just '0.0'? I have tried "print >>dataFile, timeTaken" or "print >>dataFile,str( timeTaken)", but gives me 0.0. Please tell me what am I missing?
37
4664
by: David T. Ashley | last post by:
I have Red Hat Enterprise Linux 4. I was just reading up about UTC and leap seconds. Is it true on my system that the Unix time may skip up or down by one second at midnight when there is a leap second? By "Unix time" I mean the integer returned by time() and similar functions. I'm concerned about the "down" case. Some of the software I've written
9
8287
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the following to try to determine which. import time # Determine if time.time is better than time.clock # The one with better resolution should be lower.
8
1844
by: Theo v. Werkhoven | last post by:
hi, In this code I read out an instrument during a user determined period, and save the relative time of the sample (since the start of the test) and the readback value in a csv file. #v+ from datetime import * from time import * from visa import *
0
7936
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
7874
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8241
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5402
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3853
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1469
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.