4

What is the most efficient way of getting current time/date/day/year in C language? As I have to execute this many times, I need a real efficient way. I am on freeBSD.

thanks in advance.

hari
  • 9,439
  • 27
  • 76
  • 110
  • 2
    Have you profiled your code and found that accessing the date/time is the bottle neck? – Daniel Aug 04 '10 at 04:46
  • 1
    Yes Daniel. I found that issue while trying to get current date by calling 'date' cmd from an awk script. That piece is called very frequently and is a little costly. Just try to call "date" cmd in a shell script 10000 times in a loop and you will realize. This is the reason I am trying to write that module in C instead of awk. Thanks. – hari Aug 04 '10 at 05:03
  • If you're using GNU Awk, then you can use the [`strftime()`](http://www.math.utah.edu/docs/info/gawk_13.html#SEC128) builtin awk function, rather than shelling out to an external command. – caf Aug 04 '10 at 05:06
  • Thanks caf, I am using NAWK. I dont think strftime is available there. Would you be knowing of any other alternative in NAWK? – hari Aug 04 '10 at 15:26
  • I don't believe there is - the easiest solution may simply be to install `gawk`. – caf Aug 04 '10 at 23:51
  • Humm, thats not an option sadly. Thanks for the help though. – hari Aug 06 '10 at 07:13

8 Answers8

6
/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;

  time ( &rawtime );
  printf ( "The current local time is: %s", ctime (&rawtime) );

  return 0;
}

You can use ctime, if you need it as a string.

Alex Marcotte
  • 475
  • 3
  • 6
5

Standard C provides only one way to get the time - time() - which can be converted to a time/date/year with localtime() or gmtime(). So trivially, that must be the most efficient way.

Any other methods are operating-system specific, and you haven't told us what operating system you're using.

caf
  • 233,326
  • 40
  • 323
  • 462
  • Thanks much. I am on freeBSD. – hari Aug 04 '10 at 05:29
  • 2
    If you're worried about `gmtime` or `localtime` being slow, you could simply call them once and then adjust the results based on a delta value given in seconds obtained from `time`. This is portable as far as POSIX is concerned, but not plain C, as plain C does not specify the format of the `time_t` type except that it's an arithmetic type. – R.. GitHub STOP HELPING ICE Aug 04 '10 at 12:05
3

It really depends on what you mean by "many" :-)

I think you'll probably find that using the ISO standard time() and localtime() functions will be more than fast enough. For example, on my "Intel(R) Core(TM)2 Duo CPU E6850 @ 3.00GHz", using unoptimised code, I can call time() ten million times in 1.045 seconds, and a time()/localtime() combination half a million times in 0.98 seconds. Whether that's fast enough for your needs, only you can decide, but I'm hard-pressed trying to come up with a use case that needs more grunt than that.

The time() function gives you the number of seconds since the epoch, while localtime() both converts it to local time (from UTC) and splits it into a more usable form, the struct tm structure.

#include <time.h>
time_t t = time (NULL);
struct tm* lt = localtime (&t);
// Use lt->tm_year, lt->tm_mday, and so forth.

Any attempt to cache the date/time and use other ways of finding out a delta to apply to it, such as with clock(), will almost invariably:

  • be slower; and
  • suffer from the fact you won't pick up external time changes.
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Of course if many means 100s of times/second - then you could just cache the response and check if time() has changed every 100calls – Martin Beckett Aug 04 '10 at 04:54
  • @Martin, that's fine right up until the point where his process is switched out for a second because of massive system loads :-) – paxdiablo Aug 04 '10 at 04:55
  • 1
    @Martin: the only problem with that is that unless you're *sure* it's going to be called many times a second, this could easily end up returning stale data. – Jerry Coffin Aug 04 '10 at 04:57
  • time() only has a resolution of a second so it's unlikey that a process running 100Hz would care if 10% of the values were tagged with the previous second – Martin Beckett Aug 04 '10 at 13:23
1

The simplest is

#include <time.h>
//...
time_t current_time = time (NULL);
struct tm* local_time = localtime (&current_time); 
printf ("the time is %s\n", asctime (local_time));
Akusete
  • 10,704
  • 7
  • 57
  • 73
1

You can use gettimeofday() function to get time in seconds & microseconds which is (I think) very fast (as there is a similar function in Linux kernel do_gettimeofday()) and then you can convert it to your required format (might possible to use functions mentioned above for conversion.

I hope this helps.

0

Assuming a one second resolution is enough, the most efficient way on FreeBSD (or any POSIX system) is likely

  • Install a one second interval timer with setitimer (ITIMER_REAL, ...)
  • When it triggers the SIGALRM, update a static variable holding the currrent time
  • Use the value in the static variable whenever you need the time

Even if signals get lost due to system overload this will correct itself the next time the process is scheduled.

Jens
  • 69,818
  • 15
  • 125
  • 179
0

Just about the only way (that's standard, anyway) is to call time followed by localtime or gmtime.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I don't see what's wrong with the most straightforward response. The fact he's already working in C is of great benefit, the only hit here is the syscall for time? – Matt Joiner Aug 04 '10 at 04:49
0

well, in general, directly accessing the OS's API to get the time is probably the most efficient, but not so portable.....

the C time functions are ok.

But really depends on your platform

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156