root/maint/gnulib/tests/bench-digest.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. timing_start
  2. timing_end
  3. timing_output
  4. main

   1 /*
   2  * Copyright (C) 2018-2021 Free Software Foundation, Inc.
   3  *
   4  * This program is free software: you can redistribute it and/or modify
   5  * it under the terms of the GNU General Public License as published by
   6  * the Free Software Foundation; either version 3 of the License, or
   7  * (at your option) any later version.
   8  *
   9  * This program is distributed in the hope that it will be useful,
  10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  * GNU General Public License for more details.
  13  *
  14  * You should have received a copy of the GNU General Public License
  15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 #include <stdio.h>
  18 #include <stdlib.h>
  19 #include <sys/resource.h>
  20 #include <sys/time.h>
  21 
  22 struct timings_state
  23 {
  24   /* Filled when the timings start.  */
  25   struct timeval real_start;
  26   struct timeval user_start;
  27   struct timeval sys_start;
  28   /* Filled when the timings end.  */
  29   long real_usec;
  30   long user_usec;
  31   long sys_usec;
  32 };
  33 
  34 static void
  35 timing_start (struct timings_state *ts)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37   struct rusage usage;
  38 
  39   getrusage (RUSAGE_SELF, &usage);
  40   ts->user_start = usage.ru_utime;
  41   ts->sys_start = usage.ru_stime;
  42 
  43   gettimeofday (&ts->real_start, NULL);
  44 }
  45 
  46 static void
  47 timing_end (struct timings_state *ts)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49   struct timeval real_end;
  50   struct rusage usage;
  51 
  52   gettimeofday (&real_end, NULL);
  53 
  54   getrusage (RUSAGE_SELF, &usage);
  55 
  56   ts->real_usec = (real_end.tv_sec - ts->real_start.tv_sec) * 1000000
  57                   + real_end.tv_usec - ts->real_start.tv_usec;
  58   ts->user_usec = (usage.ru_utime.tv_sec - ts->user_start.tv_sec) * 1000000
  59                   + usage.ru_utime.tv_usec - ts->user_start.tv_usec;
  60   ts->sys_usec = (usage.ru_stime.tv_sec - ts->sys_start.tv_sec) * 1000000
  61                  + usage.ru_stime.tv_usec - ts->sys_start.tv_usec;
  62 }
  63 
  64 static void
  65 timing_output (const struct timings_state *ts)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67   printf ("real %10.6f\n", (double)ts->real_usec / 1000000.0);
  68   printf ("user %7.3f\n", (double)ts->user_usec / 1000000.0);
  69   printf ("sys  %7.3f\n", (double)ts->sys_usec / 1000000.0);
  70 }
  71 
  72 int
  73 main (int argc, char *argv[])
     /* [previous][next][first][last][top][bottom][index][help] */
  74 {
  75   if (argc != 3)
  76     {
  77       fprintf (stderr, "Usage: %s SIZE REPETITIONS\n", argv[0]);
  78       exit (1);
  79     }
  80 
  81   size_t size = atol (argv[1]);
  82   int repeat = atoi (argv[2]);
  83 
  84   char *memblock = (char *) malloc (size);
  85   if (!memblock)
  86     {
  87       fprintf (stderr, "%s: memory exhausted\n", argv[0]);
  88       return 1;
  89     }
  90 
  91   /* Fill the memory block.  */
  92   {
  93     size_t i;
  94     for (i = 0; i < size; i++)
  95       memblock[i] =
  96         (unsigned char) (((i * (i-1) * (i-5)) >> 6) + (i % 499) + (i % 101));
  97   }
  98 
  99   struct timings_state ts;
 100   timing_start (&ts);
 101 
 102   int count;
 103   for (count = 0; count < repeat; count++)
 104     {
 105       char digest[64];
 106       FUNC (memblock, size, digest);
 107     }
 108 
 109   timing_end (&ts);
 110   timing_output (&ts);
 111 
 112   return 0;
 113 }

/* [previous][next][first][last][top][bottom][index][help] */