473,608 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How many CPU cycles does an instruction take ?

I am talking of C/C++ on unix platform using gcc. So feel free
to take off any crossposted NG's if others are not offended,
but I seek generic C/C++ and also unix and gcc specific answers.

Essentially:

/1/ How do I time a program in seconds or CPU cycles?
/2/ How do I use the facilities to increase performance of
any given program?
/3/ CPU has many clocks I have heard. There is also a bus
clock.
/4/ I looked in a number of books. I found a little on time.h.
But being humble, I thought I would ask experts as well.

I have played with the statements like:

#include <time.h>

cycles=10000;

start = clock();
for (i=0; i<cycles; i++){
return = slowfunction( arguments );
}
end = clock( );

printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for %d calls: %f \n",
start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end - start)/CLOCKS_PER_SEC) );
printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );
// I am not exactly clear about CLOCKS_PER_SEC, difftime() .
// I wrote this w/o clear conceptual understanding .
// But I want clear concept so I know it works before I write it.
// Using: gcc 2.7.2.3
Kevin Klein

Jul 22 '05 #1
4 6312

"Kevin Klein" <ne****@orz.com > wrote in message

/1/ How do I time a program in seconds or CPU cycles?
As you've done it. A slow function can be timed in seconds, using time(), a
fast function in clock ticks using clock().
/2/ How do I use the facilities to increase performance of
any given program?
You really need a profiler to tell you where the program is spending its
time. Then you optimise the bottlenecks. Occasionally timing something will
tell you whether one version is faster than another, but it won't be the
main weapon in your armoury.
/3/ CPU has many clocks I have heard. There is also a bus
clock.
Things get complicated with modern processors. Early machines would execute
exactly one instruction per cycle. However now processors are very fast but
memory reads and writes haven't caught up, also some instructions are
executed in parallel.
/4/ I looked in a number of books. I found a little on time.h.
But being humble, I thought I would ask experts as well.

I have played with the statements like:

#include <time.h>

cycles=10000;

start = clock();
for (i=0; i<cycles; i++){
return = slowfunction( arguments ); This is a syntax error. "return" is a keyword. }
end = clock( );

printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for %d calls: %f \n", start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end - start)/CLOCKS_PER_SEC) ); printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );

On your machine clock_t may well be an int, however this isn't guaranteed.
You need to cast values before you pass them to printf().
Jul 22 '05 #2
[snips]

On Thu, 01 Apr 2004 09:55:18 -0500, Kevin Klein wrote:
/1/ How do I time a program in seconds or CPU cycles?
Your best bet is a profiling tool.
/2/ How do I use the facilities to increase performance of
any given program?
By figuring out which bits of the program suck up the most time and
therefore could benefit from optimization.
I have played with the statements like:

#include <time.h>

cycles=10000;

start = clock();
for (i=0; i<cycles; i++){
return = slowfunction( arguments );
}
end = clock( );

printf("Start time: %d, Stop time: %d, CLOCKS_PER_SEC: %d\nSeconds for
%d calls: %f \n",
start, end, CLOCKS_PER_SEC, cycles, ((dbl)(end -
start)/CLOCKS_PER_SEC) );
printf("Ticks for %d calls: %f \n", cycles, difftime(end, start) );


difftime tells you the difference (in seconds) from time2 to time1 (oddly
enough. You'd think the order would be the other way around, but anyhow...)

It's a potentially useful metric... but not really good for finding
hotspots in your code, without a fair bit of work. You really need a
profiler - something that'll "automatica lly" watch your program run,
recording what it's doing, how long it's taking, etc. It should also
produce a report saying which functions were called the most frequently,
which ones took the most time, etc.
Jul 22 '05 #3
Kevin Klein wrote:
I am talking of C/C++ on unix platform using gcc. So feel free
to take off any crossposted NG's if others are not offended,
but I seek generic C/C++ and also unix and gcc specific answers.

Essentially:

/1/ How do I time a program in seconds or CPU cycles? Off-topic in all newsgroups you posted to.
Cpu cycles and instruction timing are dependent on the actual
processor. Unix and gcc run on many processors.

/2/ How do I use the facilities to increase performance of
any given program? Use a profiler.

/3/ CPU has many clocks I have heard. There is also a bus
clock. Some have many clocks some have a few. Some may have a separate
bus clock others may have one clock to rule them all.
Again, depends on your processor. For example, the ARM processor
differs in clock cycles from an Intel, Motorola and Signetics
processor. Texas Instruments has different processors that
each have different clock cycles.

/4/ I looked in a number of books. I found a little on time.h.
But being humble, I thought I would ask experts as well.
[snip]

Ask in a newsgroup about your platform.
You _could_ also try one of the "asm" newsgroups as well as
news:comp.arch. embedded.

Kevin Klein

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
In <uN************ *******@newssvr 16.news.prodigy .com> Thomas Matthews <Th************ *************** *@sbcglobal.net > writes:
Kevin Klein wrote:
I am talking of C/C++ on unix platform using gcc. So feel free
to take off any crossposted NG's if others are not offended,
but I seek generic C/C++ and also unix and gcc specific answers.

Essentially:

/1/ How do I time a program in seconds or CPU cycles?
^^^^^^^^^^Off-topic in all newsgroups you posted to.


Timing a program in seconds is on topic on all the groups where time(),
difftime() and clock() are topical, and this includes comp.lang.c,
comp.lang.c++ and comp.unix.progr ammer.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Jul 22 '05 #5

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

Similar topics

9
4157
by: Hayko Riemenschneider | last post by:
Hi! I've got me an XSL tranformation stylesheet for my XML file. In the XSL file I now wish to use PHP to do some scripting. So I thought I'll use the PIs like this: <xsl:processing-instruction name="php"> echo $hello; </xsl:processing-instruction>
3
2207
by: Trogdor | last post by:
I set up a server on an AMD 650 machine running gentoo linux. I installed Apachie 2, MySQL 4.1 and PHP 4.3.11 I use another computer on my local net (192.168.0.x) to access the server as a client. MySQL works perfectly. I have created and queried databases with no problem. Apachie 2 appears to work with no problem. I can call up web pages in the expected maner.
4
545
by: Kevin Klein | last post by:
I am talking of C/C++ on unix platform using gcc. So feel free to take off any crossposted NG's if others are not offended, but I seek generic C/C++ and also unix and gcc specific answers. Essentially: /1/ How do I time a program in seconds or CPU cycles? /2/ How do I use the facilities to increase performance of any given program? /3/ CPU has many clocks I have heard. There is also a bus
11
4387
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple assignment. int b = some_value;
58
30205
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
7
2574
by: Lalasa | last post by:
Hi, Can anybody tell me how many cpu cycles File.copy would take and how many cpu cycles File.Move would take? CFile::Rename in C++ takes just one cpu cycle. As there is no File.Rename in C#, I am worried about using File.copy or File.Move which would do the same job of renaming a file but I want it done in just one cpu cycle.
4
1826
by: Martin Blais | last post by:
Hi I'm a tad confused over a problem involving cycles between packages. Assume the following sets of files:: driver.py a/__init__.py a/alice.py
71
10707
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or is that automatic? Thanks
2
1683
by: skip | last post by:
We encountered a situation today where it appeared that a Boost.Python-provided class didn't participate in Python's cyclic garbage collection. The wrapped C++ instance held a reference to a method in the Python object which referenced the Boostified C++ instance, e.g.: class Foo: def __init__(self, ...): self.over_there = BoostifiedClass() self.over_there.set_callback(self.boost_callback) ...
0
8063
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
8002
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
8475
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8148
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
6816
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6013
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
3962
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...
1
2474
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
0
1329
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.