What is the fastest portable way to copy an array in C++

std::copy array
how to use memcpy to copy array in c
copy function c
c++ copy vector to array
c 14 copy
c++ copy array pointer

This question has been bothering me for some time. The possibilities I am considering are

  1. memcpy
  2. std::copy
  3. cblas_dcopy

Does anyone have any clue on what the pros and cons are with these three? Other suggestions are also welcome.

In C++ you should use std::copy by default unless you have good reasons to do otherwise. The reason is that C++ classes define their own copy semantics via the copy constructor and copy assignment operator, and of the operations listed, only std::copy respects those conventions.

memcpy() uses raw, byte-wise copy of data (though likely heavily optimized for cache line size, etc.), and ignores C++ copy semantics (it's a C function, after all...).

cblas_dcopy() is a specialized function for use in linear algebra routines using double precision floating point values. It likely excels at that, but shouldn't be considered general purpose.

If your data is "simple" POD type struct data or raw fundamental type data, memcpy will likely be as fast as you can get. Just as likely, std::copy will be optimized to use memcpy in these situations, so you'll never know the difference.

In short, use std::copy().

Learn Java for Android Development, In C++ you should use std::copy by default unless you have good reasons to do otherwise. The reason is that C++ classes define their own copy semantics via  On ARM cortex A8 with Neon instruction set, the fastest methods use interleaved read/write instructions: vld2.16 {d0,d1}, [r0]! vst1.16 {d0}, [r1]! or saturating instructions to convert a vector of 32-bit integers to a vector of 16-bit integers. Both of these methods are available in c using gcc intrinsic.

memcpy, however, if your array contains non-trivial objects, stick with std::copy.

Beginning Java 7, Tip The arraycopy() method is the fastest portable way to copy one array to 98 81 Current time: 1353115138889 file.separator: \ java.class.path: .;C:\Program  memcpy is probably the fastest way to copy a contiguous block of memory. This is because it will likely be highly optimized to your particular bit of hardware. It is often implemented as a built-in compiler function.

In most cases memcpy will be the fastest, as it is the lowest level and may be implemented in machine code on a given platform. (however, if your array contains non-trivial objects memcpy may not do the correct think, so it may be safer to stick with std::copy)

However it all depends on how well the stdlib is implanted on the given platform etc. As the standard does not say how fast operations must be, there is no way to know in a "portable" since what will be fastest.

Profiling your application will show the fasted on a given platform, but will only tell you about the test platform.

However, when you profile you application you will most likely find that the issues are in your design rather than your choose of array copy method. (E.g. why do you need to copy large arrays so match?)

[help] How to copy fast an array to another array? - C / C++, Tip The arraycopy () method is the fastest portable Way to COpy One array to another. whose methods return a reference to an internal array, you should use arraycopy() to Create a COpy of the array, Date class (discussed in Appendix C​). The code often creates copies of this class and profiling shows that the Array.Copy() call to copy this array takes a lot of time. What can I do to make it faster? The array size is very small and constant: 12 elements. So ideally I'd like something like a C-style array: a single block of memory that's inside the class itself (not a pointer).

memcpy is probably the fastest way to copy a contiguous block of memory. This is because it will likely be highly optimized to your particular bit of hardware. It is often implemented as a built-in compiler function.

Having said that, and non POD C++ object is unlikely to be contiguous and therefore copying arrays of C++ objects using memcpy is likely to give you unexpected results. When copying arrays (or collections) of C++ objects, std::copy will use the object's own copy semantics and is therefore suitable for use with non POD C++ objects.

cblas_dcopy looks like a copy for use with a specific library and probably has little use when not using that library.

fast array copy - C / C++, copy an 2D array x to another 2D array y. I know how to do it copying element by element, but I would like to use a faster method (because I These are all 0 when the array is created. Step 3: We invoke Copy, with 3 arguments—the source array, the destination array, and the length we want to copy. Step 4: The target array is written to the Console. It has the same contents as the source array.

Use std::copy unless profiling shows you a needed benefit in doing otherwise. It honours the C++ object encapsulation, invoking copy constructors and assignment operators, and the implementation could include other inline optimisations such as avoiding an out-of-line function call to memcpy() if the size is known at compile time and is too small to justify the function call overhead. (Some systems may have memcpy macros that make similar determinations, but in general the C++ compiler will have more insight into what optimisations are functionally equivalent.)

FWIW / on the old Linux box I have handy, GCC doesn't do any spectacular optimisations, but bits/type_traits.h does allow the program to easily specify whether std::copy should fall through to memcpy():

 * Copyright (c) 1997
 * Silicon Graphics Computer Systems, Inc.
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and            
 * that both that copyright notice and this permission notice appear            
 * in supporting documentation.  Silicon Graphics makes no                      
 * representations about the suitability of this software for any               
 * purpose.  It is provided "as is" without express or implied warranty.        

This header file provides a framework for allowing compile time dispatch        
based on type attributes. This is useful when writing template code.            
For example, when making a copy of an array of an unknown type, it helps        
to know if the type has a trivial copy constructor or not, to help decide       
if a memcpy can be used.

The class template __type_traits provides a series of typedefs each of
which is either __true_type or __false_type. The argument to
__type_traits can be any type. The typedefs within this template will
attain their correct values by one of these means:
    1. The general instantiation contain conservative values which work
       for all types.
    2. Specializations may be declared to make distinctions between types.
    3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
       will automatically provide the appropriate specializations for all


//Copy an array of elements which have non-trivial copy constructors
template <class _Tp> void
  copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
//Copy an array of elements which have trivial copy constructors. Use memcpy.
template <class _Tp> void
  copy(_Tp* __source,_Tp* __destination,int __n,__true_type);

//Copy an array of any type by using the most efficient copy mechanism
template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
        typename __type_traits<_Tp>::has_trivial_copy_constructor());

Memory Management, C++ FAQ, until I've figured out how every element changes. To do this, I am simply creating a duplicate array and writing the new values in that array. When  Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 64-bit integers. Copy (Array, Int32, Array, Int32, Int32) Copies a range of elements from an Array starting at the specified source index and

InfoWorld, Does C++ have arrays whose length can be specified at run-time? How do I provide reference counting with copy-on-write semantics for a hierarchy of classes? then the fastest way of getting your program complete and correct might be to less portable; usually more efficient (especially when the average object size is  Copy arrays quickly in C# Posted on May 10, 2015 by Rod Stephens It’s easy enough to use a for loop to copy an array, but for most arrays you can get even better performance by using the Array class’s Copy method.

C++ Core Guidelines, Datavue Corp., One Meca Way, Norcross, GA 30093; (404) 564-5555. May 18 8.2 i Compaq Portable III The fastest portable 286 we've ' tested, the Compaq With dual 3 '/2-inch drives, all the necessary ports, and a copy of Sidekick, it's a NDP Fortran-386 and NDP C-386 are globally optimizing 80386 native code  Array cells size . Ensure that the size (resulting from the sizeof operator) of non-large cells of arrays or of vectors be a power of two, and that the size of large cells of arrays or of vectors be not a power of two. The direct access to an array cell is performed by multiplying the index by the cell size, that is a constant.

The GNU Awk User's Guide, The C++ Core Guidelines are a set of tried-and-true guidelines, rules, and And it will run fast – you can afford to do things right. How do we transfer both ownership and all information needed for Look at pointers and arrays: Do range-checking early and not It is not standard and it is not portable. C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. Passing arrays to functions. You can pass to the function a pointer to an array by specifying the array's name without an index. Return array from a function. C allows a function to return an array. Pointer to an array.