考考大家

子达如何 2011-05-18 10:13:48
意义我调试了一下搞明白了,看看有没有大侠光看就明白意思的。
顺便请教一下为什么?(就是说一下switch的规范是怎么定义的)

void send(char*to, char* from, int count)
{
int n = (count+7)/8;
switch(count%8)
{
case 0:
do
{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while(--n > 0);
}
}
...全文
4830 78 打赏 收藏 转发到动态 举报
写回复
用AI写文章
78 条回复
切换为时间正序
请发表友善的回复…
发表回复
子达如何 2011-06-03
  • 打赏
  • 举报
回复
楼上的测试比较很好嘛:~)其实正如我所说的,什么时候都应该优先考虑用memcpy,因为每个编译器发布的时候这些库代码都是对目标机器做了最优化的。
roc1005 2011-06-02
  • 打赏
  • 举报
回复
这段代码的效率并不高。
验证如下,在VC6上编译通过。


#include <windows.h>
#include <stdio.h>


#define SIZE 2*1024


void memcpy_8(char*to, char* from, int count)
{
if(from==NULL||to==NULL||count==0)
return ;
int n = (count+7)/8;
switch(count%8)
{
case 0:
do
{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while(--n > 0);
}
}


void *memcpy_char(void *to, void *from, int count)
{
char *p1,*p2;
p1 = (char*)to;
p2 = (char*)from;
while(count-->0)
{
*p1++ = *p2++;
}
return to;
}

void *memcpy_int(void *to, void *from, int count)
{
int *p1,*p2;
char *p3,*p4;
p1 = (int*)to;
p2 = (int*)from;
while (count>3)
{
*p1++ = *p2++;
count -= 4;
}
p3 = (char*)p1;
p4 = (char*)p2;
while (count-->0)
{
*p3++ = *p4++;
}

return to;
}



int main(int argc, char* argv[])
{

char a[SIZE] = "abcdefghijelmnopqrst";
char b[SIZE] = "";

LARGE_INTEGER li1,li2;
QueryPerformanceFrequency(&li1);
double freq = (double)li1.QuadPart;

QueryPerformanceCounter(&li1);
memcpy(b,a,SIZE);
QueryPerformanceCounter(&li2);
printf("memcpy:\t\t%f\n",(li2.QuadPart - li1.QuadPart)/freq);

QueryPerformanceCounter(&li1);
memcpy_char(b,a,SIZE);
QueryPerformanceCounter(&li2);
printf("memcpy_char:\t%f\n",(li2.QuadPart - li1.QuadPart)/freq);

memset(b,0,SIZE);
QueryPerformanceCounter(&li1);
memcpy_int(b,a,SIZE);
QueryPerformanceCounter(&li2);
printf("memcpy_int:\t%f\n",(li2.QuadPart - li1.QuadPart)/freq);

QueryPerformanceCounter(&li1);
memcpy_8(b,a,SIZE);
QueryPerformanceCounter(&li2);
printf("memcpy_8:\t%f\n",(li2.QuadPart - li1.QuadPart)/freq);


return 0;
}


通过增加SIZE,发现memcpy的优势越来越大
javametro 2011-06-02
  • 打赏
  • 举报
回复
好东西啊 我再去看看 duff's device
javametro 2011-06-02
  • 打赏
  • 举报
回复
好东西 终于在各层的帮助下 理解了楼主的用心
谢谢啦
Inn1990 2011-06-02
  • 打赏
  • 举报
回复
楼主NB,神码,好帖,学习了~~~
komway 2011-06-01
  • 打赏
  • 举报
回复
当做语言特性来YY一下未尝不可,当成技巧那就蛋疼了
an9ryfr09 2011-06-01
  • 打赏
  • 举报
回复
不明白怎么竟然有人说是垃圾。

这代码的思维很巧妙。不仅仅用在c语言上,在任何语言上,都有一定用处。用在js上,即使是现在,也很有意义。用其优化循环次数,有时可以提升很大的性能。

那些说这个是垃圾的人,那你岂不是一泡屎?
an9ryfr09 2011-06-01
  • 打赏
  • 举报
回复
这个叫Duff's Device (达夫设备),是很早的一个c程序员发明的优化循环次数的算法。

这个算法发明时,当时的计算机寄存器只有8位,所以他与8求余。对于现在的计算机来说,这种算法已经不是很有意义了。
xuxun 2011-06-01
  • 打赏
  • 举报
回复
乱七八糟,有意思吗
tmy13 2011-05-31
  • 打赏
  • 举报
回复
很有意思的代码。。。开眼界了。不过貌似没看出这样写有很大的作用
snow8pine 2011-05-31
  • 打赏
  • 举报
回复
逻辑真巧妙 佩服佩服!
yuromantic 2011-05-31
  • 打赏
  • 举报
回复
嗯 刚才调试了一下 觉得 不错 呵呵 果然是memcpy 呵呵 学习了
sun_hp 2011-05-31
  • 打赏
  • 举报
回复
功能没有弄明白,
networkwx 2011-05-30
  • 打赏
  • 举报
回复
LZ是213
maimang09 2011-05-30
  • 打赏
  • 举报
回复
mark!
guozhen2010 2011-05-30
  • 打赏
  • 举报
回复
开眼界呀!
fo1_sky 2011-05-30
  • 打赏
  • 举报
回复
只是来围观一下,代码没看懂
xiaofengyay 2011-05-29
  • 打赏
  • 举报
回复
逻辑都不对啊 break也没写 do while 可能可以这样用
qq542027128 2011-05-29
  • 打赏
  • 举报
回复
很好的代码,开阔思路用
阿磊2013 2011-05-29
  • 打赏
  • 举报
回复
如果是考试时,遇到这题,还情有可原,要是工作中写这种代码,这人得拉出去毙了(个人观点)
加载更多回复(56)

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧