1、2线程中,无竞争的原子操作的性能

#include <windows.h>
#include <thread>

	enum {Count = 40000000};
 	union 
    {
        volatile long valueA;
        char _1[64];
    };

    union
    {
        volatile long valueB;
        char _2[64];
    };

    union
    {
        volatile long valueC;
        char _3[64];
    };
    for (int j = 0; j < 10; ++j)
    {
        if (j == 3) //第3+1轮开启一个读线程读A
        {
            puts("以下开启线程 read A-------------------");
            std::thread
            ([pA = &valueA, pB = &valueB, pC = &valueC]()
                {
                    volatile long v;
                    while (true)
                    {
                        v = *pA;
                        for (int i = 0; i < 1; ++i)
                            _mm_pause();
                        Sleep(0);
                    }
                }).detach();
        }
        else if (j == 6) //第6+1轮再开启一个读线程读B
        {
            puts("以下开启线程 read B-------------------");
            std::thread
            ([pA = &valueA, pB = &valueB, pC = &valueC]()
                {
                    volatile long v;
                    while (true)
                    {
                        v = *pB;
                        for (int i = 0; i < 1; ++i)
                            _mm_pause();
                        Sleep(0);
                    }
                }).detach();
        }

        {
            uint32_t t = GetTickCount();
            for (int i = 0; i < Count; ++i)
            {
                InterlockedIncrement(&valueA);
            }
            t = GetTickCount() - t;
            printf("同一个变量          {incA} =              %dM组/s,  *1=%dM/s\n", Count / t / 1000, 1 * Count / t / 1000);
        }

        {
            uint32_t t = GetTickCount();
            for (int i = 0; i < Count; ++i)
            {
                InterlockedIncrement(&valueA);
                InterlockedDecrement(&valueA);
            }
            t = GetTickCount() - t;
            printf("同一个变量          {incA, decA} =         %dM组/s,  *2=%dM/s\n", Count / t / 1000, 2 * Count / t / 1000);
        }

        {
            uint32_t t = GetTickCount();
            for (int i = 0; i < Count; ++i)
            {
                InterlockedIncrement(&valueA);
                InterlockedIncrement(&valueB);
            }
            t = GetTickCount() - t;
            printf("不同cacheline变量   {incA, incB } =        %dM组/s,  *2=%dM/s\n", Count / t / 1000, 2 * Count / t / 1000);
        }

        {
            uint32_t t = GetTickCount();
            for (int i = 0; i < Count; ++i)
            {
                InterlockedIncrement(&valueA);
                InterlockedIncrement(&valueB);
                InterlockedIncrement(&valueC);
            }
            t = GetTickCount() - t;
            printf("不同cacheline变量   {incA, incB, incC } =  %dM组/s,  *3=%dM/s\n", Count / t / 1000, 3 * Count / t / 1000);
        }

        puts("");
    }

同一个变量 {incA} = 150M组/s, *1=150M/s
同一个变量 {incA, decA} = 75M组/s, *2=150M/s
不同cacheline变量 {incA, incB } = 88M组/s, *2=176M/s
不同cacheline变量 {incA, incB, incC } = 49M组/s, *3=147M/s

同一个变量 {incA} = 160M组/s, *1=160M/s
同一个变量 {incA, decA} = 77M组/s, *2=155M/s
不同cacheline变量 {incA, incB } = 88M组/s, *2=176M/s
不同cacheline变量 {incA, incB, incC } = 49M组/s, *3=147M/s

同一个变量 {incA} = 160M组/s, *1=160M/s
同一个变量 {incA, decA} = 77M组/s, *2=155M/s
不同cacheline变量 {incA, incB } = 91M组/s, *2=183M/s
不同cacheline变量 {incA, incB, incC } = 49M组/s, *3=147M/s

以下开启线程 read A-------------------
同一个变量 {incA} = 121M组/s, *1=121M/s
同一个变量 {incA, decA} = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB } = 71M组/s, *2=142M/s
不同cacheline变量 {incA, incB, incC } = 39M组/s, *3=118M/s

同一个变量 {incA} = 128M组/s, *1=128M/s
同一个变量 {incA, decA} = 61M组/s, *2=122M/s
不同cacheline变量 {incA, incB } = 71M组/s, *2=142M/s
不同cacheline变量 {incA, incB, incC } = 41M组/s, *3=124M/s

同一个变量 {incA} = 121M组/s, *1=121M/s
同一个变量 {incA, decA} = 61M组/s, *2=122M/s
不同cacheline变量 {incA, incB } = 73M组/s, *2=146M/s
不同cacheline变量 {incA, incB, incC } = 41M组/s, *3=124M/s

以下开启线程 read B-------------------
同一个变量 {incA} = 121M组/s, *1=121M/s
同一个变量 {incA, decA} = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB } = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB, incC } = 37M组/s, *3=111M/s

同一个变量 {incA} = 128M组/s, *1=128M/s
同一个变量 {incA, decA} = 62M组/s, *2=125M/s
不同cacheline变量 {incA, incB } = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB, incC } = 36M组/s, *3=108M/s

同一个变量 {incA} = 121M组/s, *1=121M/s
同一个变量 {incA, decA} = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB } = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB, incC } = 37M组/s, *3=111M/s

同一个变量 {incA} = 121M组/s, *1=121M/s
同一个变量 {incA, decA} = 62M组/s, *2=125M/s
不同cacheline变量 {incA, incB } = 64M组/s, *2=128M/s
不同cacheline变量 {incA, incB, incC } = 34M组/s, *3=103M/s

此外,此测试进程,开两份时与一份几乎无差异,开三份时数字也有一份的2/3以上。

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值