473,387 Members | 1,844 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,387 software developers and data experts.

finding how much time that is consumed?

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

Aug 18 '06 #1
5 4090


ra*****@yahoo.no wrote On 08/18/06 09:29,:
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?
("Clocksycluses?" After some puzzlement the light dawned:
I *think* what you mean is the two-word phrase "clock cycles."
However, the word "clocksycluses" has a certain fascination,
and people may take it up and start using it. You may have
gained immortality by enriching the lexicon!)

C provides the clock() function, which returns the amount
of CPU time consumed by your program since some fixed arbitrary
moment. You use it like this:

#include <stdio.h>
#include <time.h>
...
clock_t t0, t1;
t0 = clock();
do_something();
t1 = clock();
printf ("Used %g CPU seconds\n",
(t1 - t0) / (double)CLOCKS_PER_SEC);
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?
There are at least two problems here. First, the Standard
says nothing about how precise the clock() measurement is, how
rapidly the clock "ticks." On typical systems, the "tick rate"
is somewhere between 18Hz and 1000Hz; 100Hz is a fairly common
value. What this means is that clock() is probably too coarse-
grained to measure the execution time of a few instructions or
even a few tens of instructions; the measured time for something
as short as one division will probably be zero.

The second problem is that the C language says nothing about
how much time various operations take. On actual machines, the
time taken for your division will probably be affected by many
influences, such as

- Operand type: floating-point divisions and integer
divisions might run at different speeds

- Operand values: dividing by a denormal might take more
or less time than dividing by a normalized value

- Operand location: there's probably a cascade of different
places the operands might reside (CPU, various caches,
main memory, swap device), all with different speeds

- Interference: the division might compete with other
operations for scarce resources like pipelines, floating-
point units, internal CPU latches, and whatnot

.... and, of course, many more. Modern computers are complicated
systems, and it is all but meaningless to speak of "the" amount
of time a single operation takes.

--
Er*********@sun.com

Aug 18 '06 #2
Eric Sosman wrote:
>
ra*****@yahoo.no wrote On 08/18/06 09:29,:
>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?

("Clocksycluses?" After some puzzlement the light dawned:
I *think* what you mean is the two-word phrase "clock cycles."
However, the word "clocksycluses" has a certain fascination,
and people may take it up and start using it. You may have
gained immortality by enriching the lexicon!)

C provides the clock() function, which returns the amount
of CPU time consumed by your program since some fixed arbitrary
moment. You use it like this:

#include <stdio.h>
#include <time.h>
...
clock_t t0, t1;
t0 = clock();
do_something();
t1 = clock();
printf ("Used %g CPU seconds\n",
(t1 - t0) / (double)CLOCKS_PER_SEC);
>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?

There are at least two problems here. First, the Standard
says nothing about how precise the clock() measurement is, how
rapidly the clock "ticks." On typical systems, the "tick rate"
is somewhere between 18Hz and 1000Hz; 100Hz is a fairly common
value. What this means is that clock() is probably too coarse-
grained to measure the execution time of a few instructions or
even a few tens of instructions; the measured time for something
as short as one division will probably be zero.
Most platforms do have useful (but non-portable) ways to measure clock
ticks. Often, it is done by counting bus clock ticks and multiplying by
a factor burned into the internal ROM of the CPU. It likely takes an
indeterminate number of ticks to obtain the result. See the _rdtsc()
macros built into certain compilers for x86 and related platforms.
There is not likely to be any relationship between native clock ticks
and the integral count returned by clock(); in fact, most
implementations cite Posix standards as requiring that clock() must
return some large increment after each native time interval, to permit
"posix" applications to avoid using CLOCKS_PER_SEC, thus throwing away a
number of useful bits.
>
The second problem is that the C language says nothing about
how much time various operations take. On actual machines, the
time taken for your division will probably be affected by many
influences, such as

- Operand type: floating-point divisions and integer
divisions might run at different speeds

- Operand values: dividing by a denormal might take more
or less time than dividing by a normalized value

- Operand location: there's probably a cascade of different
places the operands might reside (CPU, various caches,
main memory, swap device), all with different speeds

- Interference: the division might compete with other
operations for scarce resources like pipelines, floating-
point units, internal CPU latches, and whatnot

... and, of course, many more. Modern computers are complicated
systems, and it is all but meaningless to speak of "the" amount
of time a single operation takes.
However, there are many architectures where a division stalls the
floating point pipeline for a fixed number of cycles, once it begins
execution, depending on the width of the operands.
Aug 18 '06 #3
ra*****@yahoo.no wrote:
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?
In a modern processor, with pipelining, forwarding, branch detection,
and other optimizations in the datapath, even at the machine instruction
level, it will be difficult to determine the exact number of clock
cycles used by a given sequence of instructions. It is relatively easy
to measure "wall time" for a routine, but counting clock cycles can be
very difficult to do.

Are you doing floating point division in your example? Here's something
to ponder: It's entirely possible that when your machine code runs,
your division has been performed before the statement in your code that
comes "before" the division. That may sound like a preposterous claim,
until you explore how datapath optimizations are done.

It is also quite possible that one run through your routine is evaluated
differently than another.

But this is all architecture-specific, and has nothing at all to do with
C programming, aside from the assumption that you reached your
architecture-specific code by writing C.

It would be more helpful if you explained what you were trying to
determine, and why. Are you trying to optimize something specific, or
are you simply trying to determine how many clock cycles a given set of
instructions will require on your platform? (You will need a debugger,
and an instruction set reference for your processor).

If you are trying to optimize at a higher level than machine code, there
may be decisions you can make in your C design that will generally be
beneficial across platforms, but that's going to fundamentally depend on
what you are doing, and how you are doing it.

Your example fragment doesn't get us anywhere near what we'd need to
actually give you advice.
Aug 18 '06 #4
<ra*****@yahoo.nowrote in message
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?
It depends on your computer.
If it is simple embedded processor with 8 bits probably each instruction
takes one or more cycle, and you can calculate execution time by counting
the cycles.
If it's modern Pentium VI, Hexium 7, or whatever it probaly has really
complicated scheduling, caching, multi-tasking code. Whilst there will be
some relationship between wall clock time and the number of instructions you
give the machine, it won't necessarily be a simple one.

You might even be programming a Chinese room. Assembly instructions are
translated, by a Chinaman, to Chinese, and put in room containing an English
speaker, who doesn't speak Chinese. However he has instructions for
manipulating the symbols, and the answer comes out in Chinese, which the
Chinaman understands. the then goes around madly putting ASCII characters on
little wooden boats onto a flowing channel of water, and you can read them
off.

This type of computer has certain philosophical advantages. However it is
not very fast. Which is surprising, seeing that humans can do tasks such as
image recognition much faster and more accurately than computers.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.
Aug 19 '06 #5
Thank you all for the answers.

It is actually embedded yes Malcolm, sorry (to everybody) for not
putting that strait.

I am thinking of taking FFT on some signals and I am curious about how
much time it will take.
Seams like I will start a counter before the function and stop it after
to get a good average time on the function.
I guess I was curious about if there was a way to simulate the amount
of clock cycles used.

I am thinking of using a 32bit processor in an FPGA (MicroBlaze).

Raymond

Aug 20 '06 #6

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

Similar topics

8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
1
by: Sean | last post by:
I'm trying to find out the amount of memory used by a data structure (Hashtable) in a program. If I look at the values for "Mem Usage" and "VM Size" under the processes tab in the Windows Task...
0
by: U S Contractors Offering Service A Non-profit | last post by:
This Sunday the 26th 2006 there will be Music @ Tue Nov Inbox Reply Craig Somerford to me show details 9:54 pm (26 minutes ago) #1St "CLICK" HeAt frOm A blanket...
2
by: prathamesh.deshpande | last post by:
Hi All, I have a program written in VC++. I want to calculate the memory consumed by each of the individual functions within the program. So can anyone suggest how this could be done? I need to...
7
by: Bruce | last post by:
In a site that has about 4000 products, in 1000 categories, I thought that I can store each product and category details in its own application("var"), instead of trips to the database. When...
2
by: Karthik Gurusamy | last post by:
Hi, Wondering if there is a way to measure a child process's cpu usage (sys and user) when the child is still running. I see os.times() working fine in my system (Linux 2.6.9-42.7.ELsmp), but it...
7
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. ...
7
by: Nick | last post by:
Hi there, I have a website that functions fine locally, but when published to the server it develops a bottleneck during loading some of the pages. Basically what happens is the page loads to...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.