473,383 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 10520
On Feb 5, 2:37 am, "jm.sur...@no.spam.gmail.com" <jm.sur...@gmail.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.spam.gmail.com" <jm.sur...@gmail.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*********@mvista.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.spam.gmail.com" <jm.sur...@gmail.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.getrusage(resource.RUSAGE_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
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...
4
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...
38
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...
3
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...
13
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...
5
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
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
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...
12
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.