linux编程的108种奇淫巧计-1(FALSE SHARING)

        我打算开个长篇系列博客,预计108篇吧,但愿能在1-2年内完成。

 

        注:考虑到我本人长期使用linux系统做开发,因此有些代码在windows环境下无法编译或者会有问题,建议大家都使用linux环境做实验,最好是2.6内核的,处理器需要是多核。很多读者说我是纸上谈兵,这个确实不好,从本系列开始基本都是50行左右的代码。本系列不代表任何学术或业界立场,仅我个人兴趣爱好,由于水平有限,错误难免,请不要有过分期望。

 

        废话不多说,今天就写第一篇如下:

 

        以下一段代码分别编译成两个程序,仅仅是变量定义的差别,执行时间差距巨大,这是什么原因呢?

        本博客暂不解密,等数天后,我把后半部写上,希望读者朋友们踊跃实验,并回答。

 

#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#ifdef FS
size_t cnt_1;
size_t cnt_2;
#endif
#ifdef NONFS

size_t __attribute__((aligned(64))) cnt_1;
size_t __attribute__((aligned(64))) cnt_2;
#endif
void* sum1(void*)
{
        for(int i=0;i < 10000000;++i) {
                cnt_1 += 1;
        }
};
void* sum2(void*)
{
        for(int i=0;i < 10000000;++i) {
                cnt_2 += 1;
        }
};
int main()
{
        pthread_t* thread = (pthread_t*) malloc(2*sizeof( pthread_t));
        pthread_create(&thread[0],NULL,sum1,NULL);  //创建2个线程分别求和
        pthread_create(&thread[1],NULL,sum2,NULL);
        pthread_join(thread[0],NULL);    //等待2个线程结束计算。
        pthread_join(thread[1],NULL);
        free(thread);
        printf("cnt_1:%d,cnt_2:%d",cnt_1,cnt_2);
}
 编译方法:
g++ fs.cpp -o test_nfs -g -D NONFS –lpthread
g++ fs.cpp -o test_fs -g -D FS –lpthread

用time ./test_nfs 和 time ./test_fs会发现执行时间差别很大,请读者踊跃跟帖作答,谢谢。

 

续篇参见:http://blog.csdn.net/pennyliang/archive/2010/10/26/5966433.aspx

 

 

网友linyai做了实验,大家可以参考一下,以下来自跟帖。

 

 

linyai 发表于Thu Oct 21 2010 10:11:22 GMT+0800 (China Standard Time)   举报 回复 删除
fs: 0m0.083s nfs: 0m0.043s fs: 0m0.130s nfs: 0m0.034s fs: 0m0.084s nfs: 0m0.037s fs: 0m0.086s nfs: 0m0.042s

 

 

  • 0
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 29
    评论
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值