strcpy和memcpy哪个效率高?

xiaoligang 2004-12-01 08:59:59
strcpy和memcpy哪个效率高?为什么?
...全文
1297 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
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

  • 打赏
  • 举报
回复
回复内容太长!!请分开回复!!
自己去找吧,帖不出来了.
  • 打赏
  • 举报
回复
µ±È»ÊÇ 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
bobedong 2004-12-01
  • 打赏
  • 举报
回复
strcpy就是调用的memcpy吧

这种说法有没有根据?很想知道,怎么才能看到strcpy的源码?
a1dao 2004-12-01
  • 打赏
  • 举报
回复
当然是memcpy, memcpy一般会针对不同的处理器作特别优化的.
kobefly 2004-12-01
  • 打赏
  • 举报
回复
strcpy就是调用的memcpy吧
?
这个有根据吗?
不敢苟同

个人以为两者的用途不一样
一个适用于字符串的拷贝
另外一个是内存(事先知道长度)的拷贝

比较的话,可能后者会稍微高一点吧
因为不需要判断是否已结束
感觉就是
while跟for的区别
bingbing1981 2004-12-01
  • 打赏
  • 举报
回复
学习
Squall1009 2004-12-01
  • 打赏
  • 举报
回复
/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;
sharkhuang 2004-12-01
  • 打赏
  • 举报
回复
memcpy高。危险更大
Squall1009 2004-12-01
  • 打赏
  • 举报
回复
strcpy就是调用的memcpy吧
xu233 2004-12-01
  • 打赏
  • 举报
回复
我想应该是memcpy比较效率更高一点吧。因为memcpy是直接对内存进行拷贝,而strcpy还需要进行其他的一些处理后再进行拷贝。我是这么认为的,不知道对不对?请高手指点!
xu233 2004-12-01
  • 打赏
  • 举报
回复
关注,我用memcpy比较多一点。

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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