bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 4/5] mktime: speed up DEBUG_MKTIME benchmarks


From: Paul Eggert
Subject: [PATCH 4/5] mktime: speed up DEBUG_MKTIME benchmarks
Date: Sun, 1 May 2016 17:27:27 -0700

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.
---
 ChangeLog    | 10 ++++++++++
 lib/mktime.c | 27 +++++++++++++++------------
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 0473a32..978efec 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2016-05-01  Paul Eggert  <address@hidden>
 
+       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 <config.h>.
        Include <string.h>, for strcmp.
diff --git a/lib/mktime.c b/lib/mktime.c
index 032ea39..e365822 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;
-- 
2.5.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]