Reflections on isPalindrome

MattCoder via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Oct 28 04:48:36 PDT 2014


Hi,

I don't know if I'm missing something but I did some tests with 
the popFront and popBack version:

bool isPalindrome(R)(R range)
if (isBidirectionalRange!(R))
{
	while (!range.empty){
		if (range.front != range.back) return false;
   		range.popFront();
   	if (range.empty) break;
   	range.popBack();
	}
	return true;
}

Against the older known version (or implementation):

bool isPalindrome2(R)(R r){
     auto len = r.length;
     auto mid = len/2;
     --len;
     for(auto i=0;i<mid;++i){
         if(r[i]!=r[len-i]){
             return false;
	}
     }
     return true;
}

And in my benchmark test, the first version is 3x "slower" than 
the second one.

Matheus.


More information about the Digitalmars-d-learn mailing list