| 1 | #include <string> |
| 2 | #include <vector> |
| 3 | #include <unordered_set> |
| 4 | #include <cstdlib> |
| 5 | #include <random> |
| 6 | #include <testsuite_performance.h> |
| 7 | |
| 8 | using namespace std; |
| 9 | |
| 10 | vector<string> |
| 11 | random_strings(int n, int len) |
| 12 | { |
| 13 | string s(len, '\0'); |
| 14 | unordered_set<string> result_set; |
| 15 | random_device rd; |
| 16 | while (result_set.size() < n) |
| 17 | { |
| 18 | result_set.insert(s); |
| 19 | unsigned int tmp = rd(); |
| 20 | tmp %= len * 256; |
| 21 | s[tmp / 256] = tmp % 256; |
| 22 | } |
| 23 | return vector<string>(result_set.begin(), result_set.end()); |
| 24 | } |
| 25 | |
| 26 | int |
| 27 | main(int argc, char **argv) |
| 28 | { |
| 29 | using namespace __gnu_test; |
| 30 | time_counter time; |
| 31 | resource_counter resource; |
| 32 | |
| 33 | int string_size = 71; |
| 34 | int num_strings = 6000000; |
| 35 | if (argc > 1) |
| 36 | { |
| 37 | string_size = atoi(argv[1]); |
| 38 | if (argc > 2) |
| 39 | num_strings = atoi(argv[2]); |
| 40 | } |
| 41 | |
| 42 | // Construct random strings. |
| 43 | vector<string> v = random_strings(num_strings, string_size); |
| 44 | |
| 45 | // Time hashing. |
| 46 | size_t tmp = 0; // prevent compiler from optimizing away all the work |
| 47 | start_counters(time, resource); |
| 48 | for (int i = 0; i < num_strings; i++) |
| 49 | tmp += hash<string>()(v[i]); |
| 50 | stop_counters(time, resource); |
| 51 | |
| 52 | if (tmp != 0 || argc < 9) // use tmp to prevent compiler optimization |
| 53 | report_performance(__FILE__, "", time, resource); |
| 54 | |
| 55 | clear_counters(time, resource); |
| 56 | |
| 57 | return 0; |
| 58 | } |