Guest User

Untitled

a guest
Apr 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <sys/timeb.h>
  5.  
  6. const int kStrings = 1024*10;
  7. const int kStringLength = 128;
  8. const int kIterations = 1000;
  9.  
  10. using namespace std;
  11.  
  12. int mul_hash_code(const string& s) {
  13. int result = 0;
  14. for (int i = 0; i < s.size(); i++) {
  15. result = result*31 + s[i];
  16. }
  17.  
  18. return result;
  19. }
  20.  
  21. int shift_hash_code(const string& s) {
  22. int result = 0;
  23. for (int i = 0; i < s.size(); i++) {
  24. result = result ^ (result << 5 + result >> 2 + s[i]);
  25. }
  26.  
  27. return result;
  28. }
  29.  
  30. int diff_time(const timeb& t1, const timeb& t2) {
  31. return (t2.time * 1000 + t2.millitm) - (t1.time * 1000 - t1.millitm);
  32. }
  33.  
  34. int main (int argc, const char * argv[]) {
  35. string* test_data = new string[kStrings];
  36.  
  37. for (int i = 0; i < kStrings; i++) {
  38. test_data[i].reserve(kStringLength);
  39. for (int j = 0; j < kStringLength; j++) {
  40. test_data[i].push_back(rand() % 128);
  41. }
  42. }
  43.  
  44. timeb time1;
  45. ftime(&time1);
  46.  
  47. for (int i = 0; i < kIterations; i++) {
  48. for (int j = 0; j < kStrings; j++) {
  49. int h = mul_hash_code(test_data[j]);
  50. }
  51. }
  52.  
  53. timeb time2;
  54. ftime(&time2);
  55.  
  56. timeb time3;
  57. ftime(&time3);
  58.  
  59. for (int i = 0; i < kIterations; i++) {
  60. for (int j = 0; j < kStrings; j++) {
  61. int h = shift_hash_code(test_data[j]);
  62. }
  63. }
  64.  
  65. timeb time4;
  66. ftime(&time4);
  67.  
  68. cout << "Mul: " << diff_time(time1, time2) << " ms\n";
  69. cout << "Shift: " << diff_time(time3, time4) << " ms\n";
  70.  
  71. delete[] test_data;
  72. return 0;
  73. }
Add Comment
Please, Sign In to add comment