impl::ns_time impl::current_time_in_ns(void) { ns_time ns(0); #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME) { struct timespec tp; clock_gettime(CLOCK_REALTIME, &tp); ns = sec_to_ns(tp.tv_sec) + ns_time(tp.tv_nsec); } #else { struct timeval tv; (void)gettimeofday(&tv, NULL); ns = sec_to_ns(tv.tv_sec) + usec_to_ns(tv.tv_usec); } #endif return ns; }
static void test_file_io(void) { long long start_time, end_time; FILE *fp; unsigned char buf[256]; int len, n, fd; start_time = ns_time(); len = 0; if ((fp = fopen("/redhat.logo", "r")) != (FILE *)NULL) { while (fgets(buf, sizeof(buf), fp)) { len += strlen(buf); } fclose(fp); } end_time = ns_time(); diag_printf("'fgets': %d bytes in ", len); show_ns(end_time-start_time); diag_printf("ns\n"); start_time = ns_time(); len = 0; if ((fp = fopen("/redhat.logo", "r")) != (FILE *)NULL) { while ((n = fread(buf, 1, sizeof(buf), fp))) { len += n; } fclose(fp); } end_time = ns_time(); diag_printf("'fread': %d bytes in ", len); show_ns(end_time-start_time); diag_printf("ns\n"); start_time = ns_time(); len = 0; if ((fd = open("/redhat.logo", O_RDONLY)) >= 0) { while ((n = read(fd, buf, sizeof(buf)))) { len += n; } close(fd); } end_time = ns_time(); diag_printf("'read': %d bytes in ", len); show_ns(end_time-start_time); diag_printf("ns\n"); }
time_t impl::nsec_to_sec(const ns_time &nsec) { return (nsec / ns_time(1000000000)).get(); }