LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-10-2010, 02:24 AM   #1
vignesh1988i
LQ Newbie
 
Registered: Jan 2010
Location: INDIA ,Chennai
Posts: 1

Rep: Reputation: 0
Wink i need to find out the execution time in a C program????


i need to find out the processing or operating time in milliseconds or in nanoseconds for the instructions or for the whole process!!!!!

for eg :

i need to calculate the time taken to execute :

main()
{
int n;
scanf("%d",&n);
if( n%2==0) // i need the processing time in milliseconds or in nano
printf("even ");
else
printf("odd");
getch();
}


and i dont want the time for whole program : )
 
Old 01-10-2010, 05:48 AM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Umm ... the time taken to execute main and everything it calls is the time for the whole program, unless I'm missing something in your question. Anyhow, you can just use the time command on your compiled code, e.g.:

Code:
gcc mycode.c -o mycode
time ./mycode
If you need finer grained statistics, look into using gprof.
 
Old 01-10-2010, 08:54 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by vignesh1988i View Post
i need to find out the processing or operating time in milliseconds or in nanoseconds for the instructions or for the whole process!!!!!
Measuring the time in nanoseconds, or measuring the time of a few instructions, is not meaningful. CPUs are pipelined enough that there is no useful meaning to the elapsed time of just a few instructions.

I think the finest granularity you can measure is with rdtsc (you can find more info easily with a google search).


Quote:
for eg :

i need to calculate the time taken to execute :
Despite the example, I don't have much of a guess at what you're trying to do. If you explain the purpose, maybe you'll get better help.

Quote:
if( n%2==0) // i need the processing time in milliseconds or in nano
Does that mean you want the time for just that test and the conditional branch? That is too little to measure. If you tried to use rdtsc to measure that, the attempt to measure would greatly change the thing you're trying to measure (you could measure the time of one rdtsc plus the code you want to measure. But if you subtract the time of one rdtsc from that result, the answer might not be close to what the code you're measuring would have taken without the rdtsc).

If the code you want is executed many times in the course of a very long program execution, then it could be sampled with oprofile to get a statistically valid estimate of an execution time that might be accurate to a nanosecond in extreme cases (normally oprofile doesn't give you close to nanosecond accuracy).
 
Old 07-29-2010, 10:56 AM   #4
sharkkcmohan
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
Wink use <time.h>

declare as

time_t start,end,time;

start;//from where u want to start

//and

time=end;//up to where is has to calculate will be saved in 'time'

//and print the 'time' using float
 
Old 07-29-2010, 11:34 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190Reputation: 1190
Quote:
Originally Posted by sharkkcmohan View Post
print the 'time' using float
Since you're a new member of LQ, you probably don't know to look at things such as the date the question was asked (over six months ago) and the number of posts that the person asking the question has made since then (zero).

Those two things add up to near certainty that the person who asked the question won't be reading your answer.

I'm sure you'd rather answer questions where someone is still interested in the answer.

I also think your answer was incorrect for the question that was asked (all my reasons for that opinion are already explained in post #3 of this thread). But since no one still cares about this answer, we don't need to debate which one is correct.

Last edited by johnsfine; 07-29-2010 at 11:36 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find out the last time a program was run? bilbod Linux - General 6 06-25-2008 12:46 PM
how to find the time of execution anandkj Linux - Newbie 2 12-20-2007 03:27 AM
I want to know the program execution time daggulasreedhar Programming 6 03-15-2007 05:07 AM
Key bindings program (custom pasting, program execution, etc.) jrdioko Linux - Software 2 02-05-2005 10:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration