From: Paul Eggert Date: Sun, 1 May 2016 19:49:21 +0000 (-0700) Subject: mktime: speed up DEBUG_MKTIME benchmarks X-Git-Tag: v1.0~6752 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=e8cf92310a7f6323bc43c0143bfc3da66ef4a1c2;p=gnulib.git mktime: speed up DEBUG_MKTIME benchmarks Call tzset just once, at the start, rather than for every test case. This lets us measure the CPU cost of mktime as opposed to that of tzset. This is relevant when TZ is not set and glibc is being used. This speeds up tests by a factor of 40 on my Fedora 23 x86-64 platform. * lib/mktime.c (main) [DEBUG_MKTIME]: Call localtime at the start, to call tzset and as a sanity check. Later on, use localtime_r instead of localtime. --- diff --git a/ChangeLog b/ChangeLog index 0473a32cb3..978efec5d9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,15 @@ 2016-05-01 Paul Eggert + mktime: speed up DEBUG_MKTIME benchmarks + Call tzset just once, at the start, rather than for every test + case. This lets us measure the CPU cost of mktime as opposed to + that of tzset. This is relevant when TZ is not set and glibc is + being used. This speeds up tests by a factor of 40 on my Fedora + 23 x86-64 platform. + * lib/mktime.c (main) [DEBUG_MKTIME]: Call localtime at the start, + to call tzset and as a sanity check. Later on, use localtime_r + instead of localtime. + mktime: resurrect DEBUG_MKTIME testing * lib/mktime.c [DEBUG_MKTIME]: Do not include . Include , for strcmp. diff --git a/lib/mktime.c b/lib/mktime.c index 032ea39a94..e3658222a5 100644 --- a/lib/mktime.c +++ b/lib/mktime.c @@ -600,6 +600,14 @@ main (int argc, char **argv) time_t tk, tl, tl1; char trailer; + /* Sanity check, plus call tzset. */ + tl = 0; + if (! localtime (&tl)) + { + printf ("localtime (0) fails\n"); + status = 1; + } + if ((argc == 3 || argc == 4) && (sscanf (argv[1], "%d-%d-%d%c", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &trailer) @@ -613,12 +621,7 @@ main (int argc, char **argv) tm.tm_isdst = argc == 3 ? -1 : atoi (argv[3]); tmk = tm; tl = mktime (&tmk); - lt = localtime (&tl); - if (lt) - { - tml = *lt; - lt = &tml; - } + lt = localtime_r (&tl, &tml); printf ("mktime returns %ld == ", (long int) tl); print_tm (&tmk); printf ("\n"); @@ -633,16 +636,16 @@ main (int argc, char **argv) if (argc == 4) for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) { - lt = localtime (&tl); + lt = localtime_r (&tl, &tml); if (lt) { - tmk = tml = *lt; + tmk = tml; tk = mktime (&tmk); status |= check_result (tk, tmk, tl, &tml); } else { - printf ("localtime (%ld) yields 0\n", (long int) tl); + printf ("localtime_r (%ld) yields 0\n", (long int) tl); status = 1; } tl1 = tl + by; @@ -653,16 +656,16 @@ main (int argc, char **argv) for (tl = from; by < 0 ? to <= tl : tl <= to; tl = tl1) { /* Null benchmark. */ - lt = localtime (&tl); + lt = localtime_r (&tl, &tml); if (lt) { - tmk = tml = *lt; + tmk = tml; tk = tl; status |= check_result (tk, tmk, tl, &tml); } else { - printf ("localtime (%ld) yields 0\n", (long int) tl); + printf ("localtime_r (%ld) yields 0\n", (long int) tl); status = 1; } tl1 = tl + by;