标签:any 处理 大于 不能 word count 版本 splay 方式
复制数据的快速方法std::copy
C++复制数据各种方法大家都会,很多时候我们都会用到std::copy这个STL函数,这个效率确实很不错,比我们一个一个元素复制或者用迭代器复制都来的要快很多。
const int size = 100000000; int *k = new int[size]; int *p = new int[size]; //const int size = 5F5E100h; DWORD t1, t2; t1 = GetTickCount(); for (int i = 0; i != size; i++) p[i] = k[i]; t2 = GetTickCount(); cout << t2 - t1 << "ms" << std::endl; t1 = GetTickCount(); int *pStart = k, *pEnd = k + size, *pDest = p; for (; pStart != pEnd; pDest++, pStart++) *pDest = *pStart; t2 = GetTickCount(); cout << t2 - t1 << "ms" << std::endl; t1 = GetTickCount(); std::copy(k, k + size, p); t2 = GetTickCount(); cout << t2 - t1 << "ms" << std::endl;
for (int i = 0; i != size; i++) 00F0A8B1 mov dword ptr [ebp-54h],0 00F0A8B8 jmp main+0A3h (0F0A8C3h) 00F0A8BA mov eax,dword ptr [ebp-54h] 00F0A8BD add eax,1 00F0A8C0 mov dword ptr [ebp-54h],eax 00F0A8C3 cmp dword ptr [ebp-54h],5F5E100h 00F0A8CA je main+0C0h (0F0A8E0h) p[i] = k[i]; 00F0A8CC mov eax,dword ptr [ebp-54h] 00F0A8CF mov ecx,dword ptr [p] 00F0A8D2 mov edx,dword ptr [ebp-54h] 00F0A8D5 mov esi,dword ptr [k] 00F0A8D8 mov edx,dword ptr [esi+edx*4] 00F0A8DB mov dword ptr [ecx+eax*4],edx 00F0A8DE jmp main+9Ah (0F0A8BAh)
int *pStart = k, *pEnd = k + size, *pDest = p; 00F0A944 mov eax,dword ptr [k] 00F0A947 mov dword ptr [pStart],eax 00F0A94A mov eax,dword ptr [k] 00F0A94D add eax,17D78400h 00F0A952 mov dword ptr [pEnd],eax 00F0A955 mov eax,dword ptr [p] 00F0A958 mov dword ptr [pDest],eax for (; pStart != pEnd; pDest++, pStart++) 00F0A95B jmp main+14Fh (0F0A96Fh) 00F0A95D mov eax,dword ptr [pDest] 00F0A960 add eax,4 00F0A963 mov dword ptr [pDest],eax 00F0A966 mov ecx,dword ptr [pStart] 00F0A969 add ecx,4 00F0A96C mov dword ptr [pStart],ecx 00F0A96F mov eax,dword ptr [pStart] 00F0A972 cmp eax,dword ptr [pEnd] 00F0A975 je main+163h (0F0A983h) *pDest = *pStart; 00F0A977 mov eax,dword ptr [pDest] 00F0A97A mov ecx,dword ptr [pStart] 00F0A97D mov edx,dword ptr [ecx] 00F0A97F mov dword ptr [eax],edx 00F0A981 jmp main+13Dh (0F0A95Dh)
template<class _InIt, class _OutIt> inline _OutIt _Copy_memmove(_InIt _First, _InIt _Last, _OutIt _Dest) { // implement copy-like function as memmove const char * const _First_ch = reinterpret_cast<const char *>(_First); const char * const _Last_ch = reinterpret_cast<const char *>(_Last); char * const _Dest_ch = reinterpret_cast<char *>(_Dest); const size_t _Count = _Last_ch - _First_ch; _CSTD memmove(_Dest_ch, _First_ch, _Count); return (reinterpret_cast<_OutIt>(_Dest_ch + _Count)); } template<class _InIt, class _OutIt> inline _OutIt _Copy_unchecked1(_InIt _First, _InIt _Last, _OutIt _Dest, _General_ptr_iterator_tag) { // copy [_First, _Last) to [_Dest, ...), arbitrary iterators for (; _First != _Last; ++_Dest, (void)++_First) *_Dest = *_First; return (_Dest); } template<class _InIt, class _OutIt> inline _OutIt _Copy_unchecked1(_InIt _First, _InIt _Last, _OutIt _Dest, _Trivially_copyable_ptr_iterator_tag) { // copy [_First, _Last) to [_Dest, ...), pointers to trivially copyable return (_Copy_memmove(_First, _Last, _Dest)); } template<class _InIt, class _OutIt> inline _OutIt _Copy_unchecked(_InIt _First, _InIt _Last, _OutIt _Dest) { // copy [_First, _Last) to [_Dest, ...) // note: _Copy_unchecked is called directly elsewhere in the STL return (_Copy_unchecked1(_First, _Last, _Dest, _Ptr_copy_cat(_First, _Dest))); } template<class _InIt, class _OutIt> inline _OutIt _Copy_no_deprecate1(_InIt _First, _InIt _Last, _OutIt _Dest, input_iterator_tag, _Any_tag) { // copy [_First, _Last) to [_Dest, ...), arbitrary iterators return (_Rechecked(_Dest, _Copy_unchecked(_First, _Last, _Unchecked_idl0(_Dest)))); } template<class _InIt, class _OutIt> inline _OutIt _Copy_no_deprecate1(_InIt _First, _InIt _Last, _OutIt _Dest, random_access_iterator_tag, random_access_iterator_tag) { // copy [_First, _Last) to [_Dest, ...), random-access iterators _CHECK_RANIT_RANGE(_First, _Last, _Dest); return (_Rechecked(_Dest, _Copy_unchecked(_First, _Last, _Unchecked(_Dest)))); } template<class _InIt, class _OutIt> inline _OutIt _Copy_no_deprecate(_InIt _First, _InIt _Last, _OutIt _Dest) { // copy [_First, _Last) to [_Dest, ...), no _SCL_INSECURE_DEPRECATE_FN warnings _DEBUG_RANGE_PTR(_First, _Last, _Dest); return (_Copy_no_deprecate1(_Unchecked(_First), _Unchecked(_Last), _Dest, _Iter_cat_t<_InIt>(), _Iter_cat_t<_OutIt>())); } template<class _InIt, class _OutIt> inline _OutIt copy(_InIt _First, _InIt _Last, _OutIt _Dest) { // copy [_First, _Last) to [_Dest, ...) _DEPRECATE_UNCHECKED(copy, _Dest); return (_Copy_no_deprecate(_First, _Last, _Dest)); }
return (_Copy_no_deprecate1(_Unchecked(_First), _Unchecked(_Last), _Dest, _Iter_cat_t<_InIt>(), _Iter_cat_t<_OutIt>()));
template<class _Iter> using _Iter_cat_t = typename iterator_traits<_Iter>::iterator_category;
input_iterator_tag //输入迭代器,单向一次一步移动,读取一次
output_iterator_tag //输出迭代器,单向一次一步移动,涂写一次
forward_iterator_tag //向前迭代器,单向一次一步移动,多次读写,继承自输入迭代器
bidirectional_iterator_tag //双向迭代器,双向一次一步移动,多次读写,继承自向前迭代器
random_access_iterator_tag //随机迭代器,任意位置多次读写,继承自双向迭代器
template<class _Source, class _Dest> inline _General_ptr_iterator_tag _Ptr_copy_cat(const _Source&, const _Dest&) { // return pointer copy optimization category for arbitrary iterators return {}; } template<class _Source, class _Dest> inline conditional_t<is_trivially_assignable<_Dest&, _Source&>::value, typename _Ptr_cat_helper<remove_const_t<_Source>, _Dest>::type, _General_ptr_iterator_tag> _Ptr_copy_cat(_Source * const&, _Dest * const&) { // return pointer copy optimization category for pointers return {}; }
_CSTD memmove(_Dest_ch, _First_ch, _Count);
当源内存的首地址等于目标内存的首地址时,不进行任何拷贝
当源内存的首地址大于目标内存的首地址时,实行正向拷贝
当源内存的首地址小于目标内存的首地址时,实行反向拷贝
这三个指令每一次执行都会将源地址到目的地址的数据的复制
目标地址由di决定(对于movsb,movsw是di,movsd是edi),每执行一次,根据DF的值+1(DF == 0)或者-1(DF ==1)
源地址由si决定(对于movsb,movsw是si,movsd是esi),每执行一次,根据DF的值+1(DF == 0)或者-1(DF ==1)
__asm { mov esi, dword ptr[k]; mov edi, dword ptr[p]; mov ecx, 5F5E100h; rep movsd; };
标签:any 处理 大于 不能 word count 版本 splay 方式
原文地址:http://www.cnblogs.com/Philip-Tell-Truth/p/6555516.html