Advertisement
Guest User

Untitled

a guest
Sep 29th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <string>
  5. #define NOMINMAX
  6. #include <windows.h>
  7.  
  8. double elapsed_ms(LARGE_INTEGER start, LARGE_INTEGER end) {
  9.     LARGE_INTEGER frequency;
  10.     QueryPerformanceFrequency(&frequency);
  11.     return (end.QuadPart - start.QuadPart) * 1000 / (double) frequency.QuadPart;
  12. }
  13.  
  14. int test(char* a, char* b) {
  15.     return *a != *b;
  16. }
  17.  
  18. void compare_cstrings(int count, int length) {
  19.     std::vector<char*> strings1 = std::vector<char*>(count);
  20.     std::vector<char*> strings2 = std::vector<char*>(count);
  21.     std::string allowed_chars = std::string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  22.  
  23.     for (int str_id = 0; str_id < count; str_id++) {
  24.         strings1[str_id] = new char[length + 1];
  25.         strings2[str_id] = new char[length + 1];
  26.  
  27.         for (int char_id = 0; char_id < length; char_id++) {
  28.             strings1[str_id][char_id] = allowed_chars[std::rand() % allowed_chars.length()];
  29.             strings2[str_id][char_id] = allowed_chars[std::rand() % allowed_chars.length()];
  30.         }
  31.  
  32.         strings1[str_id][length] = strings2[str_id][length] = '\0';
  33.     }
  34.  
  35.     int matches = 0;
  36.     LARGE_INTEGER start, end;
  37.     QueryPerformanceCounter(&start);
  38.     for (int str_id = 0; str_id < count; str_id++)
  39.         if (test(strings1[str_id], strings2[str_id]) == 0)
  40.             matches++;
  41.     QueryPerformanceCounter(&end);
  42.  
  43.     for (int str_id = 0; str_id < count; str_id++) {
  44.         delete[] strings1[str_id];
  45.         delete[] strings2[str_id];
  46.     }
  47.  
  48.     printf("%d strings\t%d chars\t%fms\t(%d matches)\n", count, length, elapsed_ms(start, end), matches);
  49. }
  50.  
  51. int main(int argc, char** argv) {
  52.     srand(0);
  53.     for (int length = 1; length <= 250; length++)
  54.         compare_cstrings(250*1000, length);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement