473,770 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3779
On 28 Sep, 07:00, Magesh <gs.mag...@gmai l.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...@gmai l.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_Keit h) 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...@gmai l.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_lab el [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_lab el [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(RUSAG E_SELF,&ru) ;
return
(double) (ru.ru_utime.tv _sec+ru.ru_stim e.tv_sec)
+ ((double) (ru.ru_utime.tv _usec+ru.ru_sti me.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.u kwrites:
[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_Keit h) 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.u kwrites:
[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*****@mindsp ring.comwrites:
Keith Thompson wrote:
>Duncan Muirhead <dm***@csl.co.u kwrites:
[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_Keit h) 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
16089
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
4152
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
75
9903
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 is there some deeper underlying reason?
38
2565
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
2
2353
by: TomislaW | last post by:
how can I calculate time needed for execution of my methods in asp.net app?
1
2661
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 file)? I couldn't find any parameter in pst_status structure that defines heap memory! ANy suggestions on how to calculate it? Thanks in advance.
25
5026
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 learning advanced C.thx.
1
392
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 me the current millisecond or something like that:
15
6440
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 but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
9619
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
9454
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
10102
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
7460
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
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.