[llvm-dev] StringSwitch class

Anupama Chandrasekhar via llvm-dev llvm-dev at lists.llvm.org
Fri Feb 5 17:47:24 PST 2016


ok, I understand hashes might not be the best.

I agree that the StringSwitch is concise and easy to read, but aren't we
doing a bunch of extra work even if a match is found in the very first
case, while in the if we would early exit.

Anupama

On Fri, Feb 5, 2016 at 4:42 PM, Mehdi Amini <mehdi.amini at apple.com> wrote:

>
> > On Feb 5, 2016, at 2:43 PM, Anupama Chandrasekhar via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
> >
> > Hi:
> >
> > I have a question about the llvm StringSwitch class. Why is this more
> efficient than comparing the hashes of the strings or just using a bunch of
> if statements.
>
> I don't know about hashes performance (comparing a small string is
> probably more efficient than comparing hashes, and I'd be reluctant to
> collisions anyway). Now since you mentioned a "bunch of if statements" you
> can get very close.
> StringSwitch is caching the length of the string to compare and first
> checking the length before doing the actual comparison using memcmp. So the
> two constructs below it should be equivalent:
>
> int i = StringSwitch<int>("abc").case("de", 1).case("fghi", 2).case("jkl",
> 3).default(-1);
>
> and:
>
> int i;
> const char *str = "abc";
> int len = strlen(str);
> if(len == 2 && std::memcmp(str, "de", 2) {
>    i = 1;
> } else if(len == 4 && std::memcmp(str, "fghi", 4) {
>    i = 2;
> } else if(len == 3 && std::memcmp(str, "jkl", 3) {
>    i = 3;
> } else {
>    i = -1
> }
>
>
> I left up to you to appreciate the readability :)
>
> --
> Mehdi
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160205/807a2dc1/attachment.html>


More information about the llvm-dev mailing list