简体   繁体   中英

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. 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

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.

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.

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. 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?

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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