Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133321
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-21 14:51
个人简介

天不设牢,二人自在心中舍牢。顺天而行者,心中无牢。

文章分类

全部博文(48)

文章存档

2013年(48)

我的朋友

分类: 架构设计与优化

2013-05-22 15:36:15

原文地址:memcpy性能测试 作者:ckelsel

存在的问题

1。测试结果以微秒为单位,明显不靠谱
2.gettimeofday精度不足,对高精度需要用汇编改写
3.存在cache缓存,建议测试时尽量模拟真实环境

至少存在以上三个问题,不知道为什么会被搞成“推荐文章”,


1。堆、栈中的内存区别
不同点:
堆内存是malloc分配的,直到程序结束或用free释放
栈内存是函数局部变量或参数,函数结束则自动释放
相同点:
二者都存在于内存中,对于memcpy的测试没影响,因此使用栈内存方便。
2。测试的开始,结束边界
t1 = time1;
for ( ... ) {
memcpy(...);
}
t2 = time2;
delta = t2 - t1;
3。char[1024]的大小
1)位(比特),bit,表示0或1,速率 (bps)bits per second
2)字节,byte, 1byte = 8bits,速率(Bps)Byte per second
56Kbps拨号上网的速度是7 byte/s
char是8位,1个字节,char[1024]就是1024个字节,也就是1KB
32位系统,int是32位,4个字节大小
4。多组数据测试,1K,10K,100K,1M
1K,char[1024];
10K,char[1024*10];
100K,char[1024*100];
1M,char[1024*1024];
5。GCC优化级别,-O0,-O1,-O2,-O3,-O,-Os
-O0,不做优化
-O1,-O,减少编译后的代码大小和执行时间(reduce code size and execution time)
-O2,在-O1的基础上,增加编译时间和生成的代码的性能
-O3,在-O2的基础上,还加了剩下的几个优化选项
-Os,在-O2的基础上,但是生成的代码比-O2小
由于发行版的程序肯定会做优化,因此测试程序使用-O2
详见文章末尾的参考资料
6。GCC版本
gcc-4.4.1


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #include <sys/time.h>

  5. /* 测试数据大小 */
  6. #define SIZE_1K 1024
  7. #define SIZE_10K 1024*10
  8. #define SIZE_100K 1024*100
  9. #define SIZE_1M 1024*1024


  10. /* 1M数据*/
  11. void test_1M(void)
  12. {
  13.     struct timeval start;
  14.     struct timeval end;
  15.     int ret_start = -1;
  16.     int ret_end = -1;

  17.     char src[SIZE_1M];
  18.     char dst[SIZE_1M];

  19.     int i;
  20.     long delta_time;

  21.     /* 测试开始 */
  22.     ret_start = gettimeofday(&start, NULL);
  23.     for ( i = 0; i< 1000; ++i )
  24.     {
  25.         memcpy(src, dst, SIZE_1M);
  26.     }
  27.     ret_end = gettimeofday(&end, NULL);

  28.     /* 测试结束,确保函数执行成功 */
  29.     if ( ret_start != 0 || ret_end != 0 )
  30.     {
  31.         exit( -1 );
  32.     }

  33.     /* 输出测试结果 */
  34.     delta_time = (end.tv_sec* 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
  35.     fprintf(stderr, "SIZE_1M, \t\t%ldus\n", delta_time);
  36. }

  37. /* 100K数据*/
  38. void test_100k(void)
  39. {
  40.     struct timeval start;
  41.     struct timeval end;
  42.     int ret_start = -1;
  43.     int ret_end = -1;

  44.     char src[SIZE_100K];
  45.     char dst[SIZE_100K];

  46.     int i;
  47.     long delta_time;

  48.     /* 测试开始 */
  49.     ret_start = gettimeofday(&start, NULL);
  50.     for ( i = 0; i< 1000; ++i )
  51.     {
  52.         memcpy(src, dst, SIZE_100K);
  53.     }
  54.     ret_end = gettimeofday(&end, NULL);

  55.     /* 测试结束,确保函数执行成功 */
  56.     if ( ret_start != 0 || ret_end != 0 )
  57.     {
  58.         exit( -1 );
  59.     }

  60.     /* 输出测试结果 */
  61.     delta_time = (end.tv_sec* 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
  62.     fprintf(stderr, "SIZE_100K, \t\t%ldus\n", delta_time);
  63. }

  64. /* 10K数据*/
  65. void test_10k(void)
  66. {
  67.     struct timeval start;
  68.     struct timeval end;
  69.     int ret_start = -1;
  70.     int ret_end = -1;

  71.     char src[SIZE_10K];
  72.     char dst[SIZE_10K];

  73.     int i;
  74.     long delta_time;

  75.     /* 测试开始 */
  76.     ret_start = gettimeofday(&start, NULL);
  77.     for ( i = 0; i< 1000; ++i )
  78.     {
  79.         memcpy(src, dst, SIZE_10K);
  80.     }
  81.     ret_end = gettimeofday(&end, NULL);

  82.     /* 测试结束,确保函数执行成功 */
  83.     if ( ret_start != 0 || ret_end != 0 )
  84.     {
  85.         exit( -1 );
  86.     }

  87.     /* 输出测试结果 */
  88.     delta_time = (end.tv_sec* 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
  89.     fprintf(stderr, "SIZE_10k, \t\t%ldus\n", delta_time);
  90. }

  91. /* 1K数据*/
  92. void test_1k(void)
  93. {
  94.     struct timeval start;
  95.     struct timeval end;
  96.     int ret_start = -1;
  97.     int ret_end = -1;

  98.     char src[SIZE_1K];
  99.     char dst[SIZE_1K];

  100.     int i;
  101.     long delta_time;

  102.     /* 测试开始 */
  103.     ret_start = gettimeofday(&start, NULL);
  104.     for ( i = 0; i< 1000; ++i )
  105.     {
  106.         memcpy(src, dst, SIZE_1K);
  107.     }
  108.     ret_end = gettimeofday(&end, NULL);

  109.     /* 测试结束,确保函数执行成功 */
  110.     if ( ret_start != 0 || ret_end != 0 )
  111.     {
  112.         exit( -1 );
  113.     }

  114.     /* 输出测试结果 */
  115.     delta_time = (end.tv_sec* 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec);
  116.     fprintf(stderr, "SIZE_1k, \t\t%ldus\n", delta_time);
  117. }

  118. int main(int argc, char **argv)
  119. {
  120.     
  121.     test_1k();

  122.     test_10k();

  123.     test_100k();

  124.     test_1M();

  125.     return 0;
  126. }

测试结果:1000拷贝操作所消耗的时间

SIZE_1k,                350us
SIZE_10k,               5502us
SIZE_100K,              96624us
SIZE_1M,                689399us

参考:


阅读(1324) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~