home *** CD-ROM | disk | FTP | other *** search
/ The Best of Mecomp Multimedia 2 / MECOMP-CD-II.iso / amiga / tools / libs / regexp / examples / regbench.c next >
Encoding:
C/C++ Source or Header  |  1998-02-19  |  1.4 KB  |  58 lines

  1. /*
  2. ** regbench.c
  3. ** quick-n-dirty example of regexp.library usage
  4. ** (L) 1998 by Matthias Bethke
  5. */
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/dos.h>
  9. #include <proto/regexp.h>
  10.  
  11. struct RegexpBase *RegexpBase;
  12.  
  13. LONG main(void)
  14. {
  15. STRPTR pat="b.*x...a+w$";    // the pattern
  16. LONG i;                            // a counter
  17. regexp *re;                        // handle for the compiled expression
  18. struct DateStamp ds[2];        // for timing
  19. LONG tix;                        // elapsed ticks
  20. LONG rcode=RETURN_OK;        // program return code
  21.  
  22. /* open regexp.library */
  23.     if(RegexpBase = (struct RegexpBase*)OpenLibrary("regexp.library",37))
  24.     {
  25.         Printf("Compiling pattern once, matching 50000 times\n");
  26. /* take current time */
  27.         DateStamp(ds);
  28. /* compile the pattern to internal format */
  29.         if(re = RegComp(pat))
  30.         {
  31.             for(i=0; i<25000; i++)
  32.             {
  33.                 RegExec(re,"blllllxabcaw");            // one pattern that matches
  34.                 RegExec(re,"bhh1234567890hhx222w");    // one that doesn't - jut 2 b fair :)
  35.             }
  36. /* free the regexp */
  37.             RegFree(re);
  38.         } else
  39.         {
  40.             Printf("Can't compile pattern \"%s\"!\n",pat);
  41.             rcode = RETURN_ERROR;
  42.         }
  43. /* timing stuff */
  44.         DateStamp(ds+1);
  45.         tix = 50*60 * (ds[1].ds_Minute - ds[0].ds_Minute);
  46.         tix += (ds[1].ds_Tick - ds[0].ds_Tick);
  47.         tix <<= 1;
  48.     
  49.         Printf("Time: %ld.%02lds (%ld ticks), %ld matches per second\n",tix/100,tix%100,tix>>1,5000000/tix);
  50.         CloseLibrary((struct Library*)RegexpBase);
  51.     } else
  52.     {
  53.         Printf("Can't open regexp.library V37+!\n");
  54.         rcode = RETURN_FAIL;
  55.     }
  56.     return rcode;
  57. }
  58.