Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <Windows.h>
- typedef unsigned __int64 uint64_t;
- const unsigned char largeText[] = {
- #include "big.h"
- };
- int Test1(const unsigned char *text, size_t size)
- {
- int inWhitelist = 0;
- for (size_t i = 0; i < size; ++i)
- {
- const char c = text[i];
- if ((c >= 'a' && c <= 'z')
- || (c >= 'A' && c <= 'Z')
- || (c >= '0' && c <= '9'))
- {
- ++inWhitelist;
- }
- }
- return inWhitelist;
- }
- int Test2(const unsigned char *text, size_t size)
- {
- int inWhitelist = 0;
- static const char byteTable[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- };
- for (size_t i = 0; i < size; ++i)
- {
- const unsigned char c = text[i];
- if (byteTable[c])
- {
- ++inWhitelist;
- }
- }
- return inWhitelist;
- }
- int Test3(const unsigned char *text, size_t size)
- {
- int inWhitelist = 0;
- static const int intTable[] = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- };
- for (size_t i = 0; i < size; ++i)
- {
- const unsigned char c = text[i];
- if (intTable[c])
- {
- ++inWhitelist;
- }
- }
- return inWhitelist;
- }
- int Test4(const unsigned char *text, size_t size)
- {
- int inWhitelist = 0;
- const unsigned bitVectors[] = {
- 0x00000000,
- 0x03ff0000,
- 0x07fffffe,
- 0x07fffffe,
- 0x00000000,
- 0x00000000,
- 0x00000000,
- 0x00000000,
- };
- for (size_t i = 0; i < size; ++i)
- {
- const unsigned c = text[i];
- const unsigned bit = c & 31;
- const unsigned bitVector = c >> 5;
- if (bitVectors[bitVector] & (1 << bit))
- {
- ++inWhitelist;
- }
- }
- return inWhitelist;
- }
- int main(void)
- {
- typedef int (*TestFn)(const unsigned char *, size_t);
- TestFn testFunctions[] = {
- Test1,
- Test2,
- Test3,
- Test4
- };
- const unsigned char *text = largeText;
- const size_t size = sizeof largeText;
- uint64_t frequency = 0;
- QueryPerformanceFrequency((LARGE_INTEGER *)&frequency);
- for (size_t test = 0; test < sizeof testFunctions / sizeof testFunctions[0]; ++test)
- {
- uint64_t elapsed = 0;
- const int numIterations = 100;
- for (int iteration = 0; iteration < numIterations; ++iteration)
- {
- uint64_t start = 0;
- uint64_t end = 0;
- QueryPerformanceCounter((LARGE_INTEGER *)&start);
- testFunctions[test](text, size);
- QueryPerformanceCounter((LARGE_INTEGER *)&end);
- elapsed += (end - start);
- if (((iteration + 1) % 10) == 0)
- printf(" %d%%\n", iteration + 1);
- }
- printf("\nTest %d average milliseconds: %lf\n", test, ((double)elapsed / (double)numIterations) / ((double)frequency / 1000.0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment