这两个函数的性能差异是20倍,为啥?

std::size_t kmpFindNew_int32(const char* lch, int32_t lchCount, const char* rch, int32_t rchCount)
{
    std::vector<int32_t> next(rchCount + 1, 0);
    int32_t i = 0;
    int32_t j = -1;
    next[0] = -1;
    while (i < rchCount) {
        if (j == -1 || rch[i] == rch[j]) {
            i++;
            j++;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
    i = 0;
    j = 0;
    while (i < lchCount && j < rchCount) {
        if (j == -1 || lch[i] == rch[j]) {
            i++;
            j++;
        } else {
            j = next[j];
        }
    }
    if (j == rchCount) {
        return i - rchCount;
    }
    return lchCount;
}

std::size_t kmpFindNew_uint32(const char* lch, uint32_t lchCount, const char* rch, uint32_t rchCount)
{
    std::vector<int32_t> next(rchCount + 1, 0);
    int32_t i = 0;
    int32_t j = -1;
    next[0] = -1;
    while (i < rchCount) {
        if (j == -1 || rch[i] == rch[j]) {
            i++;
            j++;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
    i = 0;
    j = 0;
    while (i < lchCount && j < rchCount) {
        if (j == -1 || lch[i] == rch[j]) {
            i++;
            j++;
        } else {
            j = next[j];
        }
    }
    if (j == rchCount) {
        return i - rchCount;
    }
    return lchCount;
}

BM_kmpFindNew_int32_FoundOk/1/iterations:100000                   8887 ns         8886 ns       100000
BM_kmpFindNew_int32_FoundOk/10/iterations:100000                  8671 ns         8671 ns       100000
BM_kmpFindNew_int32_FoundOk/100/iterations:100000                 9799 ns         9799 ns       100000
BM_kmpFindNew_int32_FoundOk/1000/iterations:100000               10013 ns        10013 ns       100000
BM_kmpFindNew_int32_FoundOk/10000/iterations:100000               8833 ns         8833 ns       100000
BM_kmpFindNew_int32_FoundOk/100000/iterations:100000              9069 ns         9069 ns       100000
BM_kmpFindNew_int32_FoundOk/1000000/iterations:100000             8722 ns         8722 ns       100000
BM_kmpFindNew_int32_FoundOk/10000000/iterations:100000            9557 ns         9557 ns       100000
BM_kmpFindNew_uint32_FoundOk/1/iterations:100000                   282 ns          282 ns       100000
BM_kmpFindNew_uint32_FoundOk/10/iterations:100000                  255 ns          255 ns       100000
BM_kmpFindNew_uint32_FoundOk/100/iterations:100000                 251 ns          251 ns       100000
BM_kmpFindNew_uint32_FoundOk/1000/iterations:100000                273 ns          273 ns       100000
BM_kmpFindNew_uint32_FoundOk/10000/iterations:100000               402 ns          402 ns       100000
BM_kmpFindNew_uint32_FoundOk/100000/iterations:100000              249 ns          249 ns       100000
BM_kmpFindNew_uint32_FoundOk/1000000/iterations:100000             251 ns          251 ns       100000
BM_kmpFindNew_uint32_FoundOk/10000000/iterations:100000            251 ns          251 ns       100000
BM_kmpFindNew_int32_FoundFailed/1/iterations:100000               7885 ns         7885 ns       100000
BM_kmpFindNew_int32_FoundFailed/10/iterations:100000              8142 ns         8142 ns       100000
BM_kmpFindNew_int32_FoundFailed/100/iterations:100000             8603 ns         8602 ns       100000
BM_kmpFindNew_int32_FoundFailed/1000/iterations:100000            8745 ns         8744 ns       100000
BM_kmpFindNew_int32_FoundFailed/10000/iterations:100000           8096 ns         8095 ns       100000
BM_kmpFindNew_int32_FoundFailed/100000/iterations:100000          9255 ns         9255 ns       100000
BM_kmpFindNew_int32_FoundFailed/1000000/iterations:100000         8007 ns         8006 ns       100000
BM_kmpFindNew_int32_FoundFailed/10000000/iterations:100000        8268 ns         8267 ns       100000
BM_kmpFindNew_uint32_FoundFailed/1/iterations:100000               213 ns          213 ns       100000
BM_kmpFindNew_uint32_FoundFailed/10/iterations:100000              207 ns          207 ns       100000
BM_kmpFindNew_uint32_FoundFailed/100/iterations:100000             201 ns          201 ns       100000
BM_kmpFindNew_uint32_FoundFailed/1000/iterations:100000            202 ns          202 ns       100000
BM_kmpFindNew_uint32_FoundFailed/10000/iterations:100000           202 ns          202 ns       100000
BM_kmpFindNew_uint32_FoundFailed/100000/iterations:100000          201 ns          200 ns       100000
BM_kmpFindNew_uint32_FoundFailed/1000000/iterations:100000         206 ns          206 ns       100000
BM_kmpFindNew_uint32_FoundFailed/10000000/iterations:100000        202 ns          201 ns       100000
 

分别测试了在1000个字符中查找8个字符,能找到和找不到两个场景
static void BM_kmpFindNew_int32_FoundOk(benchmark::State& state)
{
    const std::string s1("a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj1A2B3C4D5E6F");
    const std::string s2("1A2B3C4D5E6F");
    for (auto _ : state) {
        kmpFindNew_int32(s1.c_str(), s1.size(), s2.c_str(), s2.size());
    }
}

static void BM_kmpFindNew_uint32_FoundOk(benchmark::State& state)
{
    const std::string s1("a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj1A2B3C4D5E6F");
    const std::string s2("1A2B3C4D5E6F");
    for (auto _ : state) {
        kmpFindNew_uint32(s1.c_str(), s1.size(), s2.c_str(), s2.size());
    }
}


static void BM_kmpFindNew_int32_FoundFailed(benchmark::State& state)
{
    const std::string s1("a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj1A2B3C4D5E6F");
    const std::string s2("ABCDEFGH");
    for (auto _ : state) {
        kmpFindNew_int32(s1.c_str(), s1.size(), s2.c_str(), s2.size());
    }
}

static void BM_kmpFindNew_uint32_FoundFailed(benchmark::State& state)
{
    const std::string s1("a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj\
a1b2c3d4ef5gsdf6hgsfk7fsd8ds9df1123456768900-99832r872432312--99!@#$%^&*()_+121efg23fdssdfjhsfdweqprvdxjksdfsdsdafsfdkj1A2B3C4D5E6F");
    const std::string s2("ABCDEFGH");
    for (auto _ : state) {
        kmpFindNew_uint32(s1.c_str(), s1.size(), s2.c_str(), s2.size());
    }
}

百思不得其解。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值