Which option is faster...

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Aug 5 07:42:56 PDT 2013


On Mon, Aug 05, 2013 at 03:59:23PM +0200, 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;
>   ...
>   ...
> }
[...]

It would appear that your bottleneck is not in the continue statements,
but in the repeated calls to std.string.tolower. It's probably better to
write it this way:

	foreach (...)
	{
		auto ext = std.string.tolower(fext[0]);
		if (ext == "doc" || ext == "docx" || ... )
			continue;
	}

This way you save on the overhead of many identical function calls,
which probably outweights any benefit you get by optimizing continue.


T

-- 
It's amazing how careful choice of punctuation can leave you hanging:


More information about the Digitalmars-d-learn mailing list