散列函数效率对比

😊💕😁👍🙌🤦‍♀️🤦‍♂️🤷‍♀️🤷‍♂️😜💖😢🎶😎🤞✌👏💋🌹🎉🎂🤳🐱‍👤🐱‍🏍✨😃👀🐱‍🚀🐱‍🚀🐱‍🐉

😊💕😁👍🙌🤦‍♀️🤦‍♂️🤷‍♀️🤷‍♂️😜💖😢🎶😎🤞✌👏💋🌹🎉🎂🤳🐱‍👤🐱‍🏍✨😃👀🐱‍🚀🐱‍🚀🐱‍🐉
#include <iostream>
#include <string>
#include <chrono>
#include <cstring>
#include <openssl/md5.h>
class TimeGuard{
public:
    std::string name;
    std::chrono::high_resolution_clock::time_point start;
    TimeGuard(std::string _name){
        name = _name;
        start = std::chrono::high_resolution_clock::now();
    }
    ~TimeGuard(){
        std::cout <<name<<" cost time : “<<std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::high_resolution_clock::now() - start).count()<<” ms"<< std::endl;
     }
};
uint64_t BKDRHash(const char *p, size_t len, uint64_t seed){
    for(size_t i = 0; i < len; ++i){
        const char ch = p[i];
        seed = seed * 131 + ch;
    }
    return seed;
}
long MD5Hash(const char *p,size_t len){
    long hashValue = 0;
    unsigned char x[16];
    bzero(x,sizeof(x));
    MD5_CTX md5;
    MD5_Init(&md5);
    MD5_Update(&md5,p,len);
    MD5_Final(x,&md5);
    for(size_t i = 0; i < 4; ++i){
        hashValue += ((long)(x[i*4+3]&0xff) << 24) |
                     ((long)(x[i*4+2]&0xff) << 16) |
                     ((long)(x[i*4+1]&0xff) << 8) |
                     ((long)(x[i*4+0]&0xff) << 0);
    }
    return hashValue;
}
int main(){
    const char * p1 = “sadafa s432535333bbbbbbbbbb4b54235b2aasfa df321”;
    const char * p2 = “WWWWfa323567853453546356346346 asfa df32gs1”;
    const char * p3 = “sa346346346436czczc2342345344655…dafa saasfa df321”;
    int nRepeat = 100000;
    {
        uint64_t seed = 123124132;
        TimeGuard _(“BKDRHash”);
        for(int i = 0; i < nRepeat; ++i){
            auto v = BKDRHash(p1,strlen(p1),seed);
            auto v1 = BKDRHash(p2,strlen(p2),seed);
            auto v2 = BKDRHash(p3,strlen(p3),seed);
        }
    }
    {
        TimeGuard _(“MD5Hash”);
        for(int i = 0; i < nRepeat; ++i){
            auto v = MD5Hash(p1,strlen(p1));
            auto v1 = MD5Hash(p2,strlen(p2));
            auto v2 = MD5Hash(p3,strlen(p3));
        }
    }
}

Algorithm,HASH

在这里插入图片描述

验证大佬的结论,发现 MD5 散列性能不是很差:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值