From: Mathias Gaunard on
On 31 ao�t, 02:55, Thomas Maeder <mae...(a)glue.ch> wrote:

> Provided the iterators are pointers (or close relatives to pointers),
> which is the prerequisite for considiering the usage of memmove() or
> memcpy(), std::copy can certainly determine whether the areas overlap.

No, it can't.
The compiler could do it, by doing alias analysis, which is a hard (NP-
hard even) problem -- which is why you can't rely on the compiler
doing it -- but a function certainly cannot.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: cpp4ever on
Sada wrote:

> hi how can I improve the performance of a C++ program?
>
> In my program I am using strcmp() strcpy() functions heavily , are
> they costly? if so how can i avoid them??
> Is memcpy() is better than strcpy()?
>
> Please suggest me the factors that affect the performance of a C++
> program.
>
Avoid allocating and deallocating memory, or passing large objects by
value. Both of these will impair performance.

JB

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: James Kuyper on
Mathias Gaunard wrote:
> On 31 août, 02:55, Thomas Maeder <mae...(a)glue.ch> wrote:
>
>> Provided the iterators are pointers (or close relatives to pointers),
>> which is the prerequisite for considiering the usage of memmove() or
>> memcpy(), std::copy can certainly determine whether the areas overlap.
>
> No, it can't.
> The compiler could do it, by doing alias analysis, which is a hard (NP-
> hard even) problem -- which is why you can't rely on the compiler
> doing it -- but a function certainly cannot.

Could you explain that in more detail? It seems to me that
std::copy<T*>(p,q,n) could certainly make use of std::less<T*> to check
whether [p,p+n) overlaps [q,q+n). What's the barrier to performing such
a comparison?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: slobasso on
On Sep 2, 6:02 am, James Kuyper <jameskuy...(a)verizon.net> wrote:
> Mathias Gaunard wrote:
> > On 31 ao�t, 02:55, Thomas Maeder <mae...(a)glue.ch> wrote:
>
> >> Provided the iterators are pointers (or close relatives to pointers),
> >> which is the prerequisite for considiering the usage of memmove() or
> >> memcpy(), std::copy can certainly determine whether the areas overlap.
>
> > No, it can't.
> > The compiler could do it, by doing alias analysis, which is a hard (NP-
> > hard even) problem -- which is why you can't rely on the compiler
> > doing it -- but a function certainly cannot.
>
> Could you explain that in more detail? It seems to me that
> std::copy<T*>(p,q,n) could certainly make use of std::less<T*> to check
> whether [p,p+n) overlaps [q,q+n). What's the barrier to performing such
> a comparison?

There are multiple problems here:

1) std::copy() is an algorithm that uses iterators. Iterators are not
raw pointers, but raw pointers can be iterators. Some types of
iterators cannot be compared for 'less than' or 'greater than' (non
random access iterators). It would be possible to write std::copy to
handle this situation, but only for random access iterators, but this
would be in appropriate due to the second problem (see below).

2) Most copies are non-overlapping. If I add code inside std::copy to
make it handle overlapping copies, it would slow down all the non-
overlapping copies. If you look at the standard implementation where
std::copy is used in various implementations, the fact that the range
is overlapping or not becomes obvious. And if it is overlapping, the
copy direction is also obvious without explicitly testing the range in
many cases. All this improves performance for everyone rather than
making all code suffer from worst case scenarios.

None of this stops you from implementing an overlapping copy in terms
of std::copy or std::copy_backward that only accepts random access
iterators. But I would suggest you look at your code and you may find
that information implicit.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: David Abrahams on

on Tue Sep 01 2009, cpp4ever <n2xssvv.g02gfr12930-AT-ntlworld.com> wrote:

> Sada wrote:
>
>> hi how can I improve the performance of a C++ program?
>>
>> In my program I am using strcmp() strcpy() functions heavily , are
>> they costly? if so how can i avoid them??
>> Is memcpy() is better than strcpy()?
>>
>> Please suggest me the factors that affect the performance of a C++
>> program.
>>
> Avoid allocating and deallocating memory, or passing large objects by
> value. Both of these will impair performance.

Not necessarily.
See http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]