473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

std::copy, backward_copy and fill implementations

I want to get rid of memcpy, memmove and memset from my sources and
replace them with corresponding algorithms.

But I want not compromise performance when, for example, using arrays
or vectors of standard types.

How can I assure my compiler (with optimizations turned on, of course),
will map gracefully these algorithms to fast microprocessor string
opcodes? (MOVS, STOS..)

Any tips? Do you know a document that describe STL implementations on
most known compilers? (VC++, gcc, mingw...)

Diego Martins
HP

Oct 5 '06 #1
2 2209
ldh

Diego Martins wrote:
I want to get rid of memcpy, memmove and memset from my sources and
replace them with corresponding algorithms.

But I want not compromise performance when, for example, using arrays
or vectors of standard types.

How can I assure my compiler (with optimizations turned on, of course),
will map gracefully these algorithms to fast microprocessor string
opcodes? (MOVS, STOS..)

Any tips? Do you know a document that describe STL implementations on
most known compilers? (VC++, gcc, mingw...)

Diego Martins
HP
I think your best best is just to make the change and trust the
compilers. The STL is used so heavily these days that the compile
writers do the right thing. You can be pretty sure you won't notice a
performance difference at all. If you want to make sure, just look at
the compiler include files -- since the STL is a template library, the
code has to be visible to you. For instance, in the gcc implementation
of copy, there are a large number of auxiliary functions to dispatch
the copy as appropriate for the type being copied. Here is one of them:

template<typename _Tp>
inline _Tp*
__copy_trivial(const _Tp* __first, const _Tp* __last, _Tp*
__result)
{
std::memmove(__result, __first, sizeof(_Tp) * (__last -
__first));
return __result + (__last - __first);
}

So you can see that under the right conditions, the copy() algorithm
will turn into an inline call to memmove.

If you are just using memcpy on POD types, the only difference after
the switch will be the use of memmove instead of memcpy, which is
necessary because copy() allows the input and output ranges to overlap.

-Lewis

Oct 5 '06 #2
ldh <lh****@gmail.comwrote:
>
>How can I assure my compiler (with optimizations turned on, of course),
will map gracefully these algorithms to fast microprocessor string
opcodes? (MOVS, STOS..)

I think your best best is just to make the change and trust the
compilers. The STL is used so heavily these days that the compile
writers do the right thing.
And, compilers are used so heavily (and have been for a while)
that chip makers do the right thing - the processors are optimized
for the simple instructions compilers tend to produce. The string
opcodes aren't as fast as you might think.

I learnt this from assembler programming: I used the string
instructions a lot untill I found out my code was faster without
them.

(I wrote a binary search for a sorted pointer array, and supplemented
it with a simple linear search using SCASD, which I figured would
be faster for small arrays - but I was wrong. The binary search
was faster even at two elements, at one element it was a close
tie. It seems beasts like SCASD are more or less considered
deprecated by the chip designers, especially at Intel. So don't
worry if your C++ compiler doesn't generate them.)
Oct 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
3
by: red floyd | last post by:
I got an error by using std::fill to set an array of pointers to 0. e.g.: class XXX; XXX* v; std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type I have to either explicitly...
12
by: ma740988 | last post by:
I've allocated 4K memory and I'd like to use std::fill to fill each 1K with a different value. Note: I could easily use a vector/deque but I'm interested in a C style array. int main() { int...
3
by: kathy | last post by:
I have array: double a; double b; std::vector <double> vDouble; when I use: std::copy(a,a+1024,vDouble.begin());
6
by: Chris Johnson | last post by:
Greetings all. I am really stuck on this one as I can't seem to grok if I am abusing the C++ language or if I am simply using components of the C++ Standard Library incorrectly. Here is the code:...
2
by: cpisz | last post by:
I saw that using std::fill was the way to go for setting all elements of an array to some value in one foul swoop. However when I tryed it I am getting an error. Can I only use this for vectors...
3
by: none | last post by:
Hi, Consider the following piece of code: int t={1,2,3,4,5,6}; vector<intv; std::copy (t, t+sizeof(t)/sizeof(t), std::back_inserter (v)); Could someone explain me why we can pass "t" as an...
1
by: Gennaro Prota | last post by:
Hi, I have the following function template template< typename T, std::size_t n > void secure_fill( volatile T ( &arr ), const T & value = T() ) { for( std::size_t i( 0 ); i < n; ++i ) {...
9
by: arnuld | last post by:
Earlier, I have posted a program like this, a month ago IIRC. I have created it again, without looking at the old program. Can I have your opinions on this: 1) I wanted my program to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.