<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 5, 2016, at 5:47 PM, Anupama Chandrasekhar <<a href="mailto:anupama.lists@gmail.com" class="">anupama.lists@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">ok, I understand hashes might not be the best.<div class=""><br class=""></div><div class="">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.</div></div></div></blockquote><div><br class=""></div><div>The StringSwitch should behaves *exactly* like the if-sequence I sent. I you have any reason to believe it does, please quote the code and I'll be happy to review.</div><div><br class=""></div><div>-- </div><div>Mehdi</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><br class=""></div><div class="">Anupama</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Feb 5, 2016 at 4:42 PM, Mehdi Amini <span dir="ltr" class=""><<a href="mailto:mehdi.amini@apple.com" target="_blank" class="">mehdi.amini@apple.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
> On Feb 5, 2016, at 2:43 PM, Anupama Chandrasekhar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:<br class="">
><br class="">
> Hi:<br class="">
><br class="">
> 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.<br class="">
<br class="">
</span>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.<br class="">
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:<br class="">
<br class="">
int i = StringSwitch<int>("abc").case("de", 1).case("fghi", 2).case("jkl", 3).default(-1);<br class="">
<br class="">
and:<br class="">
<br class="">
int i;<br class="">
const char *str = "abc";<br class="">
int len = strlen(str);<br class="">
if(len == 2 && std::memcmp(str, "de", 2) {<br class="">
i = 1;<br class="">
} else if(len == 4 && std::memcmp(str, "fghi", 4) {<br class="">
i = 2;<br class="">
} else if(len == 3 && std::memcmp(str, "jkl", 3) {<br class="">
i = 3;<br class="">
} else {<br class="">
i = -1<br class="">
}<br class="">
<br class="">
<br class="">
I left up to you to appreciate the readability :)<br class="">
<span class="HOEnZb"><font color="#888888" class=""><br class="">
--<br class="">
Mehdi<br class="">
<br class="">
<br class="">
</font></span></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>