changeset 18306:6998697a3e42

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.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 01 May 2016 12:49:21 -0700
parents ebba8469e29d
children 534fd793480e
files ChangeLog lib/mktime.c
diffstat 2 files changed, 25 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun May 01 12:15:52 2016 -0700
+++ b/ChangeLog	Sun May 01 12:49:21 2016 -0700
@@ -1,5 +1,15 @@
 2016-05-01  Paul Eggert  <eggert@cs.ucla.edu>
 
+	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.
--- a/lib/mktime.c	Sun May 01 12:15:52 2016 -0700
+++ b/lib/mktime.c	Sun May 01 12:49:21 2016 -0700
@@ -600,6 +600,14 @@
   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 @@
       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 @@
       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 @@
 	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;