Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string>
- #include <sys/timeb.h>
- const int kStrings = 1024*10;
- const int kStringLength = 128;
- const int kIterations = 1000;
- using namespace std;
- int mul_hash_code(const string& s) {
- int result = 0;
- for (int i = 0; i < s.size(); i++) {
- result = result*31 + s[i];
- }
- return result;
- }
- int shift_hash_code(const string& s) {
- int result = 0;
- for (int i = 0; i < s.size(); i++) {
- result = result ^ (result << 5 + result >> 2 + s[i]);
- }
- return result;
- }
- int diff_time(const timeb& t1, const timeb& t2) {
- return (t2.time * 1000 + t2.millitm) - (t1.time * 1000 - t1.millitm);
- }
- int main (int argc, const char * argv[]) {
- string* test_data = new string[kStrings];
- for (int i = 0; i < kStrings; i++) {
- test_data[i].reserve(kStringLength);
- for (int j = 0; j < kStringLength; j++) {
- test_data[i].push_back(rand() % 128);
- }
- }
- timeb time1;
- ftime(&time1);
- for (int i = 0; i < kIterations; i++) {
- for (int j = 0; j < kStrings; j++) {
- int h = mul_hash_code(test_data[j]);
- }
- }
- timeb time2;
- ftime(&time2);
- timeb time3;
- ftime(&time3);
- for (int i = 0; i < kIterations; i++) {
- for (int j = 0; j < kStrings; j++) {
- int h = shift_hash_code(test_data[j]);
- }
- }
- timeb time4;
- ftime(&time4);
- cout << "Mul: " << diff_time(time1, time2) << " ms\n";
- cout << "Shift: " << diff_time(time3, time4) << " ms\n";
- delete[] test_data;
- return 0;
- }
Add Comment
Please, Sign In to add comment