473,402 Members | 2,055 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,402 software developers and data experts.

How to calculate the execution time for a peace of code in Unix env?

Consider a block,

fncall( ); /* tells me the current millisecond or something like that:
time-1 */
{/* block of code for which I need to know the exec time
...
...
...
}
fncall( ); /* tells me the current millisecond or something like that:
time-2 */

now I can calculate the diff in time to know the exec time for that
particular block. Is there any way like this?

Thx in advance to all who participate in this discussion...
- Magesh

Sep 28 '07 #1
8 3747
On 28 Sep, 07:00, Magesh <gs.mag...@gmail.comwrote:

<snip>
fncall( ); /* tells me the current millisecond or something like that:
time-1 */
{/* block of code for which I need to know the exec time
..
..
..}

fncall( ); /* tells me the current millisecond or something like that:
time-2 */

now I can calculate the diff in time to know the exec time for that
particular block. Is there any way like this?
look at clock(). It doesn't give msec but it can be used for timeing.
You could run the inner loop many times to give a better reading.
--
Nick Keighley

Sep 28 '07 #2
Nick Keighley <ni******************@hotmail.comwrites:
On 28 Sep, 07:00, Magesh <gs.mag...@gmail.comwrote:
<snip>
>fncall( ); /* tells me the current millisecond or something like that:
time-1 */
{/* block of code for which I need to know the exec time
..
..
..}

fncall( ); /* tells me the current millisecond or something like that:
time-2 */

now I can calculate the diff in time to know the exec time for that
particular block. Is there any way like this?

look at clock(). It doesn't give msec but it can be used for timeing.
You could run the inner loop many times to give a better reading.
Keep in mind that the value returned by clock() indicates the amount
of CPU time used by the program. If that's what you need, that's
great, but if you're looking for wall clock time it's not going to
help you much.

You're probably better off using some system-specific method such as a
profiler.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #3
On Sep 28, 11:00 am, Magesh <gs.mag...@gmail.comwrote:
Consider a block,

fncall( ); /* tells me the current millisecond or something like that:
time-1 */
{/* block of code for which I need to know the exec time
..
..
..}

fncall( ); /* tells me the current millisecond or something like that:
time-2 */

now I can calculate the diff in time to know the exec time for that
particular block. Is there any way like this?

Thx in advance to all who participate in this discussion...
- Magesh
Lot of profilers are available.

Check : http://www.cs.utah.edu/dept/old/texi...rof.html#SEC13

Here is an example portion of a call graph which shows a cycle
containing functions a and b. The cycle was entered by a call to a
from main; both a and b called c.

index % time self children called name
----------------------------------------
1.77 0 1/1 main [2]
[3] 91.71 1.77 0 1+5 <cycle 1 as a whole[3]
1.02 0 3 b <cycle 1[4]
0.75 0 2 a <cycle 1[5]
----------------------------------------
3 a <cycle 1[5]
[4] 52.85 1.02 0 0 b <cycle 1[4]
2 a <cycle 1[5]
0 0 3/6 c [6]
----------------------------------------
1.77 0 1/1 main [2]
2 b <cycle 1[4]
[5] 38.86 0.75 0 1 a <cycle 1[5]
3 b <cycle 1[4]
0 0 3/6 c [6]
----------------------------------------

(The entire call graph for this program contains in addition an entry
for main, which calls a, and an entry for c, with callers a and b.)
index % time self children called name
<spontaneous>
[1] 100.00 0 1.93 0 start [1]
0.16 1.77 1/1 main [2]
----------------------------------------
0.16 1.77 1/1 start [1]
[2] 100.00 0.16 1.77 1 main [2]
1.77 0 1/1 a <cycle 1[5]
----------------------------------------
1.77 0 1/1 main [2]
[3] 91.71 1.77 0 1+5 <cycle 1 as a whole[3]
1.02 0 3 b <cycle 1[4]
0.75 0 2 a <cycle 1[5]
0 0 6/6 c [6]
----------------------------------------
3 a <cycle 1[5]
[4] 52.85 1.02 0 0 b <cycle 1[4]
2 a <cycle 1[5]
0 0 3/6 c [6]
----------------------------------------
1.77 0 1/1 main [2]
2 b <cycle 1[4]
[5] 38.86 0.75 0 1 a <cycle 1[5]
3 b <cycle 1[4]
0 0 3/6 c [6]
----------------------------------------
0 0 3/6 b <cycle 1[4]
0 0 3/6 a <cycle 1[5]
[6] 0.00 0 0 6 c [6]
----------------------------------------
Also,
Check : http://www.cs.mu.oz.au/research/merc...profiling.html

Here is a small portion of the call graph profile from an example
program.

called/total parents
index %time self descendents called+self name
index
called/total children
<spontaneous>
[1] 100.0 0.00 0.75 0
call_engine_label [1]
0.00 0.75 1/1
do_interpreter [3]

-----------------------------------------------

0.00 0.75 1/1
do_interpreter [3]
[2] 100.0 0.00 0.75 1 io.run/0(0) [2]
0.00 0.00 1/1
io.init_state/2(0) [11]
0.00 0.74 1/1 main/2(0)
[4]

-----------------------------------------------

0.00 0.75 1/1
call_engine_label [1]
[3] 100.0 0.00 0.75 1 do_interpreter
[3]
0.00 0.75 1/1 io.run/0(0)
[2]

-----------------------------------------------

0.00 0.74 1/1 io.run/0(0)
[2]
[4] 99.9 0.00 0.74 1 main/2(0) [4]
0.00 0.74 1/1 sort/2(0)
[5]
0.00 0.00 1/1 print_list/
3(0) [16]
0.00 0.00 1/10
io.write_string/3(0) [18]

-----------------------------------------------

0.00 0.74 1/1 main/2(0)
[4]
[5] 99.9 0.00 0.74 1 sort/2(0) [5]
0.05 0.65 1/1 list.perm/
2(0) [6]
0.00 0.09 40320/40320 sorted/1(0)
[10]

-----------------------------------------------

8 list.perm/
2(0) [6]
0.05 0.65 1/1 sort/2(0)
[5]
[6] 86.6 0.05 0.65 1+8 list.perm/2(0)
[6]
0.00 0.60 5914/5914 list.insert/
3(2) [7]
8 list.perm/
2(0) [6]

-----------------------------------------------

0.00 0.60 5914/5914 list.perm/
2(0) [6]
[7] 80.0 0.00 0.60 5914 list.insert/3(2)
[7]
0.60 0.60 5914/5914 list.delete/
3(3) [8]

-----------------------------------------------

40319 list.delete/
3(3) [8]
0.60 0.60 5914/5914 list.insert/
3(2) [7]
[8] 80.0 0.60 0.60 5914+40319 list.delete/3(3)
[8]
40319 list.delete/
3(3) [8]

-----------------------------------------------

0.00 0.00 3/69283 tree234.set/
4(0) [15]
0.09 0.09 69280/69283 sorted/1(0)
[10]
[9] 13.3 0.10 0.10 69283 compare/3(0) [9]
0.00 0.00 3/3
__Compare___io__stream/0(0) [20]
0.00 0.00 69280/69280
builtin_compare_int/3(0) [27]

-----------------------------------------------

0.00 0.09 40320/40320 sort/2(0)
[5]
[10] 13.3 0.00 0.09 40320 sorted/1(0) [10]
0.09 0.09 69280/69283 compare/3(0)
[9]

-----------------------------------------------
Karthik Balaguru

Sep 28 '07 #4
On Thu, 27 Sep 2007 23:00:26 -0700, Magesh wrote:
Consider a block,

fncall( ); /* tells me the current millisecond or something like that:
time-1 */
{/* block of code for which I need to know the exec time
..
..
..
}
fncall( ); /* tells me the current millisecond or something like that:
time-2 */

now I can calculate the diff in time to know the exec time for that
particular block. Is there any way like this?

Thx in advance to all who participate in this discussion...
- Magesh
Off topic
/* return CPU seconds (system + user) used since program start start
*/
#include <sys/time.h>
#include <sys/resource.h>
double second( void)
{
struct rusage ru;
getrusage(RUSAGE_SELF,&ru) ;
return
(double) (ru.ru_utime.tv_sec+ru.ru_stime.tv_sec)
+ ((double) (ru.ru_utime.tv_usec+ru.ru_stime.tv_usec))*1.0e-6
;
}
The advantage of using this over clock() or gettimeofday() is that those
measure elapsed time, whereas the above measures the processor time used
by your process only.

Sep 28 '07 #5
Duncan Muirhead <dm***@csl.co.ukwrites:
[snip]
The advantage of using this over clock() or gettimeofday() is that those
measure elapsed time, whereas the above measures the processor time used
by your process only.
No, clock() measures processor time.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #6
Duncan Muirhead wrote:

[...]

The advantage of using this over clock() or gettimeofday() is that those
measure elapsed time, whereas the above measures the processor time used
by your process only.
Nope

7.23.2.1 The clock function
Synopsis
#include <time.h>
clock_t clock(void);
Description
The clock function determines the processor time used.
--
Tor <torust [at] online [dot] no>
"I have stopped reading Stephen King novels. Now I just read C code instead"
Sep 28 '07 #7
Keith Thompson wrote:
>
Duncan Muirhead <dm***@csl.co.ukwrites:
[snip]
The advantage of using this over clock()
or gettimeofday() is that those
measure elapsed time,
whereas the above measures the processor time used
by your process only.

No, clock() measures processor time.
clock() is for timing program execution.
If you want to know if one function runs faster than another,
clock() is good for that.

The subject line of this thread:
How to calculate the execution time for a peace of code in Unix env?
suggests to me that the use of clock() may be appropriate.

--
pete
Sep 28 '07 #8
pete <pf*****@mindspring.comwrites:
Keith Thompson wrote:
>Duncan Muirhead <dm***@csl.co.ukwrites:
[snip]
The advantage of using this over clock()
or gettimeofday() is that those
measure elapsed time,
whereas the above measures the processor time used
by your process only.

No, clock() measures processor time.

clock() is for timing program execution.
If you want to know if one function runs faster than another,
clock() is good for that.
Probably, but it depends on what you mean by "faster".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 10 '07 #9

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

Similar topics

7
by: Tim Quon | last post by:
Hi Is there any function to get the current time so I can calculate the execution time of my code? What all is in the time.h and sys/times.h? Thanks Tim
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
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...
2
by: TomislaW | last post by:
how can I calculate time needed for execution of my methods in asp.net app?
1
by: Bujji | last post by:
Hi, How to calculate Heap memory used by a process in HPUX machine (Itanium) using aCC compiler at run time ( My ultimate aim is to calculate heap memory at run-time and write it into a log...
25
by: Umesh | last post by:
i want to calculate the time required to execute a program. Also i want to calcute the time remaining for the execution of the program. how can i do that? pl mention some good websites for...
1
by: Magesh | last post by:
Consider a block, fncall( ); /* tells me the current millisecond or something like that: time-1 */ {/* block of code for which I need to know the exec time ... ... ... } fncall( ); /* tells...
15
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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,...
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.