473,587 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding cpu time spent on my program

I am trying to measure the time the processor spends on some
operation, and I want this measure to not depend on the current load
of the machine. But doing the following prints different values each
time a run.

import time
c1 = time.clock()
for x in xrange(0xFFFFF) : pass

c2 = time.clock()
print "Time spent is %f" % (c2-c1)

Is there a way to measure the number of cpu cycles spent on my program
alone irrespective of the current load etc of the machine.

-
Suresh

Feb 5 '07 #1
3 10549
On Feb 5, 2:37 am, "jm.sur...@no.s pam.gmail.com" <jm.sur...@gmai l.com>
wrote:
I am trying to measure the time the processor spends on some
operation, and I want this measure to not depend on the current load
of the machine. But doing the following prints different values each
time a run.

import time
c1 = time.clock()
for x in xrange(0xFFFFF) : pass

c2 = time.clock()
print "Time spent is %f" % (c2-c1)

Is there a way to measure the number of cpu cycles spent on my program
alone irrespective of the current load etc of the machine.

-
Suresh
One of the best ways to time small snippets of code is python's timeit
module. See the docs at http://docs.python.org/lib/module-timeit.html

- Mike

Feb 5 '07 #2
ky******@gmail. com wrote:
On Feb 5, 2:37 am, "jm.sur...@no.s pam.gmail.com" <jm.sur...@gmai l.com>
wrote:
>I am trying to measure the time the processor spends on some
operation, and I want this measure to not depend on the current load
of the machine. But doing the following prints different values each
time a run.

import time
c1 = time.clock()
for x in xrange(0xFFFFF) : pass

c2 = time.clock()
print "Time spent is %f" % (c2-c1)

Is there a way to measure the number of cpu cycles spent on my program
alone irrespective of the current load etc of the machine.
There's an performance testing suite that you might also find useful.
Check out TAU: Tuning and Analysis Utilities toolkit (simple google
search will take you to the page). You can get some serious numbers
using that thing and they have python support as well as some tools for
automatically profiling an entire application. Check it out.

-carl

--

Carl J. Van Arsdall
cv*********@mvi sta.com
Build and Release
MontaVista Software

Feb 5 '07 #3
ky******@gmail. com <ky******@gmail .comwrote:
On Feb 5, 2:37 am, "jm.sur...@no.s pam.gmail.com" <jm.sur...@gmai l.com>
wrote:
I am trying to measure the time the processor spends on some
operation, and I want this measure to not depend on the current load
of the machine.

One of the best ways to time small snippets of code is python's timeit
module. See the docs at
http://docs.python.org/lib/module-timeit.html
Timeit is very cool, but it doesn't measure CPU time, it measure real
time, eg on a linux box

$ python -m timeit 'for i in xrange(100000): pass'
100 loops, best of 3: 11.4 msec per loop

Now run 10 copies of the same program at once

$ for i in `seq 10` ; do python -m timeit 'for i in xrange(100000): pass' & done
10 loops, best of 3: 24.4 msec per loop
10 loops, best of 3: 83.2 msec per loop
10 loops, best of 3: 83.4 msec per loop
10 loops, best of 3: 81.4 msec per loop
10 loops, best of 3: 83 msec per loop
10 loops, best of 3: 60.7 msec per loop
10 loops, best of 3: 47 msec per loop
10 loops, best of 3: 48.6 msec per loop
10 loops, best of 3: 42.3 msec per loop
10 loops, best of 3: 38.7 msec per loop
Is there a way to measure the number of cpu cycles spent on my program
alone irrespective of the current load etc of the machine.
The most accurate way of measuring CPU usage under linux is getrusage,
eg
>>import resource
def cpu_time():
.... return resource.getrus age(resource.RU SAGE_SELF)[0]
....

Now try this out
>>def f():
.... for i in xrange(100000):
.... pass
....
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.008001
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.012001
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.012
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.012001
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.016001
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.012001
>>start = cpu_time(); f(); dt = cpu_time() - start; print dt
0.008001

You'll see the result is quantised to 4 ms (not sure what the .000001
bits are about!)

4ms is the clock rate of this machine, ie 250 Hz. This is a compile
time option for the linux kernel and is usually set in the range 100
Hz to 1000 Hz. The kernel doesn't measure CPU usage more accurately
than this.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Feb 6 '07 #4

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

Similar topics

77
4542
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
4
9869
by: JackyMove | last post by:
Dear all, I would like to get the time on how long to run a short segment of my c program in usec under Windows platform using dev-c++. Is there any method or function call to get it other than using clock()? Thank you very much. Regards, Jacky
38
2541
by: vashwath | last post by:
Might be off topic but I don't know where to post this question.Hope some body clears my doubt. The coding standard of the project which I am working on say's not to use malloc.When I asked my lead(I have just started working) he said we should not use dynamic allocation in real time systems, the code will not run in predictable time period.Can anybody tell what does he mean?Why the execution time becomes unpredictable? Thanks
3
9293
by: SSG | last post by:
Hi All! I know how to calculate the running time of a particular routine, but i want to know how to reduce the running time in a program........... can anyone knows help me........ also tell me the definitions for system time, user time, real time..........
13
3197
by: pb648174 | last post by:
Whenever I want help on a query, I get told my design is wrong, So this time I'm posting a message during the design phase: How am I going to perfectly design the following? We want to be able to track time for users for multiple modules, for now a Schedule module and a Punchlist module. These modules already exist and there are dozens of other modules which we will add to the list as well, two or three at a time - so it should be...
5
7651
by: Tobiah | last post by:
The manual says: On Unix, return the current processor time as a floating point number expressed in seconds. So I ran this program: #!/usr/bin/python import time
19
2645
by: xianwei | last post by:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main ( int argc, char *argv ) { long i = 10000000L; clock_t start, end; double duration;
0
5536
NeoPa
by: NeoPa | last post by:
Introduction: This seems like a very straightforward topic. Why would anyone need help finding MS Access Help related to this topic? I can't really answer that except to say that I know from experience that many do. I myself spent a great deal of time under the impression that it was non-existent. How to Find it: Different versions of MS Access structure their help systems differently, so don't expect it to be in the same place for each...
12
1870
by: Jeff | last post by:
As I'm learning PHP, I'm making a fair number of mistakes in syntax. In perl, you can turn on reading these errors from the browser by adding this: use CGI::Carp 'fatalsToBrowser'; Is there something like this in PHP? Or do I need to find the php.ini file and look to see where the error
0
7924
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
7854
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,...
1
7978
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
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
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();...
1
2364
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
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.