Results 1 to 6 of 6

Thread: Timing programs in C

  1. #1
    Join Date
    Aug 2006
    Posts
    142

    Timing programs in C

    Hello Friends,
    I am stuck in the coding of C language. I am new to the C programming language. And I am trying to make a solar system program. I'm trying to use the functions getrusage or times, or the time command to time my programs. But I am not getting the desired results for that. I was wondering if anybody could help me with timing in C. Does anyone know how to use Timing programs in C.?? Please try to help me immediately.
    Thanks in Advance.!!
    ~HARE KRISHNA HARE RAM~

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Timing programs in C

    Since you are new in C programming language, I think that you should know something basic about the timings that are used in the programming language. Uses of time functions include:
    1. timing programs and functions.
    2. telling the time.
    3. setting number seeds.
    Some of the basic time functions are prototypes as follows:

    time_t time(time_t *tloc)
    This command will return the time since 00:00:00 GMT, Jan. 1, 1970,
    which is measured in seconds.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Timing programs in C

    You can use the different functions while including that function in #include <time.h>. The two other functions are described as follows :
    • char *ctime(time_t *clock),
    • char *asctime(struct tm *tm)

    ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime().
    asctime() converts a time value contained in a tm structure to a 26-character string of the form:
    Sun Sep 16 01:03:52 1973
    Also the asctime() returns a pointer to the string.

  4. #4
    Join Date
    Mar 2008
    Posts
    672

    Re: Timing programs in C

    I am providing you the simple program that illustrates that calling the time function at distinct moments.
    Code:
    /* timer.c */
     
    #include <stdio.h>
    #include <sys/types.h>
    #include <time.h>
     
    main()
      {  int i;
    				 time_t t1,t2;
     
    				 (void) time(&t1);
    				 for (i=1;i<=250;++i)
    				   printf(``%d %d %d\n'',i, i*i, i*i*i);
    				  (void) time(&t2);
    				  printf(``\n Time to do 250 squares and
    				 cubes= %d seconds\n'', (int) t2-t1);
    		 }

  5. #5
    Join Date
    Mar 2008
    Posts
    349

    Re: Timing programs in C

    I think that you can also use the SetTimer(); for setting the Timing programs in C. Its event driven so if you do:
    SetTimer(1, 50, NULL);
    By using this you can call the function OnTimer(); every 50 milliseconds. Also you have not told that exactly how your program is set. So I can't tell you how to use OnTimer(); properly. I just right click on my dlg class and select "add windows message handler" then add "WM_TIMMER" which is declared as "afx_msg void OnTimer(UINT nIDEvent);" I hope that the OnTimer(); will help you to set the Timings.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,192

    Re: Timing programs in C

    You can also try the following coding that I have made for setting the timings in C program. Here is the coding for that :
    Code:
    #include<stdio.h>
    #include<sys/time.h>
    #include<stdlib.h>
    #include<sys/resource.h>
           
    double getcputime();
    
          int main(void)
          {
          int i, n, a;
          double t;
          n = 15;
          printf("The 5-times Table\n");
          for(i=1;i<n;i++)
          {
          a = (i * 5);
          printf("%d\t%d\n", i, a);
          }
           
          printf("\n");
          t = getcputime();
          printf("The total time taken by the system is: %d (in decimal format)\n", t);
           
          return 0;
          }
     
          double getcputime(void)
          {
          double t;
          struct timeval tim;
          struct rusage ru;
          getrusage(RUSAGE_SELF, &ru);
          tim=ru.ru_stime;
          t=(double)tim.tv_sec * 1000000.0 + (double)tim.tv_usec;
          return t;
          }

Similar Threads

  1. Nba2k12 Online Jumpshot Timing Vs The Regular Game Timing
    By UNCLEBREADMAN in forum Video Games
    Replies: 1
    Last Post: 26-12-2011, 03:37 PM
  2. Bad Timing with TWIN2X4096-6400C5C
    By Farzana in forum Motherboard Processor & RAM
    Replies: 5
    Last Post: 24-07-2011, 12:30 AM
  3. Help to Set RAM timing to use with MSI 790FX-GD70
    By Ansari Bros in forum Motherboard Processor & RAM
    Replies: 4
    Last Post: 14-03-2011, 07:17 AM
  4. Error in timing with KHX6400D2K2/4G Ram
    By X-quisite in forum Hardware Peripherals
    Replies: 6
    Last Post: 09-11-2010, 07:23 AM
  5. Lower memory timing
    By Hakon in forum Motherboard Processor & RAM
    Replies: 3
    Last Post: 28-08-2009, 09:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,093,480.27274 seconds with 17 queries