Thread: Finding Program Running Time

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    Finding Program Running Time

    Hi, I'm a new C student, and I need to find out how long my program takes to run. i'm using the following code, but I get a ton of C2143 errors when I use this. When I don't use anything in <time.h>, my program compiles, and works perfectly. I'm using MS Visual C++ (even though I'm trying to program in C).

    Is there a problem with my code?
    Code:
    #include <time.h>
    int main ()
    { 
      clock_t start, end;
      double runTime;
      start = clock();
      /* my actual program */
      end = clock();
      runTime = ((end – start) / (double) CLOCKS_PER_SEC );
      printf (“Run time is %g seconds”, runTime);
      getch();
      return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pf732k3 View Post
    Hi, I'm a new C student, and I need to find out how long my program takes to run. i'm using the following code, but I get a ton of C2143 errors when I use this. When I don't use anything in <time.h>, my program compiles, and works perfectly. I'm using MS Visual C++ (even though I'm trying to program in C).
    Please give the exact text of the C2143 error.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    error C2143: syntax error : missing ';' before 'type'

    After the clock function, I'm initializing some int variables and some char arrays.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    I'm getting errors compiling your code because it has strange characters in it.

    Code:
      runTime = ((end – start) / (double) CLOCKS_PER_SEC );
      printf (“Run time is %g seconds”, runTime);
    On those two lines, the '-' character in the expression (end - start) isn't a normal hyphen character. Also, the quotes you used in your string literal aren't normal double quotes either.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main ()
    { 
      clock_t start, end;
      double runTime;
      start = clock();
      /* my actual program */
      end = clock();
      runTime = (end - start) / (double) CLOCKS_PER_SEC ;
      printf ("Run time is &#37;g seconds\n", runTime);
      getchar();
      return 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by arpsmack View Post
    I'm getting errors compiling your code because it has strange characters in it.
    What the heck was the OP using for an editor? Word?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Haha, you know I almost added that as an edit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar to keep program running
    By shadow1515 in forum C Programming
    Replies: 8
    Last Post: 05-25-2008, 02:33 AM
  2. Multi - File Program not finding files
    By johnh444 in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2004, 01:48 AM
  3. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  4. Geophysics student needs help in running a C++ program
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 01-29-2002, 02:15 AM
  5. Running program on background?
    By Couhilin in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2001, 07:50 AM