Taking System Time in C

Asked

Viewed 3,962 times

4

I want to take the time of beginning and end of a for. Ex.:

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

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  int i, j;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  for (j = 0; j < 10; j ++)
        for (i = 0; i < 138763431; i ++);
  printf ( "\nCurrent local time and date: %s", asctime (timeinfo) );

  return 0;
}

I’m already using the GetTickCount() for seconds. I could manipulate but the way I’ve tried using the time.h the time is fixed when I call anywhere in my main.

  • I don’t understand why you don’t GetTickCount.

  • Put what you did and what went wrong.

  • because I have to show the start time and the end time. I will use Gettickcount to show the seconds between that time.

  • @edited mustache...

2 answers

3

See if it solves what you want with the code I found in the OS.

#include <stdio.h>
#include <sys\timeb.h> 

int main() { 
    struct timeb start, end;
    int diff;
    int i = 0;
    ftime(&start);

    while(i++ < 999) {
        /*gastando tempo*/
        printf(".");    
    }

    ftime(&end);
    diff = (int) (1000.0 * (end.time - start.time) + (end.millitm - start.millitm));

    printf("\nGastou %u milisegundos\n", diff);
    return 0;
}

It is possible to unlock the timeb. Behold in that reply:

void getCurrTimeString(TCHAR* mytime) {
    _timeb myTime;
    struct tm * timeinfo;

    _ftime64_s( &myTime );
    time_t rawtime = myTime.time;
    unsigned short myMillis = myTime.millitm;
    timeinfo = localtime ( &rawtime );
    _stprintf(mytime,_T("%d-%02d-%02dT%02d:%02d:%02d.%03d"),
        (1900+timeinfo->tm_year),
        (timeinfo->tm_mon+1),
        timeinfo->tm_mday,
        timeinfo->tm_hour,
        timeinfo->tm_min,
        timeinfo->tm_sec,
        myMillis);
}

I put in the Github for future reference.

  • already helped with the milliseconds, but what I really want is to print the time before and after the loop.

  • @Josinaldo You can’t ride this with the start and end?

  • would give yes if the 'program' were direct, in my case the loop is the user who enters the amount, and has menu where would be oncioso waiting for the user, well, as far as I know would not be able to calculate so, if there is an output :/

2


A simple "way" to calculate the time difference using little code

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

int main(int arc, char *argv[])
{
 clock_t t1, t2; // duas variáveis para guardar o registro clock

 t1 = clock(); // pega esse instante

 int i;

 for(i = 0;i < 500000 ;i++)
 {
      int x = 90; 
 }

 t2 = clock(); // pega esse

 // tira a diferença e divide por 1000000.0F

 float diff = (((float)t2 - (float)t1) / 1000000.0F ); // mile segundos
 printf("%f",diff);

 return 0;
 } 

That one project demonstrates the use of this calculation

  • 1

    Why float? Looks much better with double: double diff = (((double)t2 - (double)t1) / 1000000.0);

  • Important: the only correct way to know how many units of clock you have in a second is to use the macro CLOCKS_PER_SEC.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.