Ticket #3116 (closed enhancement: fixed)
Tokenizer: Improve Token::isStandardType()
| Reported by: | stinsen | Owned by: | tjarosch |
|---|---|---|---|
| Priority: | Milestone: | ||
| Component: | Refactoring | Keywords: | |
| Cc: |
Description
Hi all,
First of all, thank you for cppcheck, it's a lovely tool!
I don't know if this is a type of activity that you want in TRAC, but I thought I'd give it a shot at least. :)
The patch deals with a rater inefficiently (excessive string comparisons) implemented method in lib/token.cpp. I made some small modifications:
* Made it return directly if it finds a match.
* Rearranged the 'type' array by moving the common 'int' to the start of the array in hopes of getting an earlier match.
* Made the const array static.
Below are the execution counts from a run on lib/*.cpp:
Before:
=======
83305: 731:bool Token::isStandardType() const
-: 732:{
83305: 733: bool ret = false;
83305: 734: const char *type[] = {"bool", "char", "short", "int", "long", "float", "double", "size_t", 0};
749745: 735: for (int i = 0; type[i]; i++)
666440: 736: ret |= (_str == type[i]);
83305: 737: return ret;
-: 738:}
After:
=======
83297: 731:bool Token::isStandardType() const
-: 732:{
-: 733: static const char * const type[] = {"int", "char", "bool", "long", "short", "float", "double", "size_t", 0};
-: 734:
523601: 735: for (int i = 0; type[i]; i++)
472971: 736: if(_str == type[i])
32667: 737: return true;
-: 738:
50630: 739: return false;