#include #include #include #include namespace my{ template I2 copy_imp(I1 first, I1 last, I2 out, const std::integral_constant&){ while(first != last){ *out = *first; ++out; ++first; } return out; } template T* copy_imp(const T* first, const T* last, T* out, const std::true_type&){ memcpy(out, first, (last-first)*sizeof(T)); return out+(last-first); } template I2 copy(I1 first, I1 last, I2 out){ typedef typename std::iterator_traits::value_type value_type; return copy_imp(first, last, out, std::has_trivial_assign()); } } const int arraySize = 1000; const int counter=100000; int intArray[arraySize] = {0,}; int intArray2[arraySize]={0,}; int* pArray = intArray; const int* pArray2 = intArray2; int main(){ auto begin= std::chrono::monotonic_clock::now(); for(int i = 0; i < counter; ++i){ my::copy(pArray2, pArray2 + arraySize, pArray); } auto last= std::chrono::monotonic_clock::now() - begin; std::cout << " takes " << std::chrono::duration(last).count() << " seconds" << std::endl; begin= std::chrono::monotonic_clock::now(); for(int i = 0; i < counter; ++i){ my::copy(intArray2, intArray2 + arraySize, intArray); } last= std::chrono::monotonic_clock::now() - begin; std::cout << " takes " << std::chrono::duration(last).count() << " seconds" << std::endl; }