Which option is faster...

Jonathan M Davis jmdavisProg at gmx.com
Mon Aug 5 23:50:29 PDT 2013


On Monday, August 05, 2013 15:59:23 jicman wrote:
> Greetings!
> 
> I have this code,
> 
> foreach (...)
> {
> 
>    if (std.string.tolower(fext[0]) == "doc" ||
>      std.string.tolower(fext[0]) == "docx" ||
>      std.string.tolower(fext[0]) == "xls" ||
>      std.string.tolower(fext[0]) == "xlsx" ||
>      std.string.tolower(fext[0]) == "ppt" ||
>      std.string.tolower(fext[0]) == "pptx")
>     continue;
> }
> 
> foreach (...)
> {
>    if (std.string.tolower(fext[0]) == "doc")
>      continue;
>    if (std.string.tolower(fext[0]) == "docx")
>      continue;
>    if (std.string.tolower(fext[0]) == "xls")
>      continue;
>    if (std.string.tolower(fext[0]) == "xlsx")
>      continue;
>    if (std.string.tolower(fext[0]) == "ppt")
>      continue;
>    if (std.string.tolower(fext[0]) == "pptx")
>     continue;
>    ...
>    ...
> }

As others have pointed out, the two code snippets are semantically identical, 
and there's a decent chance that the compiler will generate identical code for 
both. It wouldn't surprise me if the first snippet resulted in more concise 
assembly code, but there shouldn't be any performance difference between the 
two. But the first snippet is cleaner, so between those two, you should use 
that.

Of course, as others have pointed out, it would be far more efficient to simply 
calculate std.string.tolower(fext[0]) once and re-use the result, which should 
be a very large performance gain here (assuming that the code around it 
doesn't dwarf it in cost). However, another possibility that I don't think 
anyone has pointed out is std.string.icmp, which will do case-insensitive 
comparison, meaning that you could do

if(fext[0].icmp("doc") == 0 || fext[0].icmp("docx") == 0 || ...)

and that might be more efficient as it avoids having to allocate a new string 
when fext[0] isn't all lowercase already. However, you would have to benchmark 
the code to be sure, since depending on the typical input, the code around 
this code, and how many times this code gets called, calling toLower once 
could be more efficient or using icmp could be more efficient, since toLower 
allocates whereas icmp doesn't, but icmp does more comparisons that a simple 
==. But if you really care about efficiency, you should try both ways and see 
which is faster for your use case.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list