Time.h and counter

This is a discussion on Time.h and counter within the C Programming forums, part of the General Programming Boards category; Can someone take a look and verify I am getting the proper results? #include <stdio.h> #include <time.h> int main ( ...

  1. #1
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52

    Time.h and counter

    Can someone take a look and verify I am getting the proper results?

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

    int main ( ) {
    clock_t start, end;

    int i;
    long count;
    count = 1000000;

    start = clock();


    printf( "Counting to %d\n", count );

    for (i=0; i < count; i ++);




    end = clock();

    printf( "That took %d seconds and I counted up to %d", (end-start)/CLOCKS_PER_SEC, count );
    printf( "\nThat also took %d clock tics\n ", clock());
    return 0;
    }

    Thanks,
    Mattz

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Exclamation

    Fixed code:

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

    int main ( )
    {

    clock_t start, end;
    long i, count=1000000;

    printf( "Counting to %ld\n", count );
    start = clock();
    for (i=0; i < count; i++)
    {

    if (count == i-1) end = clock();

    }
    printf("That took %f seconds and I counted up to %ld", (end-start)/CLOCKS_PER_SEC, count);
    printf( "\nThat also took %f clock tics\n", clock());
    return 0;
    }
    Last edited by GaPe; 12-11-2001 at 01:08 PM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about?

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

    int main ( ) {
    clock_t start, end;

    long i;
    long count;
    count = 10000000;

    start = clock();

    printf( "Counting to %ld\n", count );

    for (i=0; i < count; i++);

    end = clock();

    printf( "That took %f seconds and I counted up to %ld", (end-start)/CLOCKS_PER_SEC, count );
    printf( "\nThat also took %ld clock tics\n ", clock());
    return 0;
    }

  4. #4
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Both of the above examples caused some errors when run for me. Thanks anyway. Is there a way to display milliseconds vs just seconds....make it more accurate?

    Matt

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you are using a Windows compiler, you can use GetTickCount() in <windows.h>

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    You can divide the seconds with 1000 and you get miliseconds.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    #include <stdio.h>
    #include <time.h>

    int main ( ) {
    time_t t;
    clock_t start, end;
    long i;
    long count;
    count = 1000000;

    start = clock();

    time(&t);

    printf(ctime(&t));
    printf( "Counting to %ld\n", count );

    for (i=0; i < count; i ++);




    end = clock();

    printf( "That took %ld seconds and I counted up to %ld", (((end-start)/CLOCKS_PER_SEC)/1000), count );
    printf( "\nThat also took %d clock tics\n ", clock());
    return 0;
    }
    tried this....

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If I remember correctly, you cannot use time()/etc for milliseconds. Additionally, you cannot divide the seconds by 1000 to get milliseconds. That is backwards. Consider:

    3 seconds

    3 seconds / 1000 = .003 seconds? No. This is wrong. To get milliseconds from seconds, you would multiply.

    3 seconds * 1000 = 3000 milliseconds.

    See? You cannot gain millisecond precision this way.

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

  9. #9
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    uf uf STUPID ME.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  10. #10
    Registered User mattz's Avatar
    Join Date
    Nov 2001
    Posts
    52
    Okay figured out answer through some searches and some local help:

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

    int main ( ) {
    time_t t;
    clock_t start, end;
    long i;
    long count;
    double x = 0.0;
    count = 1000000;

    start = clock();

    time(&t);

    printf(ctime(&t));
    printf( "Counting to %ld\n", count );

    for (i=0; i < count; i ++);




    end = clock();

    printf( "That took %f seconds and I counted up to %ld", (((double)(end-start)/CLOCKS_PER_SEC)), count );
    printf( "\nThat also took %d clock tics\n ", clock());
    return 0;
    }
    Mattz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 05:36 PM
  2. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 02:17 AM
  3. Flowchart Question
    By dmkanz07 in forum C Programming
    Replies: 1
    Last Post: 04-08-2007, 11:33 AM
  4. Counter Heap Sort
    By Achillles in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 12:17 PM
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21