简体   繁体   English

std :: vector查找比执行简单操作更快吗?

[英]Is a std::vector lookup faster than performing a simple operation?

I'm trying to optimize some C++ code for speed, and not concerned about memory usage. 我正在尝试优化一些C ++代码以提高速度,而不关心内存使用情况。 If I have some function that, for example, tells me if a character is a letter: 如果我有一些功能,例如,告诉我一个字符是否是一个字母:

bool letterQ ( char letter ) {
    return (lchar>=65 && lchar<=90) ||
       (lchar>=97 && lchar<=122);
}

Would it be faster to just create a lookup table, ie 是否可以更快地创建查找表,即

int lookupTable[128];
for (i = 0 ; i < 128 ; i++) {
    lookupTable[i] = // some int value that tells what it is
}

and then modifying the letterQ function above to be 然后修改上面的letterQ函数

bool letterQ ( char letter ) {
    return lookupTable[letter]==LETTER_VALUE;
}

I'm trying to optimize for speed in this simple region, because these functions are called a lot, so even a small increase in speed would accumulate into long-term gain. 我正在尝试在这个简单的区域中优化速度,因为这些函数被大量调用,因此即使速度的小幅增加也会累积到长期增益中。

EDIT: 编辑:

I did some testing, and it seems like a lookup array performs significantly better than a lookup function if the lookup array is cached. 我做了一些测试,如果查找数组被缓存,它似乎比查找函数表现得更好。 I tested this by trying 我通过尝试测试了这个

for (int i = 0 ; i < size ; i++) {
      if ( lookupfunction( text[i] ) )
      // do something
}

against 反对

bool lookuptable[128];
for (int i = 0 ; i < 128 ; i++) {
    lookuptable[i] = lookupfunction( (char)i );
}

for (int i = 0 ; i < size ; i++) {
    if (lookuptable[(int)text[i]])
        // do something
}

Turns out that the second one is considerably faster - about a 3:1 speedup. 事实证明,第二个速度要快得多 - 大约是3:1的加速。

About the only possible answer is "maybe" -- and you can find out by running a profiler or something else to time the code. 关于唯一可能的答案是“可能” - 您可以通过运行分析器或其他东西来查找代码的时间。 At one time, it would have been pretty easy to give "yes" as the answer with little or no qualification. 有一段时间,如果答案很少或没有资格,那么很容易给出“是”。 Now, given how much faster CPUs have gotten than memory, it's a lot less certain -- you can do a lot of computation in the time it takes to fill one cache line from main memory. 现在,由于速度更快的CPU是如何比内存得到,这是少了很多肯定的-你可以做很多计算的需要,填补从主内存中的一个缓存行的时间。

Edit: I should add that in either C or C++, it's probably best to at least start with the functions (or macros) built into the standard library. 编辑:我应该在C或C ++中添加它,最好至少从标准库中内置的函数(或宏)开始。 These are often fairly carefully optimized for the target and (more importantly for most people) support things like switching locales, so you won't be stuck trying to explain to your German users that 'ß' isn't really a letter (and I doubt many will be much amused by "but that's really two letters, not one!) 这些通常针对目标进行相当精心优化的(对于大多数人来说更为重要)支持切换区域设置等事项,因此您不会试图向您的德国用户解释'ß'不是真正的字母(而且我怀疑很多人会觉得很有趣“但那真是封信,而不是一封!)

First, I assume you have profiled the code and verified that this particular function is consuming a noticeable amount of CPU time over the runtime of the program? 首先,我假设您已经对代码进行了分析并验证了此特定函数在程序运行时间内消耗了大量的CPU时间?

I wouldn't create a vector as you're dealing with a very fixed data size. 当你处理一个非常固定的数据大小时,我不会创建一个向量。 In fact, you could just create a regular C++ array and initialize is at program startup. 实际上,您可以创建一个常规的C ++数组,并在程序启动时进行初始化。 With a really modern compiler that supports array initializers you even can do something like this: 使用支持数组初始化程序的真正现代编译器,您甚至可以执行以下操作:

bool lookUpTable[128] = { false, false, false, ..., true, true, ... };

Admittedly I'd probably write a small script that generates out the code rather then doing it all manually. 不可否认,我可能会编写一个小脚本来生成代码,而不是手动完成所有操作。

对于这样的简单计算,内存访问(由查找表引起)将比每次执行计算更昂贵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM