CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

strcpy和memcpy哪个效率高?

楼主xiaoligang()2004-12-01 08:59:59 在 C/C++ / C语言 提问

strcpy和memcpy哪个效率高?为什么? 问题点数:0、回复次数:12Top

1 楼xu233(飞天)回复于 2004-12-01 09:05:03 得分 0

关注,我用memcpy比较多一点。Top

2 楼xu233(飞天)回复于 2004-12-01 09:06:41 得分 0

我想应该是memcpy比较效率更高一点吧。因为memcpy是直接对内存进行拷贝,而strcpy还需要进行其他的一些处理后再进行拷贝。我是这么认为的,不知道对不对?请高手指点!Top

3 楼Squall1009(钰枫)(找工作ing)回复于 2004-12-01 09:08:26 得分 0

strcpy就是调用的memcpy吧Top

4 楼sharkhuang(走吧走吧!人总会慢慢长大~)回复于 2004-12-01 09:08:49 得分 0

memcpy高。危险更大Top

5 楼Squall1009(钰枫)(找工作ing)回复于 2004-12-01 09:12:14 得分 0

/usr/include>grep   memcpy   *.h   |more  
  curses.h:#define                 memcpy(dst,   src,   len)       bcopy((src),   (dst),   (len))  
  memory.h:extern   void   *memcpy(void   *,   const   void   *,   size_t);  
  memory.h:extern   void   *memcpy();  
  string.h:using   std::memcpy;       //   here  
  wchar.h:using   std::wmemcpy;Top

6 楼bingbing1981(^_^)回复于 2004-12-01 09:12:39 得分 0

学习Top

7 楼kobefly(科比--网络学习中)回复于 2004-12-01 09:13:05 得分 0

strcpy就是调用的memcpy吧  
  ?  
  这个有根据吗?  
  不敢苟同  
   
  个人以为两者的用途不一样  
  一个适用于字符串的拷贝  
  另外一个是内存(事先知道长度)的拷贝  
   
  比较的话,可能后者会稍微高一点吧  
  因为不需要判断是否已结束  
  感觉就是  
  while跟for的区别Top

8 楼a1dao(希望之盾)回复于 2004-12-01 09:23:39 得分 0

当然是memcpy,   memcpy一般会针对不同的处理器作特别优化的.Top

9 楼bobedong(bobedong)回复于 2004-12-01 10:05:43 得分 0

strcpy就是调用的memcpy吧  
   
  这种说法有没有根据?很想知道,怎么才能看到strcpy的源码?Top

10 楼DiabloWalkOnTheEarth(我想到个绝妙的昵称,只是地方太小,写不下)回复于 2004-12-01 11:07:02 得分 0

µ±È»ÊÇ   memcpy   ¸ü¿ì,   strcpy   ÐèÒª¼ì²é   EOS   ,   ¿Ï¶¨»á±È   memcpy   ¸üÂýЩ.²»¹ýºÃÏñVC6ÏÂmemcpy   ==   memmove   ,   ¼ì²éÁËÄÚ´æÖصþµÄÇé¿ö,   ¿ÉÄÜÔÚÓÐÐ©ÌØÊâµÄÇé¿öÏ»áÂýЩ.   ²¢ÇÒÒ»°ãÀ´Ëµ,¶Ô   memcpy   µÄÓÅ»¯³Ì¶ÈÒª¸ßµÄ¶à.  
  VC6µÄCRTÔ´Âë¿ÉÔÚ   $(VCROOT)/CRT/SRC   ÏÂÕÒµ½,Ïñ   memcpy   ,   ÕâЩ´úÂë¾Íµ½   $(VCROOT)/CRT/SRC/Intel   ÏÂÕÒÈ¥:  
   
  memcpy   :  
   
                page         ,132  
                  title       memcpy   -   Copy   source   memory   bytes   to   destination  
  ;***  
  ;memcpy.asm   -   contains   memcpy   and   memmove   routines  
  ;  
  ;               Copyright   (c)   1986-1997,   Microsoft   Corporation.   All   right   reserved.  
  ;  
  ;Purpose:  
  ;               memcpy()   copies   a   source   memory   buffer   to   a   destination   buffer.  
  ;               Overlapping   buffers   are   not   treated   specially,   so   propogation   may   occur.  
  ;               memmove()   copies   a   source   memory   buffer   to   a   destination   buffer.  
  ;               Overlapping   buffers   are   treated   specially,   to   avoid   propogation.  
  ;  
  ;*******************************************************************************  
   
                  .xlist  
                  include   cruntime.inc  
                  .list  
   
  M_EXIT     macro  
                  ret                                           ;   _cdecl   return  
                  endm         ;   M_EXIT  
   
                  CODESEG  
   
  page  
  ;***  
  ;memcpy   -   Copy   source   buffer   to   destination   buffer  
  ;  
  ;Purpose:  
  ;               memcpy()   copies   a   source   memory   buffer   to   a   destination   memory   buffer.  
  ;               This   routine   does   NOT   recognize   overlapping   buffers,   and   thus   can   lead  
  ;               to   propogation.  
  ;               For   cases   where   propogation   must   be   avoided,   memmove()   must   be   used.  
  ;  
  ;               Algorithm:  
  ;  
  ;               void   *   memcpy(void   *   dst,   void   *   src,   size_t   count)  
  ;               {  
  ;                               void   *   ret   =   dst;  
  ;  
  ;                               /*  
  ;                                 *   copy   from   lower   addresses   to   higher   addresses  
  ;                                 */  
  ;                               while   (count--)  
  ;                                               *dst++   =   *src++;  
  ;  
  ;                               return(ret);  
  ;               }  
  ;  
  ;memmove   -   Copy   source   buffer   to   destination   buffer  
  ;  
  ;Purpose:  
  ;               memmove()   copies   a   source   memory   buffer   to   a   destination   memory   buffer.  
  ;               This   routine   recognize   overlapping   buffers   to   avoid   propogation.  
  ;               For   cases   where   propogation   is   not   a   problem,   memcpy()   can   be   used.  
  ;  
  ;       Algorithm:  
  ;  
  ;               void   *   memmove(void   *   dst,   void   *   src,   size_t   count)  
  ;               {  
  ;                               void   *   ret   =   dst;  
  ;  
  ;                               if   (dst   <=   src   ||   dst   >=   (src   +   count))   {  
  ;                                               /*  
  ;                                                 *   Non-Overlapping   Buffers  
  ;                                                 *   copy   from   lower   addresses   to   higher   addresses  
  ;                                                 */  
  ;                                               while   (count--)  
  ;                                                               *dst++   =   *src++;  
  ;                                               }  
  ;                               else   {  
  ;                                               /*  
  ;                                                 *   Overlapping   Buffers  
  ;                                                 *   copy   from   higher   addresses   to   lower   addresses  
  ;                                                 */  
  ;                                               dst   +=   count   -   1;  
  ;                                               src   +=   count   -   1;  
  ;  
  ;                                               while   (count--)  
  ;                                                               *dst--   =   *src--;  
  ;                                               }  
  ;  
  ;                               return(ret);  
  ;               }  
  ;  
  ;  
  ;Entry:  
  ;               void   *dst   =   pointer   to   destination   buffer  
  ;               const   void   *src   =   pointer   to   source   buffer  
  ;               size_t   count   =   number   of   bytes   to   copy  
  ;  
  ;Exit:  
  ;               Returns   a   pointer   to   the   destination   buffer   in   AX/DX:AX  
  ;  
  ;Uses:  
  ;               CX,   DX  
  ;  
  ;Exceptions:  
  ;*******************************************************************************  
   
  ifdef   MEM_MOVE  
                  _MEM_           equ   <memmove>  
  else     ;   MEM_MOVE  
                  _MEM_           equ   <memcpy>  
  endif     ;   MEM_MOVE  
   
  %               public     _MEM_  
  _MEM_       proc   \  
                  dst:ptr   byte,   \  
                  src:ptr   byte,   \  
                  count:IWORD  
   
                              ;   destination   pointer  
                              ;   source   pointer  
                              ;   number   of   bytes   to   copy  
   
  ;               push         ebp                           ;U   -   save   old   frame   pointer  
  ;               mov           ebp,   esp                 ;V   -   set   new   frame   pointer  
   
                  push         edi                           ;U   -   save   edi  
                  push         esi                           ;V   -   save   esi  
   
                  mov           esi,[src]               ;U   -   esi   =   source  
                  mov           ecx,[count]           ;V   -   ecx   =   number   of   bytes   to   move  
   
                  mov           edi,[dst]               ;U   -   edi   =   dest  
   
  ;  
  ;   Check   for   overlapping   buffers:  
  ;               If   (dst   <=   src)   Or   (dst   >=   src   +   Count)   Then  
  ;                               Do   normal   (Upwards)   Copy  
  ;               Else  
  ;                               Do   Downwards   Copy   to   avoid   propagation  
  ;  
   
                  mov           eax,ecx                   ;V   -   eax   =   byte   count...  
   
                  mov           edx,ecx                   ;U   -   edx   =   byte   count...  
                  add           eax,esi                   ;V   -   eax   =   point   past   source   end  
   
                  cmp           edi,esi                   ;U   -   dst   <=   src   ?  
                  jbe           short   CopyUp         ;V   -   yes,   copy   toward   higher   addresses  
   
                  cmp           edi,eax                   ;U   -   dst   <   (src   +   count)   ?  
                  jb             CopyDown                 ;V   -   yes,   copy   toward   lower   addresses  
  Top

11 楼DiabloWalkOnTheEarth(我想到个绝妙的昵称,只是地方太小,写不下)回复于 2004-12-01 11:08:32 得分 0

回复内容太长!!请分开回复!!  
  自己去找吧,帖不出来了.Top

12 楼DiabloWalkOnTheEarth(我想到个绝妙的昵称,只是地方太小,写不下)回复于 2004-12-01 11:09:36 得分 0

strcpy   ,   strcat   的:  
   
   
  %               public     strcat,   strcpy             ;   make   both   functions   available  
  strcpy     proc  
                  push         edi                                   ;   preserve   edi  
                  mov           edi,[esp+8]                   ;   edi   points   to   dest   string  
                  jmp           short   copy_start  
   
  strcpy     endp  
   
                  align       16  
   
  strcat     proc  
   
                  .FPO         (   0,   2,   0,   0,   0,   0   )  
   
                  mov           ecx,[esp+4]                   ;   ecx   ->   dest   string  
                  push         edi                                   ;   preserve   edi  
                  test         ecx,3                               ;   test   if   string   is   aligned   on   32   bits  
                  je             short   find_end_of_dest_string_loop  
   
  dest_misaligned:                                         ;   simple   byte   loop   until   string   is   aligned  
                  mov           al,byte   ptr   [ecx]  
                  inc           ecx  
                  test         al,al  
                  je             short   start_byte_3  
                  test         ecx,3  
                  jne           short   dest_misaligned  
   
                  align       4  
   
  find_end_of_dest_string_loop:  
                  mov           eax,dword   ptr   [ecx]   ;   read   4   bytes  
                  mov           edx,7efefeffh  
                  add           edx,eax  
                  xor           eax,-1  
                  xor           eax,edx  
                  add           ecx,4  
                  test         eax,81010100h  
                  je             short   find_end_of_dest_string_loop  
                  ;   found   zero   byte   in   the   loop  
                  mov           eax,[ecx   -   4]  
                  test         al,al                               ;   is   it   byte   0  
                  je             short   start_byte_0  
                  test         ah,ah                               ;   is   it   byte   1  
                  je             short   start_byte_1  
                  test         eax,00ff0000h               ;   is   it   byte   2  
                  je             short   start_byte_2  
                  test         eax,0ff000000h             ;   is   it   byte   3  
                  je             short   start_byte_3  
                  jmp           short   find_end_of_dest_string_loop  
                                                                          ;   taken   if   bits   24-30   are   clear   and   bit  
                                                                          ;   31   is   set  
  start_byte_3:  
                  lea           edi,[ecx   -   1]  
                  jmp           short   copy_start  
  start_byte_2:  
                  lea           edi,[ecx   -   2]  
                  jmp           short   copy_start  
  start_byte_1:  
                  lea           edi,[ecx   -   3]  
                  jmp           short   copy_start  
  start_byte_0:  
                  lea           edi,[ecx   -   4]  
  ;               jmp           short   copy_start  
   
  ;               edi   points   to   the   end   of   dest   string.  
  copy_start::  
                  mov           ecx,[esp+0ch]               ;   ecx   ->   sorc   string  
                  test         ecx,3                               ;   test   if   string   is   aligned   on   32   bits  
                  je             short   main_loop_entrance  
   
  src_misaligned:                                           ;   simple   byte   loop   until   string   is   aligned  
                  mov           dl,byte   ptr   [ecx]  
                  inc           ecx  
                  test         dl,dl  
                  je             short   byte_0  
                  mov           [edi],dl  
                  inc           edi  
                  test         ecx,3  
                  jne           short   src_misaligned  
                  jmp           short   main_loop_entrance  
   
  main_loop:                                                     ;   edx   contains   first   dword   of   sorc   string  
                  mov           [edi],edx                       ;   store   one   more   dword  
                  add           edi,4                               ;   kick   dest   pointer  
  main_loop_entrance:  
                  mov           edx,7efefeffh  
                  mov           eax,dword   ptr   [ecx]   ;   read   4   bytes  
   
                  add           edx,eax  
                  xor           eax,-1  
   
                  xor           eax,edx  
                  mov           edx,[ecx]                       ;   it's   in   cache   now  
   
                  add           ecx,4                               ;   kick   dest   pointer  
                  test         eax,81010100h  
   
                  je             short   main_loop  
                  ;   found   zero   byte   in   the   loop  
  ;   main_loop_end:  
                  test         dl,dl                               ;   is   it   byte   0  
                  je             short   byte_0  
                  test         dh,dh                               ;   is   it   byte   1  
                  je             short   byte_1  
                  test         edx,00ff0000h               ;   is   it   byte   2  
                  je             short   byte_2  
                  test         edx,0ff000000h             ;   is   it   byte   3  
                  je             short   byte_3  
                  jmp           short   main_loop           ;   taken   if   bits   24-30   are   clear   and   bit  
                                                                          ;   31   is   set  
  byte_3:  
                  mov           [edi],edx  
                  mov           eax,[esp+8]                   ;   return   in   eax   pointer   to   dest   string  
                  pop           edi  
                  ret  
  byte_2:  
                  mov           [edi],dx  
                  mov           eax,[esp+8]                   ;   return   in   eax   pointer   to   dest   string  
                  mov           byte   ptr   [edi+2],0  
                  pop           edi  
                  ret  
  byte_1:  
                  mov           [edi],dx  
                  mov           eax,[esp+8]                   ;   return   in   eax   pointer   to   dest   string  
                  pop           edi  
                  ret  
  byte_0:  
                  mov           [edi],dl  
                  mov           eax,[esp+8]                   ;   return   in   eax   pointer   to   dest   string  
                  pop           edi  
                  ret  
   
  strcat     endp  
   
                  end  
   
  Top

相关问题

  • memcpy and strcpy
  • 哪个效率高?
  • 哪个效率快?
  • memcpy的效率高吗?
  • strcpy和memcpy的区别?
  • 请问哪个流的效率最高?
  • if/else与switch/case哪个效率高?
  • map,vector哪个效率更高,help!
  • 请问memcpy和strcpy有什么区别?
  • C和C++哪个运行效率高点?为什么?

关键词

  • c++
  • 拷贝
  • memcpy
  • dst
  • strcpy
  • ecx
  • je short
  • 效率
  • frac
  • dest

得分解答快速导航

  • 帖主:xiaoligang

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo