home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / des / speed.c < prev   
Encoding:
C/C++ Source or Header  |  1994-09-15  |  2.9 KB  |  157 lines

  1. /* speed.c */
  2. /* Copyright (C) 1993 Eric Young - see README for more details */
  3. /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
  4. /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
  5.  
  6.  
  7. typedef unsigned char des_cblock[8];
  8. typedef struct des_ks_struct
  9.     {
  10.     union    {
  11.         des_cblock _;
  12.         /* make sure things are correct size on machines with
  13.          * 8 byte longs */
  14.         unsigned long pad[2];
  15.         } ks;
  16. /* #define _    ks._ */
  17.     } des_key_schedule[16];
  18.  
  19. #define TIMES
  20.  
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #ifndef VMS
  24. #ifndef _IRIX
  25. #include <time.h>
  26. #endif
  27. #ifdef TIMES
  28. #include <sys/types.h>
  29. #include <sys/times.h>
  30. #endif /* TIMES */
  31. #else /* VMS */
  32. #include <types.h>
  33. struct tms {
  34.     time_t tms_utime;
  35.     time_t tms_stime;
  36.     time_t tms_uchild;    /* I dunno...  */
  37.     time_t tms_uchildsys;    /* so these names are a guess :-) */
  38.     }
  39. #endif
  40. #ifndef TIMES
  41. #include <sys/timeb.h>
  42. #endif
  43.  
  44. #include "des_crypt.h"
  45.  
  46. /* The following if from times(3) man page.  It may need to be changed */
  47. #ifndef CLK_TCK
  48. #ifndef VMS
  49. #define HZ    60.0
  50. #else /* VMS */
  51. #define HZ    100.0
  52. #endif
  53. #else /* CLK_TCK */
  54. #define HZ ((double)CLK_TCK)
  55. #endif
  56.  
  57. #define BUFSIZE    ((long)1024*8)
  58. long run=0;
  59.  
  60. #ifdef SIGALRM
  61. #ifdef __STDC__
  62. #define SIGRETTYPE void
  63. #else
  64. #define SIGRETTYPE int
  65. #endif 
  66.  
  67. SIGRETTYPE sig_done(sig)
  68. int sig;
  69.     {
  70.     signal(SIGALRM,sig_done);
  71.     run=0;
  72.     }
  73. #endif
  74.  
  75. #define START    0
  76. #define STOP    1
  77.  
  78. double Time_F(s)
  79. int s;
  80.     {
  81.     double ret;
  82. #ifdef TIMES
  83.     static struct tms tstart,tend;
  84.  
  85.     if (s == START)
  86.         {
  87.         times(&tstart);
  88.         return(0);
  89.         }
  90.     else
  91.         {
  92.         times(&tend);
  93.         ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  94.         return((ret == 0.0)?1e-6:ret);
  95.         }
  96. #else /* !times() */
  97.     static struct timeb tstart,tend;
  98.     long i;
  99.  
  100.     if (s == START)
  101.         {
  102.         ftime(&tstart);
  103.         return(0);
  104.         }
  105.     else
  106.         {
  107.         ftime(&tend);
  108.         i=(long)tend.millitm-(long)tstart.millitm;
  109.         ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  110.         return((ret == 0.0)?1e-6:ret);
  111.         }
  112. #endif
  113.     }
  114.  
  115. #define COND(c)    (run)
  116. #define COUNT(d) (count)
  117.  
  118. main(argc,argv)
  119. int argc;
  120. char *argv[];
  121.     {
  122.     long count;
  123.     static unsigned char buf[BUFSIZE];
  124.     static des_cblock key={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
  125.     des_key_schedule sch;
  126.     double d,a,b,c;
  127.     long ca,cb,cc,cd;
  128.  
  129.     signal(SIGALRM,sig_done);
  130.  
  131.     printf("Doing ecb_crypt on %ld byte blocks for 10 seconds\n",
  132.         BUFSIZE);
  133.     alarm(10);
  134.  
  135.     Time_F(START);
  136.     for (count=0,run=1; COND(cb); count++)
  137.         ecb_crypt(key, buf, BUFSIZE, DES_ENCRYPT);
  138.     d=Time_F(STOP);
  139.     printf("%ld ecb_crypt's in %.2f second\n",count,d);
  140.     b=((double)COUNT(cb)*BUFSIZE)/d;
  141.  
  142.     printf("Doing cbc_crypt on %ld byte blocks for 10 seconds\n",
  143.         BUFSIZE);
  144.     alarm(10);
  145.  
  146.     Time_F(START);
  147.     for (count=0,run=1; COND(cc); count++)
  148.         cbc_crypt(key,buf,BUFSIZE,DES_ENCRYPT,buf);
  149.     d=Time_F(STOP);
  150.     printf("%ld cbc_crypt's of %ld byte blocks in %.2f second\n",
  151.         count,BUFSIZE,d);
  152.     c=((double)COUNT(cc)*BUFSIZE)/d;
  153.  
  154.     printf("DES ecb bytes per sec = %12.2f (%5.1fuS)\n",b,8.0e6/b);
  155.     printf("DES cbc bytes per sec = %12.2f (%5.1fuS)\n",c,8.0e6/c);
  156. }
  157.