strcpy VS memcpy, 不看不知道,一看吓一跳
时间:2010-08-05
来源:互联网
- //g++ -g malloc.c
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys/time.h>
- #include <string.h>
-
- #define SIZE 128
-
- struct timeval begin, end;
- char source[SIZE], destination[SIZE];
-
- void mcp(char* dst, char* src, int len, int num)
- {
- for(int i = 0; i<num; i++)
- {
- memcpy(dst, src, len);
- }
- }
-
- void strcopy(char* dst, char* src, int num)
- {
- for(int i = 0; i<num; i++)
- {
- strcpy(dst, src);
- }
- }
-
- int main(int argc, char** argv)
- {
- int num;
-
- if ( argc < 2 )
- {
- printf("usage: %s num\n", argv[0]);
- exit(1);
- }
- num = atoi(argv[1]);
-
- memset(source, 0x21, sizeof(source));
- source[sizeof(source)-1] = '\0';
-
- gettimeofday(&begin, NULL);
- mcp(destination, source, 128, 600000);
- gettimeofday(&end, NULL);
- printf("dur: %u seconds, %u micro sec\n", end.tv_sec-begin.tv_sec, end.tv_usec-begin.tv_usec);
-
- gettimeofday(&begin, NULL);
- strcopy(destination, source, 600000);
- gettimeofday(&end, NULL);
- printf("dur: %u seconds, %u micro sec\n", end.tv_sec-begin.tv_sec, end.tv_usec-begin.tv_usec);
- return 0;
- }
dur: 0 seconds, 64623 micro sec
dur: 0 seconds, 46371 micro sec
作者: mukey 发布时间: 2010-08-05
静待高手解答
作者: ilwmin 发布时间: 2010-08-05
- $ ./a.out 2
- dur: 0 seconds, 11797 micro sec
- dur: 0 seconds, 64604 micro sec
作者: daybreakcx 发布时间: 2010-08-05
[root@localhost test]# ./2 2
dur: 0 seconds, 13710 micro sec
dur: 0 seconds, 62005 micro sec
作者: rain_fish 发布时间: 2010-08-05

作者: phy0077 发布时间: 2010-08-05
作者: rain_fish 发布时间: 2010-08-05
作者: ilwmin 发布时间: 2010-08-05
作者: prolj 发布时间: 2010-08-05
[root@localhost test]# ./2 2
dur: 0 seconds, 13710 micro sec
dur: 0 seconds, 62005 mic ...
rain_fish 发表于 2010-08-05 17:23
嗯,我又看了一下,和优化很有关系,如果编译时用:
#:~/test$ g++ -g -O2 malloc.c
#:~/test$ ./a.out
dur: 0 seconds, 9993 micro sec
dur: 0 seconds, 28225 micro sec
所以,你可以试试g++ -g -O0 malloc.c, 再测测
作者: mukey 发布时间: 2010-08-05
prolj 发表于 2010-08-05 17:27
期待给出源码
作者: mukey 发布时间: 2010-08-05
int i;
for(i = 0; i<num; i++)
不知道为什么,我这边默认的是C99。C89你的写法也报错,我不知道你怎么编过的。
作者: prolj 发布时间: 2010-08-05
strcpy是可以四个一次
作者: zliming 发布时间: 2010-08-05
O0 +
O1 +
O2 +
O3 +
作者: zliming 发布时间: 2010-08-05
/* Copyright (C) 1992, 1995, 1997, 2002, 2004 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#undef __stpcpy
#undef stpcpy
#ifndef weak_alias
# define __stpcpy stpcpy
#endif
/* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */
char *
__stpcpy (dest, src)
char *dest;
const char *src;
{
register char *d = dest;
register const char *s = src;
do
*d++ = *s;
while (*s++ != '\0');
return d - 1;
}
#ifdef libc_hidden_def
libc_hidden_def (__stpcpy)
#endif
#ifdef weak_alias
weak_alias (__stpcpy, stpcpy)
#endif
#ifdef libc_hidden_builtin_def
libc_hidden_builtin_def (stpcpy)
#endif
memcpy代码:
/* Copy memory to memory until the specified number of bytes
has been copied. Overlap is NOT handled correctly.
Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Torbjorn Granlund ([email protected]).
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <string.h>
#include <memcopy.h>
#include <pagecopy.h>
#undef memcpy
void *
memcpy (dstpp, srcpp, len)
void *dstpp;
const void *srcpp;
size_t len;
{
unsigned long int dstp = (long int) dstpp;
unsigned long int srcp = (long int) srcpp;
/* Copy from the beginning to the end. */
/* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ);
/* Copy whole pages from SRCP to DSTP by virtual address manipulation,
as much as possible. */
PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len);
/* Copy from SRCP to DSTP taking advantage of the known alignment of
DSTP. Number of bytes remaining is put in the third argument,
i.e. in LEN. This number may vary from machine to machine. */
WORD_COPY_FWD (dstp, srcp, len, len);
/* Fall out and copy the tail. */
}
/* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len);
return dstpp;
}
libc_hidden_builtin_def (memcpy)
作者: ilwmin 发布时间: 2010-08-05
- __Z3mcpPcS_ii:
- LFB21:
- pushl %ebp
- LCFI0:
- movl %esp, %ebp
- LCFI1:
- pushl %edi
- LCFI2:
- pushl %esi
- LCFI3:
- pushl %ebx
- LCFI4:
- movl 16(%ebp), %edx
- movl 20(%ebp), %ebx
- testl %ebx, %ebx
- jle L1
- xorl %eax, %eax
- .p2align 2,,3
- L3:
- movl 8(%ebp), %edi
- movl 12(%ebp), %esi
- movl %edx, %ecx
- rep movsb
- incl %eax
- cmpl %eax, %ebx
- jg L3
- L1:
- popl %ebx
- LCFI5:
- popl %esi
- LCFI6:
- popl %edi
- LCFI7:
- leave
- LCFI8:
- ret
http://dev.firnow.com/course/3_p ... 00861/118646_4.html
这个是strcpy->strcat的实现
作者: zliming 发布时间: 2010-08-05
作者: chinesedragon 发布时间: 2010-08-05
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28