views:

72

answers:

2

This is a follow up question to Char* vs String Speed in C++. I have declared the following variables:

std::vector<std::string> siteNames_;
std::vector<unsigned int> ids_;
std::vector<std::string> names_;

I call this function tens of thousands of times and is a major bottleneck. Is there a more efficient way to compare strings? The answer must be cross-platform compatible.

unsigned int converter::initilizeSiteId(unsigned int siteNumber){
    unsigned int siteId = 0;
    for (unsigned int i = 0; i < ids_.size(); i ++){
        if (siteNames_[siteNumber].compare(names_[i]) == 0){
            siteId = ids_[i];
            break; // Once found, will stop searching and break out of for loop
        }
    }
    if (siteId == 0)
        std::cerr << "Could not find ID for site number " << siteNumber << std::endl;

    return siteId;
}
+4  A: 

Use a map or unordered map instead. Then you can do this:

std::map<string, int>names_;
// ...

unsigned int converter::initilizeSiteId(unsigned int siteNumber){
    unsigned int siteId = 0;
    std::map<string, int>::iterator i = names_.find(siteNames_[siteNumber]);
    if (i != names_.end()){
        siteId = i->second;
    }
    else (siteId == 0)
        std::cerr << "Could not find ID for site number " << siteNumber << std::endl;

    return siteId;
}

This will perform in O(log n) time rather than the O(n) you had before.

There are other options if you have a sorted list, such as binary search.

JoshD
And often `unordered_map`'s work better for frequent lookup.
GMan
I am getting errors when I try to compile this code, I also have tried `std::map::iterator i` and that fails. What is mapiterator?
Elpezmuerto
@Elpezmuerto: I've updated it a bit. You need template parameters, and `names_` needs to be a map, and it should match your names to the ids. So `names_["bob"] = 2` for example.
JoshD
@Elpezmuerto: Well, what are the errors and what is your code? Also, this should be covered in a C++ book, [do you have one](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)?
GMan
@GMan, I tend to favor C++ Primer, 5th Edition http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326973
Elpezmuerto
@Elpezmuerto: well surely that book has a section about maps. Reading through that will give you a lot of very useful knowledge. I highly recommend it.
JoshD
Actually maps are barely covered, they only have two pages in Appendix G and thats it, hence probably why I am stuck now :p
Elpezmuerto
@JoshD, I know this is asking alot, but can you provide a map solution to http://stackoverflow.com/questions/3989111/char-vs-string-speed-in-c. `names_.insert(std::pair<std::string, unsigned in>(name,id));` isn't working and not sure why
Elpezmuerto
@Elpez: Ah, you need to be more careful. The title is C++ Primer Plus, not C++ Primer. It matters, especially in this case since C++ Primer is it's own book. In that book (the one you linked), turn to page 1101. Unfortunately, that's a pretty bad book, which is why we don't have it on the list. It's always a bad thing having a bad book. :(
GMan
@Gman, haha that it is, I will have to get some of the recommended books from SO. I tend to use www.cplusplus.com before anything else
Elpezmuerto
@Elpezmuerto: if that snippet of code is accurate, then you misspelled `int`.
JoshD
+1: A good hash map would be an excellent solution here.
DeadMG
@JoshD, hahaah that was it! I feel very relieved that I am not completely worthless. Thanks so much, I have seen a significant boost in performance!
Elpezmuerto
A: 

If you often look up just a few different siteNumber and call it enough times it could be worthwile to implement a cache to store the latest siteNumber:s. Although since you're only working in memory and not to/from disk I doubt it.

dutt