zoukankan      html  css  js  c++  java
  • 测试伪共享例子-转

    缓存行,避免缓存依赖,补充字段,清除伪共享

    1. time : 7846747771

    2. remove padding time : 36836838456

    可以看出差距非常明显

    public final class FalseSharing implements Runnable {
        public final static int NUM_THREADS = 8;
        public final static long ITERATIONS = 500L * 1000L * 1000L;
        private final int arrayIndex;
    
        private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];
        static {
            for (int i = 0; i < longs.length; i++) {
                longs[i] = new VolatileLong();
            }
        }
    
        public FalseSharing(final int arrayIndex) {
            this.arrayIndex = arrayIndex;
        }
    
        public static void main(final String[] args) throws Exception {
            final long start = System.nanoTime();
            runTest();
            System.out.println("duration = " + (System.nanoTime() - start));
        }
    
        private static void runTest() throws InterruptedException {
            Thread[] threads = new Thread[NUM_THREADS];
    
            for (int i = 0; i < threads.length; i++) {
                threads[i] = new Thread(new FalseSharing(i));
            }
    
            for (Thread t : threads) {
                t.start();
            }
    
            for (Thread t : threads) {
                t.join();
            }
        }
    
        public void run() {
            long i = ITERATIONS + 1;
            while (0 != --i) {
                longs[arrayIndex].value = i;
            }
        }
    
        public final static class VolatileLong {
            public volatile long value = 0L;
            public long p1, p2, p3, p4, p5, p6;   ------use or unuse padding
        }
    }
  • 相关阅读:
    bzoj4183: tree
    bzoj4389: ZYB and Trees
    bzoj3253: 改编
    uoj#274. 【清华集训2016】温暖会指引我们前行
    uoj#272. 【清华集训2016】石家庄的工人阶级队伍比较坚强
    uoj#11. 【UTR #1】ydc的大树
    uoj#29. 【IOI2014】Holiday
    uoj#187. 【UR #13】Ernd
    bzoj5019: [Snoi2017]遗失的答案
    bzoj5017: [Snoi2017]炸弹
  • 原文地址:https://www.cnblogs.com/it-worker365/p/7205988.html
Copyright © 2011-2022 走看看