【问题标题】:What changes do I make to this function to optimize the execution time?为了优化执行时间,我对该函数进行了哪些更改?
【发布时间】:2015-08-30 14:49:00
【问题描述】:
unsigned int lookup_bloom(unsigned char (*id)[HEXXID], unsigned int len,
        void *bf)
{
    int i;
    struct bloom_structure *filter = (struct bloom_structure *) bf;
    unsigned int *nexthop = NULL;
    // The returned values of counting_bloom_check() are 0 if found else 1
    unsigned char matchvec[WDIST] = {1};
    unsigned char tmp1[HEXXID + 1] = {0};
    unsigned char tmp2[HEXXID] = {0};

    memcpy(tmp1, id, HEXXID);
    memcpy(tmp2, tmp2, HEXXID);
    // Although the paper suggests to perform parallel membership queries
    for (i = len; i >= MINLENGTH; i--) {
        tmp1[i / BYTE] = tmp1[i / BYTE] >> (BYTE - i % BYTE) <<
                            (BYTE - i % BYTE);
        if (!filter->flag[i - MINLENGTH])
            continue;
        matchvec[i - MINLENGTH] =
        counting_bloom_check(filter->bloom[i - MINLENGTH], tmp1,
                                HEXXID);
    }
    // Parse the matchvec from longest to shortest to perform table search
    for (i = len; i >= MINLENGTH; i--) {
        tmp2[i / BYTE] = tmp2[i / BYTE] >> (BYTE - i % BYTE) <<
                            (BYTE - i % BYTE);
        if (matchvec[i - MINLENGTH] || !filter->flag[i - MINLENGTH])
            continue;
        nexthop = hashit_lookup(filter->hashtable[i - MINLENGTH],
                    tmp2);
        if (nexthop)
            return *nexthop;
    }

    return 0;
}

以下是代码中使用的一些定义:

#define WDIST 140
#define MINLENGTH 20

struct bloom_structure {
    bool flag[WDIST];
    unsigned int length[WDIST];
    int low[WDIST];
    int high[WDIST];
    counting_bloom_t *bloom[WDIST];
    hash_t hashtable[WDIST];
};

我正在测量这个函数的执行时间。 有人可以帮我优化这个程序吗?

如果有人可以建议对编写循环进行任何更改以减少执行时间,那就太好了。

提前谢谢你!

【问题讨论】:

  • 分析。并测量每一段代码,看看瓶颈在哪里(这基本上就是分析所做的)。

标签: c performance optimization


【解决方案1】:

根据此功能的重要性,您可以尝试最小化执行的除法次数( / 和 % ),因为它们是 CPU 成本最高的操作。

您可以合并两个循环,因为它们使用相同的范围。这样您就可以在变量中使用相同的索引计算(那些暗示除法的计算)。

如果你真的想推动这个,你可以预先计算所有暗示除法计算的索引,以便从数组或任何认为合适的容器中访问值。

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    相关资源
    最近更新 更多