2010-12-01 
				
				
                
                
                
                
                
                
                
            
    
            http://abloz.com
2010.12.1
比较好奇memcpy对性能有多大影响。所以写了个测试程序。源码如下。
对1024个字节进行memcpy,拷贝100万次。将b放在全局区是因为如果放栈区会导致栈空间不足。
zhouhh@zhh64:~$ cat sms/test/testmemcpy.c
//////////////////////////////////////////
//author 	zhouhh
//date 		2010.12.1
//notes       http://abloz.com
//history
//copyright( 2010 ) allright reserved!
/////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUF_LEN 1024
char b[1000000][BUF_LEN]={0};
void main()
{
char a[BUF_LEN]={"1234567890 abcdefg higklmn opq rst uvw xyz"};
a[BUF_LEN]='\0';
int i = 0;
int max = 1000000;
struct timespec ts,te;
printf("a=%sn",a);
clock_gettime (CLOCK_REALTIME, &ts) ;
printf("begin:%ld s,%ld nsn",ts.tv_sec,ts.tv_nsec);
for(i = 0; i< max; i++)
{
memcpy(b[i],a,BUF_LEN);
}
printf("memcpy %d times,buflen=%dn",max,BUF_LEN);
clock_gettime (CLOCK_REALTIME, &te) ;
printf("end:%ld s,%ld nsn",te.tv_sec,te.tv_nsec);
long nsec = te.tv_nsec-ts.tv_nsec;
int sec = te.tv_sec - ts.tv_sec;
if(nsec < 0)
{
nsec += 1000000000;
sec -= 1;
}
printf("spend:%.3f secn",sec+nsec/1000000000.0);
printf("every memcpy spend:%.3f usecn",(sec*1000000000+nsec)/(max*1000.0));
printf("chek b buf:%sn",b[max-1]);
}
zhouhh@zhh64:~/sms/test$ gcc -o tm -lrt testmemcpy.c
zhouhh@zhh64:~/sms/test$ ./tm
a=1234567890 abcdefg higklmn opq rst uvw xyz
begin:1291188463 s,18960481 ns
memcpy 1000000 times,buflen=1024
end:1291188464 s,200238201 ns
spend:1.181 sec
every memcpy spend:1.181 usec
chek b buf:1234567890 abcdefg higklmn opq rst uvw xyz
由此可见,1024个字节,拷贝100万次,花费了1.181秒。 测试CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 5200+,1000MHZ
另一个测试服务器: CPU :Intel(R) Xeon(R) CPU X5680 @ 3.33GHz 24核。
# ./tm
a=1234567890 abcdefg higklmn opq rst uvw xyz
begin:1291188745 s,642300000 ns
memcpy 1000000 times,buflen=1024
end:1291188746 s,904286000 ns
spend:1.262 sec
every memcpy spend:1.262 usec
chek b buf:1234567890 abcdefg higklmn opq rst uvw xyz
总花费1.26秒。与我工作机器用时并无太多差别。 平均大概1次memcpy 1024个字节,花费1微秒多点。
Technorati 标签: memcpy

如非注明转载, 均为原创. 本站遵循知识共享CC协议,转载请注明来源
FEATURED TAGS
        				
                            
        				
                            
                				
                                    css
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    vc6
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    http
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    automake
                                
                            
        				
                            
                				
                                    linux
                                
                            
        				
                            
                				
                                    make
                                
                            
        				
                            
                				
                                    makefile
                                
                            
        				
                            
                				
                                    voip
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    乱码
                                
                            
        				
                            
        				
                            
                				
                                    awk
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    flash
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    vista
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    vi
                                
                            
        				
                            
                				
                                    vim
                                
                            
        				
                            
        				
                            
                				
                                    javascript
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    pietty
                                
                            
        				
                            
                				
                                    putty
                                
                            
        				
                            
        				
                            
                				
                                    ssh
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    posix
                                
                            
        				
                            
                				
                                    subversion
                                
                            
        				
                            
                				
                                    svn
                                
                            
        				
                            
                				
                                    windows
                                
                            
        				
                            
                				
                                    删除
                                
                            
        				
                            
        				
                            
                				
                                    编译
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    多线程
                                
                            
        				
                            
        				
                            
                				
                                    wxwidgets
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    ie
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    ubuntu
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    开源
                                
                            
        				
                            
        				
                            
                				
                                    c
                                
                            
        				
                            
                				
                                    python
                                
                            
        				
                            
                				
                                    bash
                                
                            
        				
                            
                				
                                    备份
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    性能
                                
                            
        				
                            
        				
                            
                				
                                    scp
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    汉字
                                
                            
        				
                            
                				
                                    log
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    ruby
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    中文
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    bug
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    msn
                                
                            
        				
                            
        				
                            
                				
                                    nginx
                                
                            
        				
                            
                				
                                    php
                                
                            
        				
                            
                				
                                    shell
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    wordpress
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    mqueue
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    android
                                
                            
        				
                            
                				
                                    eclipse
                                
                            
        				
                            
                				
                                    java
                                
                            
        				
                            
        				
                            
                				
                                    mac
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    ios
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    html5
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    js
                                
                            
        				
                            
        				
                            
                				
                                    mysql
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    protobuf
                                
                            
        				
                            
        				
                            
                				
                                    apache
                                
                            
        				
                            
                				
                                    hadoop
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    install
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    iocp
                                
                            
        				
                            
                				
                                    twisted
                                
                            
        				
                            
                				
                                    centos
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    mapreduce
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    hbase
                                
                            
        				
                            
        				
                            
                				
                                    thrift
                                
                            
        				
                            
                				
                                    tutorial
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    hive
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    erlang
                                
                            
        				
                            
                				
                                    lucene
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    hdfs
                                
                            
        				
                            
                				
                                    sqoop
                                
                            
        				
                            
        				
                            
                				
                                    utf8
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    filter
                                
                            
        				
                            
        				
                            
                				
                                    草原
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    yarn
                                
                            
        				
                            
                				
                                    ganglia
                                
                            
        				
                            
        				
                            
                				
                                    恢复
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    scrapy
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    django
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    fsimage
                                
                            
        				
                            
        				
                            
                				
                                    flume
                                
                            
        				
                            
                				
                                    tail
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    flume-ng
                                
                            
        				
                            
        				
                            
                				
                                    mining
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    scala
                                
                            
        				
                            
        				
                            
                				
                                    go
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    kafka
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    gradle
                                
                            
        				
                            
        				
                            
                				
                                    cassandra
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    baas
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    spring
                                
                            
        				
                            
                				
                                    postgres
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    maven
                                
                            
        				
                            
                				
                                    mybatis
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    mongodb
                                
                            
        				
                            
        				
                            
                				
                                    https
                                
                            
        				
                            
                				
                                    nodejs
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    镜像
                                
                            
        				
                            
                				
                                    心理学
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    机器学习
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    Keras
                                
                            
        				
                            
        				
                            
                				
                                    theano
                                
                            
        				
                            
        				
                            
        				
                            
                				
                                    anaconda
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    docker
                                
                            
        				
                            
                				
                                    spark
                                
                            
        				
                            
        				
                            
                				
                                    akka-http
                                
                            
        				
                            
                				
                                    json
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    群论
                                
                            
        				
                            
                				
                                    区块链
                                
                            
        				
                            
                				
                                    加密
                                
                            
        				
                            
                				
                                    抽象代数
                                
                            
        				
                            
        				
                            
                				
                                    离散对数
                                
                            
        				
                            
                				
                                    同余
                                
                            
        				
                            
        				
                            
                				
                                    欧拉函数
                                
                            
        				
                            
                				
                                    扩展欧几里德算法
                                
                            
        				
                            
                				
                                    ES6
                                
                            
        				
                            
        				
                            
                				
                                    node-inspect
                                
                            
        				
                            
        				
                            
                				
                                    debug
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    win10
                                
                            
        				
                            
                				
                                    vscode
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
                				
                                    挖矿
                                
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
                            
        				
        			
                