memcpy函数实现及其优化
2015-05-07 21:31
246 查看
1:函数原型void * memcpy ( void * destination, const void * source, size_t num );
函数作用
参考:http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
Copy block of memory
Copies the values of num bytes
from the location pointed by source directly
to the memory block pointed by destination.
The underlying type of the objects pointed by both the source and destination pointers
are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source -
it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters,
shall be at least numbytes,
and should not overlap (for overlapping memory blocks, memmove is
a safer approach).
实现1:《高质量c++,c编程指南》
[cpp] view
plaincopy
void *mymemcpy(void *dst,const void *src,size_t num)
{
assert((dst!=NULL)&&(src!=NULL));
//assert(des>=src+num||src>dst+num);
byte * psrc = (byte *)src;//byte 既为unsigned char类型
byte * pdst = (byte *)dst;
while(num-->0)*pdst++ = *psrc++;
return dst;
}
缺点:没有考虑内存重叠的情况,可以加一个断言换为:assert(des>=src+num||src>dst+num);
实现2:考虑重叠,有重叠情况也复制
[cpp] view
plaincopy
void * mymemcpy(void *dest, const void *src, size_t count)
{
if (dest == NULL || src == NULL)
return NULL;
char *pdest = static_cast <char*>(dest);
const char *psrc = static_cast <const char*>(psrc);
int n = count;
if (pdest > psrc && pdest < psrc+count)
{
for (size_t i=n-1; i != -1; --i)
{
pdest[i] = psrc[i];
}
}
else
{
for (size_t i= 0; i < n; i++)
{
pdest[i] = psrc[i];
}
}
return dest;
}
对memcpy函数的改进:
改进思想:
大部分认为memcpy是一个char到char的拷贝的循环,担心它的效率。实际上,memcpy是一个效率最高的内存拷贝函数,他不会那么傻,来做一个一个字节的内存拷贝,在地址不对齐的情况下,他是一个字节一个字节的拷,地址对齐以后,就会使用CPU字长来拷(和dma类似),32bit或64bit,还会根据cpu的类型来选择一些优化的指令来进行拷贝。总的来说,memcpy的实现是和CPU类型、操作系统、cLib相关的。毫无疑问,它是内存拷贝里效率最高的,请放心使用。
[cpp] view
plaincopy
void *mymemcpy(void *dst,const void *src,size_t num)
{
assert((dst!=NULL)&&(src!=NULL));
int wordnum = num/4;//计算有多少个32位,按4字节拷贝
int slice = num%4;//剩余的按字节拷贝
int * pintsrc = (int *)src;
int * pintdst = (int *)dst;
while(wordnum--)*pintdst++ = *pintsrc++;
while (slice--)*((char *)pintdst++) =*((char *)pintsrc++);
return dst;
}
函数作用
参考:http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
Copy block of memory
Copies the values of num bytes
from the location pointed by source directly
to the memory block pointed by destination.
The underlying type of the objects pointed by both the source and destination pointers
are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source -
it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters,
shall be at least numbytes,
and should not overlap (for overlapping memory blocks, memmove is
a safer approach).
实现1:《高质量c++,c编程指南》
[cpp] view
plaincopy
void *mymemcpy(void *dst,const void *src,size_t num)
{
assert((dst!=NULL)&&(src!=NULL));
//assert(des>=src+num||src>dst+num);
byte * psrc = (byte *)src;//byte 既为unsigned char类型
byte * pdst = (byte *)dst;
while(num-->0)*pdst++ = *psrc++;
return dst;
}
缺点:没有考虑内存重叠的情况,可以加一个断言换为:assert(des>=src+num||src>dst+num);
实现2:考虑重叠,有重叠情况也复制
[cpp] view
plaincopy
void * mymemcpy(void *dest, const void *src, size_t count)
{
if (dest == NULL || src == NULL)
return NULL;
char *pdest = static_cast <char*>(dest);
const char *psrc = static_cast <const char*>(psrc);
int n = count;
if (pdest > psrc && pdest < psrc+count)
{
for (size_t i=n-1; i != -1; --i)
{
pdest[i] = psrc[i];
}
}
else
{
for (size_t i= 0; i < n; i++)
{
pdest[i] = psrc[i];
}
}
return dest;
}
对memcpy函数的改进:
改进思想:
大部分认为memcpy是一个char到char的拷贝的循环,担心它的效率。实际上,memcpy是一个效率最高的内存拷贝函数,他不会那么傻,来做一个一个字节的内存拷贝,在地址不对齐的情况下,他是一个字节一个字节的拷,地址对齐以后,就会使用CPU字长来拷(和dma类似),32bit或64bit,还会根据cpu的类型来选择一些优化的指令来进行拷贝。总的来说,memcpy的实现是和CPU类型、操作系统、cLib相关的。毫无疑问,它是内存拷贝里效率最高的,请放心使用。
[cpp] view
plaincopy
void *mymemcpy(void *dst,const void *src,size_t num)
{
assert((dst!=NULL)&&(src!=NULL));
int wordnum = num/4;//计算有多少个32位,按4字节拷贝
int slice = num%4;//剩余的按字节拷贝
int * pintsrc = (int *)src;
int * pintdst = (int *)dst;
while(wordnum--)*pintdst++ = *pintsrc++;
while (slice--)*((char *)pintdst++) =*((char *)pintsrc++);
return dst;
}
相关文章推荐
- memcpy函数实现及其优化
- memcpy函数实现及其优化
- memcpy函数实现及其优化
- memcpy函数实现及其优化
- memcpy函数实现及其优化
- memcpy函数实现及其优化
- 机器学习优化算法L-BFGS及其分布式实现
- swap实现(优化空间和时间),及其带来的问题
- 使用复述,实现分布式锁及其优化
- mmseg中文分词算法的python实现及其优化
- 马踏棋盘问题(骑士周游问题)及其优化算法java实现
- 顺序表上实现冒泡排序及其优化
- 【C++】String类及其优化版的实现
- strcpy函数与memcpy函数的区别及其函数实现
- java版排序算法简介及冒泡排序以及优化,选择排序,直接插入排序,希尔排序,堆排序,快速排序及其优化前言 2 分类 2 稳定性 3 时间复杂度 4 Java实现版本 5 1、冒泡排序 6 2、选择排序
- SSE图像算法优化系列八:自然饱和度(Vibrance)算法的模拟实现及其SSE优化(附源码,可作为SSE图像入门,Vibrance算法也可用于简单的肤色调整)。
- 使用Redis实现分布式锁及其优化
- 【Algorithm】一般约束优化问题——PHR算法及其Matlab实现
- strcpy和memcpy函数的实现及其区别
- 快排实现及其优化