can I count on my compiler to optimize strlen on const char *?

前端 未结 4 781
后悔当初
后悔当初 2021-01-04 03:34

In my SAX xml parsing callback (XCode 4, LLVM), I am doing a lot of calls to this type of code:

static const char* kFoo = \"Bar\";

void SaxCallBack(char* sa         


        
4条回答
  •  庸人自扰
    2021-01-04 03:52

    Follow-up question:

    Have you tested the following ?

    static std::string const kFoo = "BAR";
    
    void SaxCallBack(char* sax_string,.....)
    {
      if ( sax_string == kFoo)
      {
    
      }
    
    
    }
    

    It's a net win in readability, but I have no idea about the performance cost.

    As an alternative, if you must dispatch by yourself, I have found that using a state-machine like approach (with stack) is much better readability-wise, and might also win performance-wise (instead of having a large number of tags to switch on you only have the tags that can be met right now).

提交回复
热议问题