Guest User

Untitled

a guest
Oct 25th, 2011
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <Windows.h>
  3. typedef unsigned __int64 uint64_t;
  4.  
  5. const unsigned char largeText[] = {
  6. #include "big.h"
  7. };
  8.  
  9. int Test1(const unsigned char *text, size_t size)
  10. {
  11.     int inWhitelist = 0;
  12.     for (size_t i = 0; i < size; ++i)
  13.     {
  14.         const char c = text[i];
  15.         if ((c >= 'a' && c <= 'z')
  16.                 || (c >= 'A' && c <= 'Z')
  17.                 || (c >= '0' && c <= '9'))
  18.         {
  19.             ++inWhitelist;
  20.         }
  21.     }
  22.  
  23.     return inWhitelist;
  24. }
  25.  
  26.  
  27. int Test2(const unsigned char *text, size_t size)
  28. {
  29.     int inWhitelist = 0;
  30.     static const char byteTable[] = {
  31.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  35.         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  36.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  37.         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  39.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  40.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  41.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  42.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  43.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  44.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47.     };
  48.     for (size_t i = 0; i < size; ++i)
  49.     {
  50.         const unsigned char c = text[i];
  51.         if (byteTable[c])
  52.         {
  53.             ++inWhitelist;
  54.         }
  55.     }
  56.  
  57.     return inWhitelist;
  58.  
  59. }
  60.  
  61. int Test3(const unsigned char *text, size_t size)
  62. {
  63.     int inWhitelist = 0;
  64.     static const int intTable[] = {
  65.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  67.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  68.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
  69.         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  70.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  71.         0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  72.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
  73.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  78.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81.     };
  82.     for (size_t i = 0; i < size; ++i)
  83.     {
  84.         const unsigned char c = text[i];
  85.         if (intTable[c])
  86.         {
  87.             ++inWhitelist;
  88.         }
  89.     }
  90.  
  91.     return inWhitelist;
  92. }
  93.  
  94. int Test4(const unsigned char *text, size_t size)
  95. {
  96.     int inWhitelist = 0;
  97.     const unsigned bitVectors[] = {
  98.         0x00000000,
  99.         0x03ff0000,
  100.         0x07fffffe,
  101.         0x07fffffe,
  102.         0x00000000,
  103.         0x00000000,
  104.         0x00000000,
  105.         0x00000000,
  106.     };
  107.     for (size_t i = 0; i < size; ++i)
  108.     {
  109.         const unsigned c = text[i];
  110.         const unsigned bit = c & 31;
  111.         const unsigned bitVector = c >> 5;
  112.         if (bitVectors[bitVector] & (1 << bit))
  113.         {
  114.             ++inWhitelist;
  115.         }
  116.     }
  117.  
  118.     return inWhitelist;
  119. }
  120.  
  121. int main(void)
  122. {
  123.     typedef int (*TestFn)(const unsigned char *, size_t);
  124.  
  125.     TestFn testFunctions[] = {
  126.         Test1,
  127.         Test2,
  128.         Test3,
  129.         Test4
  130.     };
  131.  
  132.     const unsigned char *text = largeText;
  133.     const size_t size = sizeof largeText;
  134.  
  135.     uint64_t frequency = 0;
  136.     QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
  137.  
  138.     for (size_t test = 0; test < sizeof testFunctions / sizeof testFunctions[0]; ++test)
  139.     {
  140.         uint64_t elapsed = 0;
  141.         const int numIterations = 100;
  142.         for (int iteration = 0; iteration < numIterations; ++iteration)
  143.         {
  144.             uint64_t start = 0;
  145.             uint64_t end = 0;
  146.             QueryPerformanceCounter((LARGE_INTEGER *)&start);
  147.                 testFunctions[test](text, size);
  148.             QueryPerformanceCounter((LARGE_INTEGER *)&end);
  149.             elapsed += (end - start);
  150.  
  151.             if (((iteration + 1) % 10) == 0)
  152.                 printf("  %d%%\n", iteration + 1);
  153.         }
  154.  
  155.         printf("\nTest %d average milliseconds: %lf\n", test, ((double)elapsed / (double)numIterations) / ((double)frequency / 1000.0));
  156.     }
  157. }
  158.  
  159.  
Advertisement
Add Comment
Please, Sign In to add comment