Fastest hash for non-cryptographic uses?

后端 未结 13 1091
挽巷
挽巷 2020-12-04 08:23

I\'m essentially preparing phrases to be put into the database, they may be malformed so I want to store a short hash of them instead (I will be simply comparing if they exi

13条回答
  •  -上瘾入骨i
    2020-12-04 08:45

    fcn     time  generated hash
    crc32:  0.03163  798740135
    md5:    0.0731   0dbab6d0c841278d33be207f14eeab8b
    sha1:   0.07331  417a9e5c9ac7c52e32727cfd25da99eca9339a80
    xor:    0.65218  119
    xor2:   0.29301  134217728
    add:    0.57841  1105
    

    And the code used to generate this is:

     $loops = 100000;
     $str = "ana are mere";
    
     echo "
    ";
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $x = crc32($str);
     }
     $tse = microtime(true);
     echo "\ncrc32: \t" . round($tse-$tss, 5) . " \t" . $x;
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $x = md5($str);
     }
     $tse = microtime(true);
     echo "\nmd5: \t".round($tse-$tss, 5) . " \t" . $x;
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $x = sha1($str);
     }
     $tse = microtime(true);
     echo "\nsha1: \t".round($tse-$tss, 5) . " \t" . $x;
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $l = strlen($str);
      $x = 0x77;
      for($j=0;$j<$l;$j++){
       $x = $x xor ord($str[$j]);
      }
     }
     $tse = microtime(true);
     echo "\nxor: \t".round($tse-$tss, 5) . " \t" . $x;
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $l = strlen($str);
      $x = 0x08;
      for($j=0;$j<$l;$j++){
       $x = ($x<<2) xor $str[$j];
      }
     }
     $tse = microtime(true);
     echo "\nxor2: \t".round($tse-$tss, 5) . " \t" . $x;
    
     $tss = microtime(true);
     for($i=0; $i<$loops; $i++){
      $l = strlen($str);
      $x = 0;
      for($j=0;$j<$l;$j++){
       $x = $x + ord($str[$j]);
      }
     }
     $tse = microtime(true);
     echo "\nadd: \t".round($tse-$tss, 5) . " \t" . $x;
    

提交回复
热议问题