In the book Accelerated C++ Programming, on page 205, there are the two following implementation of find
template <class In, class X> In find(In begin, In end, const X& x)
I am interested in knowing what is any difference in terms of performance (whether it's actually the same after the compilation?) of the following two implementations.
non-recursive
template <class In, class X> In find(In begin, In end, const X& x)
{
while (begin != end && *begin != x)
++begin;
return begin;
}
recursive
template <class In, class X> In find(In begin, In end, const X& x)
{
if (begin == end || *begin == x)
return begin;
begin++;
return find(begin, end, x);
}
By using Compiler Explorer suggested by Kerrek I got the following
non-recursive https://godbolt.org/g/waKUF2
recursive https://godbolt.org/g/VKNnYZ
It seems to be exactly the same after the compilation? (If I use the tool correctly.. Sorry, I am very new to C++)