• 有关CPU合并写的一个java小实验


    CPU中读操作有乱序的情况,其实写操作也有乱序的情况。

    设计到CPU的合并写操作。

    在CPU的寄存器和L1缓存之间有一个WCBuffer,有四个位置大小。数据读写先存入WCBuffer,然后再在寄存器和L1之间交换。

    问题是,现在有长度为6的数据,是一次性操作6个比较快,还是分成两组,一组3个,每组再凑一个字节变成4个长度 的方式快。

    实验验证,刚开始分组的形式快,后来就不一定了。

    /**
     * @version v1.0
     * @Description 验证CPU 合并写操作。其中 WCBuffer只有4个位置大小。
     *  实验验证:CPU一次改6个位置快,还是把6个位置分成两份,都凑成两个4个位置快
     */
    public class WriteCombining {
        private static final int ITERATIONS = Integer.MAX_VALUE;
        private static final int ITEMS = 1 << 24;
        private static final int MASK = ITEMS - 1;
    
        private static final byte[] arrayA = new byte[ITEMS];
        private static final byte[] arrayB = new byte[ITEMS];
        private static final byte[] arrayC = new byte[ITEMS];
        private static final byte[] arrayD = new byte[ITEMS];
        private static final byte[] arrayE = new byte[ITEMS];
        private static final byte[] arrayF = new byte[ITEMS];
    
        public static void main(final String[] args){
            for (int i = 0; i < 5; i++) {
                System.out.println(i+" SingleLoop duraption (ns) = " +runCaseOne());
                System.out.println(i+" SingleLoop duraption (ns) = " +runCaseTwo());
            }
        }
        public static long runCaseOne(){
            long start = System.nanoTime();
            int i = ITERATIONS;
            //第一个循环,尝试一次改动6个位置
            while (--i != 0){
                int slot = i & MASK;
                byte b = (byte) i;  //b也占用一个位置
                arrayA[slot] = b;
                arrayB[slot] = b;
                arrayC[slot] = b;
                arrayD[slot] = b;
                arrayE[slot] = b;
                arrayF[slot] = b;
            }
            return System.nanoTime() - start;
        }
        public static long runCaseTwo(){
            long start = System.nanoTime();
            int i = ITERATIONS;
            //分成两个循环,一个循环负责改其中的3个位置
            while (--i != 0){
                int slot = i & MASK;
                byte b = (byte) i;  //b也占用一个位置,与这三个位置,共同构成4个位置
                arrayA[slot] = b;
                arrayB[slot] = b;
                arrayC[slot] = b;
            }
            while (--i != 0){
                int slot = i & MASK;
                byte b = (byte) i;
                arrayA[slot] = b;
                arrayB[slot] = b;
                arrayC[slot] = b;
            }
            return System.nanoTime() - start;
        }
    }
    莫等闲,白了少年头,空悲切
  • 相关阅读:
    定时器
    表单事件
    闭包,string类,Array类
    构造函数,原型链补充
    Elasticsearch安装(6.4.3版)
    快速配置ssh免密登录
    idea远程debug SpringBoot项目
    java获取一个对象的内存大小
    nginx代理其他网站
    外呼系统实现平均分配策略的实现方式之一
  • 原文地址:https://www.cnblogs.com/lkldeblog/p/15539350.html
Copyright © 2020-2023  润新知