LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2011, 12:14 PM   #1
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Rep: Reputation: 0
clock()?


Hi all,

im trying to calculate how much time does my program run, use very simple script

clock_t end, start;
start= clock();
int i;
printf("initial %d\n",(int)start);

for(i=0;i<20;i++)
{
end=clock();
printf("Time elapsed: %d\n",(int)end);
}

but it outputs 0 all the time. cant figure out where could be the problem.

any ideas?
 
Old 05-11-2011, 02:10 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by nushki View Post
...it outputs 0 all the time.
That's because your process is using basically no processor time, since it's not doing anything. Try doing something like this (without enabling any optimization):

Code:
#include <stdio.h>
#include <time.h>

int main(void)
{
    clock_t end, start;
    start = clock();
    int i;
    printf("initial %d\n", (int) start);

    for (i = 0; i < 20; i++) {
        end = clock();
        printf("Time elapsed: %d\n", (int) end);
        int p, q, r;
        for (p = 0 ; p < 1000 ; p++)
            for (q = 0 ; q < 1000 ; q++)
                for (r = 0 ; r < 1000 ; r++)
                    printf("");
    }
}
 
Old 05-11-2011, 02:50 PM   #3
nushki
LQ Newbie
 
Registered: Mar 2009
Posts: 26

Original Poster
Rep: Reputation: 0
oh my gush, you are right!!! thank you so much!!!!
 
  


Reply



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
starting kttsd failed - and how to you set clock to 12 hour clock? Ook Slackware 3 08-13-2010 05:04 PM
BYE Clock skew detected. Check the clock on the file server Guardian-Mage Linux - Server 7 04-21-2009 09:48 AM
Slow system clock and stray hardware clock, pleas help! Epox Linux - General 6 01-02-2007 02:43 PM
Linux OS Clock/Timezone shows the clock off by 6 hours between OS's JBailey742 Linux - Software 9 04-06-2006 11:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 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
Open Source Consulting | Domain Registration