2017-09-21

猎数博客

数据挖掘,机器学习

CPU乘法速度测试

作者:江航 / 2011-04-09 / (阅读 2,381 次) /



#include "stdio.h"
#include <sys/time.h>        

#include "stdint.h"      

//count_time         

void mul(void* data)         

{        

int64_t nums=*((int64_t*)data);      

int a = 0x4567;      

int c=1;         

for(int64_t i =0 ; i < nums ; i ++){         

c *= a;      

}        

}        

int main()       

{        

int64_t num=1024 ; //1K      

printf("mul %lld times take time:",num) ;        

count_time(mul,(void*)&amp;amp;num);         

printf("\n");        

num=1024*1024 ; //1M         

printf("mul %lld times take time:",num) ;        

count_time(mul,(void*)&amp;amp;num);         

printf("\n");        

num=1024*1024*1024 ; //1G        

printf("mul %lld times take time:",num) ;        

count_time(mul,(void*)&amp;amp;num);         

printf("\n");        

num=(int64_t)1024*1024*1024*4 ; //4G         

printf("mul %lld times take time:",num) ;        

count_time(mul,(void*)&amp;amp;num);         

printf("\n");        

num=(int64_t)1024*1024*1024*8 ; //8G         

printf("mul %lld times take time:",num) ;        

count_time(mul,(void*)&amp;amp;num);         

printf("\n");        

return 0;        

}        

输出为:

mul 1024 times take time:10 us

mul 1048576 times take time:9928 us

mul 1073741824 times take time:6156654 us

mul 4294967296 times take time:20495925 us

mul 8589934592 times take time:39255101 us

由此,不超过1G计算的情况下,乘法和加法基本差不多,达到4G乘法时,乘法比加法慢了。8G/40s = 0.2G/s (含计数比较)

如果单独对计数进行计时,得到的数据如下:

count 1024 times take time:9 us

count 1048576 times take time:7983 us

count 1073741824 times take time:5046938 us

count 4294967296 times take time:17359920 us

count 8589934592 times take time:34514233 us

如果简单相减,8G次乘法耗时6s, 1.33G乘法/s,

前面的加法, 8G次加法耗时1.5s, 5.3G加法/s

问题: 为何单单计数花去的时间这么长?比乘法和加法都耗时,这是不合理的。计数只是个简单的比较问题。那么算算减法或者比较这么多次看看。

对于这个函数的计时:


void compare(void* data)         

{        

 int64_t nums=*((int64_t*)data);         

 int a =0xabcd ;         

 int b;      

 for(int64_t i =0 ; i < nums ; i ++){        

 b = (i>a);      

 }       

}        

输出为:

compare 1024 times take time:13 us

compare 1048576 times take time:11994 us

compare 1073741824 times take time:7090761 us

compare 4294967296 times take time:23723733 us

compare 8589934592 times take time:47382765 us



本文地址: http://www.bagualu.net/wordpress/archives/82 转载请注明






相关文章

  • 为solr配中文分词( 3,580 )
  • 霍夫曼编码和算术编码简单比较( 2,978 )
  • CPU乘法速度测试( 2,381 )
  • CPU 加法速度测试( 1,886 )
  • 多线程文件处理实例( 1,759 )
  • 磁盘速度问题( 1,544 )
  • nutch 2.x index( 1,494 )
  • nutch solrdedup( 1,470 )
  • 硬盘参数及速度(二)( 1,444 )
  • nutch 2.x 蜘蛛抓来的数据( 1,395 )
  • Leave a Reply

    您必须登录以发表评论,

    沪ICP备11036560号
    联系我: jianghang at bagualu.net