Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <vector>
- #include <string>
- #define NOMINMAX
- #include <windows.h>
- double elapsed_ms(LARGE_INTEGER start, LARGE_INTEGER end) {
- LARGE_INTEGER frequency;
- QueryPerformanceFrequency(&frequency);
- return (end.QuadPart - start.QuadPart) * 1000 / (double) frequency.QuadPart;
- }
- int test(char* a, char* b) {
- return *a != *b;
- }
- void compare_cstrings(int count, int length) {
- std::vector<char*> strings1 = std::vector<char*>(count);
- std::vector<char*> strings2 = std::vector<char*>(count);
- std::string allowed_chars = std::string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
- for (int str_id = 0; str_id < count; str_id++) {
- strings1[str_id] = new char[length + 1];
- strings2[str_id] = new char[length + 1];
- for (int char_id = 0; char_id < length; char_id++) {
- strings1[str_id][char_id] = allowed_chars[std::rand() % allowed_chars.length()];
- strings2[str_id][char_id] = allowed_chars[std::rand() % allowed_chars.length()];
- }
- strings1[str_id][length] = strings2[str_id][length] = '\0';
- }
- int matches = 0;
- LARGE_INTEGER start, end;
- QueryPerformanceCounter(&start);
- for (int str_id = 0; str_id < count; str_id++)
- if (test(strings1[str_id], strings2[str_id]) == 0)
- matches++;
- QueryPerformanceCounter(&end);
- for (int str_id = 0; str_id < count; str_id++) {
- delete[] strings1[str_id];
- delete[] strings2[str_id];
- }
- printf("%d strings\t%d chars\t%fms\t(%d matches)\n", count, length, elapsed_ms(start, end), matches);
- }
- int main(int argc, char** argv) {
- srand(0);
- for (int length = 1; length <= 250; length++)
- compare_cstrings(250*1000, length);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement