Thread: time.h

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by wuzzo
    And my CLOCKS_PER_SECOND is 1000000...
    As I said, use %g when you want to print the elapsed time. The calculations are fine, it's just that floats don't have enough bits to do the math properly. We need to convert to something bigger - doubles.

    Edit:
    But that does seem a rather odd value. Your processor must be pretty fast.
    Last edited by whiteflags; 04-14-2007 at 01:05 AM.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The man page from BSD states:
    STANDARDS
    The clock() function conforms to ISO/IEC 9899:1990 (``ISO C90''). How-
    ever, Version 2 of the Single UNIX Specification (``SUSv2'') requires
    CLOCKS_PER_SEC to be defined as one million. FreeBSD does not conform to
    this requirement; changing the value would introduce binary incompatibil-
    ity and one million is still inadequate on modern processors.
    Still, in my earlier example with 15 being both start and stop, a float should easily handle that, since we'd end up with 1/1000.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    It could very well be that, waiting for getchar() doesn't need any clocks, so your clock counter isn't incremented at all and thus giving the same result independent of how long you wait to press enter.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's what it seems like to me. The only way I can get it to change is by causing a loop to do something inbetween the prompts. Otherwise, it doesn't ever increment. The math still seems off though, so I'm not sure. But I think cygwin is broken at the moment, because it doesn't have strtol, and I'm too lazy to reboot so its packages can take effect.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    Quote Originally Posted by KONI View Post
    It could very well be that, waiting for getchar() doesn't need any clocks, so your clock counter isn't incremented at all and thus giving the same result independent of how long you wait to press enter.
    KONI, you might just be right! ....
    I decided to add this, before the getchar();

    like,
    Code:
    while(x<1000000)
         x++;
    and finally it wasn't 0...but was like 0.09..
    Btw, (start-stop)/CLOCKS_PER_SEC should be in seconds right??

  6. #21
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by wuzzo87 View Post
    KONI, you might just be right! ....
    I decided to add this, before the getchar();

    like,
    Code:
    while(x<1000000)
         x++;
    and finally it wasn't 0...but was like 0.09..
    Btw, (start-stop)/CLOCKS_PER_SEC should be in seconds right??
    (stop-start) is clocks difference ... and guess what happens when you divide it by clocks_per_SEC(OND)

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by quzah View Post
    That's what it seems like to me. The only way I can get it to change is by causing a loop to do something inbetween the prompts. Otherwise, it doesn't ever increment.
    The clock doesn't tick when the process is sleeping. Calling sleep() or waiting for 45 minutes for keyboard input isn't going to increment the clock at all. I can't believe people are arguing over printf() format specifiers...

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    But the funny thing is,
    I did a timing for a mergesort program, and it always results in 0 time interval..
    I used a 'double' and %g for printing.

    Could it be that the sorting happens too fast?
    Even for 1000 items it prints 0 time interval....

  9. #24
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i was going to say maybe your computer is just really fast.. but a value of 0 would mean the CPU didnt spend any time on the operation, correct?

    can you post your code?

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by brewbuck View Post
    The clock doesn't tick when the process is sleeping. Calling sleep() or waiting for 45 minutes for keyboard input isn't going to increment the clock at all. I can't believe people are arguing over printf() format specifiers...
    That's what the FAQ says to do. The major portion of this thread is with regards to how the FAQ says to do it and the different results regarding it. Mine wasn't just sitting there the whole 45 minutes. It was running a loop outputting . four billion times.

    The wording on the description of clock is a bit vague. The number of clock cycles used by who? The CPU in general, or just the particular program? Turns out it appears to be the latter, but it could be mis-interpreted, which is has been by more than one person in this very thread.

    Another interesting thing to note was the way Cygwin happens to use CPU usage on my particular machine. While running the (my edit of the FAQ's code) previously posted code in the foreground, it takes 1&#37; CPU time. While running it in the background, it uses 100% CPU usage according to the task manager in WinXP.


    Quzah.
    Last edited by quzah; 04-14-2007 at 11:41 PM.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    Quote Originally Posted by nadroj View Post
    i was going to say maybe your computer is just really fast.. but a value of 0 would mean the CPU didnt spend any time on the operation, correct?

    can you post your code?
    Here's it:
    Code:
    int main(int argc, char **argv)
    {
            int array[100];
            int item,j, i=0;
            int *comp = malloc(sizeof(int));
            int comp_result;
            clock_t start, stop;
            double t;
    
            while(scanf("%d", &item) > 0)
            {
                    array[i] = item;
                    i++;
            }
    
            start = clock();
    
            sort(array,i,comp);
    
            stop = clock();
    
            t = (double)(stop-start)/(double)(CLOCKS_PER_SEC);
    
            comp_result = *comp;
    
            for(j=0; j < i; j++)
                    printf("%d ", array[j]);
    
            printf("\n");
            printf("%d comparisons made\n", comp_result);
            printf("Time taken : %.10g\n", t);
    
    
            return 0;
    }
    
    void sort(int array[],int size, int *comp)
    {
            int b[100];
            int n=size;
            naturalmergesort(array, b, n, comp);
    }
    
    static bool mergeruns(int a[], int b[], int n, int *comp)
    {
            int i=0, k=0, x;
            bool asc=true;
    
            while (i<n)
            {
                k=i;
                do x=a[i++]; while (i<n && x<=a[i]);
                while (i<n && x>=a[i]) x=a[i++];
                merge (a, b, k, i-1, asc, comp);
                asc=!asc;
            }
            return k==0;
    }
    
    
    void merge(int a[], int b[], int lo, int hi, bool asc, int *comp)
    {
            int l = lo; int r = hi;
            int k=asc ? l : r;
            int c=asc ? 1 : -1;
            int i=lo, j=hi;
            int comparisons = 0;
    
            while (i<=j)
            {
                if (a[i]<=a[j])
                {
                    b[k]=a[i++];
                    comparisons++;
                }
                else
                {
                    b[k]=a[j--];
                    comparisons++;
                }
                k+=c;
            }
            *comp = comparisons;
            /*printf("%d comparisons made\n", comparisons);*/
    }
    
    
    void naturalmergesort(int a[], int b[], int n, int *comp)
    {
            while (!mergeruns(a, b, n, comp) & !mergeruns(b, a, n, comp));
    }
    There's another one for sorting structs, but it's rather lengthy...

    I used the shell command 'time', with 15 numbers,

    said, 0:07.80elapse 0%CPU

  12. #27
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    My version of mergesort only displays a result starting from 15k upwards. I guess the process is probably too fast to be displayed with 6 decimal places.

  13. #28
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    @wuzzo87:

    since your using 'bool', are you writing a C++ program? or have you included or defined whatever you need to so you can use 'bool'? notice you have a memory leak as you call 'malloc' without a corresponding 'free' on the variable.
    Last edited by nadroj; 04-15-2007 at 01:29 AM.

  14. #29
    Registered User
    Join Date
    Sep 2006
    Posts
    51
    Quote Originally Posted by nadroj View Post
    @wuzzo87:

    since your using 'bool', are you writing a C++ program? or have you included or defined whatever you need to so you can use 'bool'? notice you have a memory leak as you call 'malloc' without a corresponding 'free' on the variable.
    woops...forgot to add the free() ...again...
    Anyway, i've added the <stdbool.h> header to implement bools in C....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time.h ctime and milliseconds
    By Deewhyandy1 in forum C Programming
    Replies: 4
    Last Post: 01-26-2008, 01:42 AM
  2. Help with time.h functions please.
    By Ifurita. in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2003, 03:58 AM
  3. time.h and miliseconds question
    By Diamonds in forum C++ Programming
    Replies: 10
    Last Post: 12-16-2002, 08:41 AM
  4. Replies: 2
    Last Post: 10-18-2002, 08:30 AM
  5. a time.h problem
    By Max in forum C Programming
    Replies: 14
    Last Post: 10-15-2002, 02:43 PM