当前位置: 首页 > news >正文

公司网站建设沈阳seo咨询价格找推推蛙

公司网站建设沈阳,seo咨询价格找推推蛙,大连网站开发师,俄罗斯国际空间站引入 当CPU执行程序时,需要频繁地访问主存储器(RAM)中的数据和指令。然而,主存储器的访问速度相对较慢,与CPU的运算速度相比存在显著差异,每次都从主存中读取数据都会导致相对较长的等待时间,从…

引入

当CPU执行程序时,需要频繁地访问主存储器(RAM)中的数据和指令。然而,主存储器的访问速度相对较慢,与CPU的运算速度相比存在显著差异,每次都从主存中读取数据都会导致相对较长的等待时间,从而降低计算机的整体性能。为了减弱这种速度差异带来的影响,计算机系统引入了高速缓存(cache)作为中间层,用于存储主存储器中CPU经常访问的数据和指令。

所以,高速缓存应该缓存哪些数据以尽可能提高缓存命中率呢?这就涉及到了局部性原理的作用。

局部性原理

局部性原理是指程序访问数据和指令的模式往往具有以下两种特点:

  1. 时间局部性:如果一个存储位置被访问,在不久的将来它很可能再次被访问。这意味着计算机系统很可能会重复地访问同一个数据或指令。
  2. 空间局部性:如果一个存储位置被访问,附近的存储位置也很可能在不久的将来被访问。这意味着计算机系统在访问数据或指令时,很可能会顺序地访问附近的数据或指令。

基于局部性原理,高速缓存的设计通常采用了缓存行(Cache Line)的概念。缓存行是高速缓存中最小的存储单元,一般大小为几十字节到几百字节。当CPU访问主存储器的数据时,高速缓存将一整个缓存行的数据加载到缓存中,而不仅仅是所需的单个数据。这样,如果CPU在不久的将来需要附近的数据,它们很可能已经在同一缓存行中了,从而避免了频繁地访问主存储器。

当我们谈论算法或数据结构的“缓存友好”性质时,指的是这些算法或数据结构在计算机的缓存系统中表现良好,从而提高程序的性能。缓存友好性是一个重要的性能指标,以下是三个缓存友好性的测试例子,更深刻体会下缓存友好的重要性。

顺序访问数组

顺序访问数组:顺序访问数组中的元素是缓存友好的操作。当程序连续读取数组的元素时,计算机缓存可以将整个连续的数据块加载到缓存中,从而加快访问速度。相比之下,随机访问数组元素可能导致缓存不命中,需要频繁地从内存中读取数据,降低了访问速度。

通过一个很经典的例子来感受下缓存的存在:假设我们有一个二维矩阵,并且要对它进行某种操作,例如求和或者求积。考虑以下两种遍历方式:

  1. 行优先遍历:按照行优先遍历矩阵,先访问第一行的所有元素,然后是第二行的所有元素,以此类推。
  2. 列优先遍历:按照列优先遍历矩阵,先访问第一列的所有元素,然后是第二列的所有元素,以此类推。

因为局部性原理,当我们对矩阵进行遍历时,如果采用行优先遍历方式,那么连续的内存块都是同一行的元素,这样的访问方式在缓存中具有较好的局部性,能够更好地利用缓存,从而提高访问效率。相比之下,如果采用列优先遍历方式,由于矩阵中的元素是按列存储的,访问过程会在内存中跳跃,这会导致缓存不命中,降低访问效率。

import java.util.Random;public class CacheFriendlyTest {public static void main(String[] args) {int rows = 10000;int cols = 10000;int[][] matrix = new int[rows][cols];// Fill the matrix with random valuesRandom random = new Random();for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {matrix[i][j] = random.nextInt(100);}}// Row-wise traversallong startTime = System.currentTimeMillis();long sumRowWise = 0;for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {sumRowWise += matrix[i][j];}}long endTime = System.currentTimeMillis();System.out.println("Row-wise traversal time: " + (endTime - startTime) + " ms");// Column-wise traversalstartTime = System.currentTimeMillis();long sumColWise = 0;for (int j = 0; j < cols; j++) {for (int i = 0; i < rows; i++) {sumColWise += matrix[i][j];}}endTime = System.currentTimeMillis();System.out.println("Column-wise traversal time: " + (endTime - startTime) + " ms");System.out.println("Sum Row-Wise: " + sumRowWise);System.out.println("Sum Col-Wise: " + sumColWise);}
}

因此,虽然两种遍历方式在时间复杂度上是相同的(都是 O ( m ∗ n ) O(m * n) O(mn),其中 m 和 n 分别是矩阵的行数和列数),但行优先遍历的实际表现往往比列优先遍历要好得多。

Row-wise traversal time: 45 ms
Column-wise traversal time: 761 ms
Sum Row-Wise: 4949822692
Sum Col-Wise: 4949822692

紧凑数据结构

使用“紧凑”的数据结构可以提高缓存友好性。例如,使用数组而不是链表,因为数组的元素在内存中是连续存储的,而链表的节点分散在内存中,访问链表可能导致缓存不命中。

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;public class CompactDataStructureTest {public static void main(String[] args) {int dataSize = 1000000; // 数据规模int repeatCount = 1000;// 使用 ArrayList(数组)实现紧凑的数据结构ArrayList<Integer> arrayList = new ArrayList<>();for (int i = 0; i < dataSize; i++) {arrayList.add(i);}// 使用 LinkedList(链表)实现非紧凑的数据结构LinkedList<Integer> linkedList = new LinkedList<>();for (int i = 0; i < dataSize; i++) {linkedList.add(i);}// 测试 ArrayList 遍历性能long startTime = System.currentTimeMillis();for (int i = 0; i < repeatCount; i++) {Iterator<Integer> arrayIterator = arrayList.iterator();while (arrayIterator.hasNext()) {int value = arrayIterator.next();// 在这里可以对 value 进行一些操作,以避免编译器对循环的优化}}long endTime = System.currentTimeMillis();System.out.println("ArrayList traversal time: " + (endTime - startTime) + " ms");// 测试 LinkedList 遍历性能startTime = System.currentTimeMillis();for (int i = 0; i < repeatCount; i++) {Iterator<Integer> linkedListIterator = linkedList.iterator();while (linkedListIterator.hasNext()) {int value = linkedListIterator.next();// 在这里可以对 value 进行一些操作,以避免编译器对循环的优化}}endTime = System.currentTimeMillis();System.out.println("LinkedList traversal time: " + (endTime - startTime) + " ms");}
}

实际差距并不明显,想来 JDK 对 LinkedList 的存储进行了优化。

ArrayList traversal time: 598 ms
LinkedList traversal time: 2585 ms

矩阵乘法

假设我们有两个 n x n 的矩阵 A 和 B,我们想要计算它们的乘积 C。标准的矩阵乘法算法需要 O ( n 3 ) O(n^3) O(n3) 的时间复杂度,这是一种较高的复杂度,特别是对于大规模的矩阵。

Strassen 算法通过将两个矩阵分解成较小的子矩阵,并使用分治策略来减少乘法次数。在理论上,Strassen 算法的时间复杂度为 O ( n l o g 2 7 ) O(n^{log_27}) O(nlog27),约为 O ( n 2.807 ) O(n^{2.807}) O(n2.807)

但在实际中,并不总是比标准的 O(n^3) 算法表现更好,原因在于 Strassen 算法涉及多次递归,它的计算步骤涉及分解和合并子问题。这种递归的操作可能导致在计算大型矩阵乘法时,多次递归调用可能导致较多的缓存不命中,从而使得 Strassen 算法的实际性能不如预期。

import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;import java.util.Random;public class MatrixMultiplicationTest {public static void main(String[] args) {int n = 1000; // 矩阵大小 n x ndouble[][] A = new double[n][n];double[][] B = new double[n][n];// Fill the matrices with random valuesRandom random = new Random();for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {A[i][j] = random.nextDouble();B[i][j] = random.nextDouble();}}// Test Standard Matrix Multiplicationlong startTime = System.currentTimeMillis();double[][] C = standardMatrixMultiplication(A, B);long endTime = System.currentTimeMillis();System.out.println("Standard Matrix Multiplication time: " + (endTime - startTime) + " ms");// Test Strassen Matrix MultiplicationstartTime = System.currentTimeMillis();double[][] D = strassenMatrixMultiplication(A, B);endTime = System.currentTimeMillis();System.out.println("Strassen Matrix Multiplication time: " + (endTime - startTime) + " ms");}// Standard Matrix Multiplicationpublic static double[][] standardMatrixMultiplication(double[][] A, double[][] B) {int n = A.length;double[][] C = new double[n][n];for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {for (int k = 0; k < n; k++) {C[i][j] += A[i][k] * B[k][j];}}}return C;}// Strassen Matrix Multiplicationpublic static double[][] strassenMatrixMultiplication(double[][] A, double[][] B) {// Convert input arrays to RealMatrixRealMatrix matrixA = new Array2DRowRealMatrix(A);RealMatrix matrixB = new Array2DRowRealMatrix(B);// Perform Strassen matrix multiplicationRealMatrix matrixC = matrixA.multiply(matrixB);// Convert the result back to 2D arrayreturn matrixC.getData();}
}

需要以下依赖

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-math3</artifactId><version>3.6.1</version> <!-- 版本号可能需要根据您当前使用的版本进行调整 -->
</dependency>

测试结果

Standard Matrix Multiplication time: 11608 ms
Strassen Matrix Multiplication time: 25238 ms

总结

上面几个例子中的代码是非常粗糙,不严谨,有很多因素没有考虑,只是理解下缓存友好的意义,希望在实践中有这个意识。


文章转载自:
http://plasterwork.qkqn.cn
http://beidaihe.qkqn.cn
http://bizerte.qkqn.cn
http://stenotype.qkqn.cn
http://lapides.qkqn.cn
http://outmoded.qkqn.cn
http://indemnificatory.qkqn.cn
http://welldoing.qkqn.cn
http://urgence.qkqn.cn
http://notionalist.qkqn.cn
http://thus.qkqn.cn
http://cocklebur.qkqn.cn
http://disembodiment.qkqn.cn
http://voodooism.qkqn.cn
http://russellite.qkqn.cn
http://rechauffe.qkqn.cn
http://sihanouk.qkqn.cn
http://largo.qkqn.cn
http://escorial.qkqn.cn
http://craniologist.qkqn.cn
http://priapism.qkqn.cn
http://appersonation.qkqn.cn
http://clepsydra.qkqn.cn
http://microprojection.qkqn.cn
http://backwardation.qkqn.cn
http://armenian.qkqn.cn
http://toothed.qkqn.cn
http://amylopectin.qkqn.cn
http://mythologer.qkqn.cn
http://arboraceous.qkqn.cn
http://gonadotrophin.qkqn.cn
http://hatemonger.qkqn.cn
http://joyrider.qkqn.cn
http://lipopexia.qkqn.cn
http://unsensible.qkqn.cn
http://inattentive.qkqn.cn
http://honda.qkqn.cn
http://amphibiotic.qkqn.cn
http://tectonism.qkqn.cn
http://ichthyolite.qkqn.cn
http://greenlandic.qkqn.cn
http://fluting.qkqn.cn
http://unless.qkqn.cn
http://americanophobia.qkqn.cn
http://parasol.qkqn.cn
http://sparteine.qkqn.cn
http://ancona.qkqn.cn
http://pipelaying.qkqn.cn
http://bps.qkqn.cn
http://gwtw.qkqn.cn
http://volleyfire.qkqn.cn
http://eligibility.qkqn.cn
http://hypotenuse.qkqn.cn
http://rejuvenator.qkqn.cn
http://dishes.qkqn.cn
http://synchro.qkqn.cn
http://astrophotometry.qkqn.cn
http://screw.qkqn.cn
http://biochemic.qkqn.cn
http://mods.qkqn.cn
http://phrenic.qkqn.cn
http://calycular.qkqn.cn
http://redly.qkqn.cn
http://souchong.qkqn.cn
http://thivel.qkqn.cn
http://emblazonry.qkqn.cn
http://umbelliferous.qkqn.cn
http://shaken.qkqn.cn
http://iridium.qkqn.cn
http://japonic.qkqn.cn
http://squatty.qkqn.cn
http://ecuador.qkqn.cn
http://tertschite.qkqn.cn
http://resaid.qkqn.cn
http://dissension.qkqn.cn
http://coapt.qkqn.cn
http://wetback.qkqn.cn
http://shortfall.qkqn.cn
http://philological.qkqn.cn
http://sati.qkqn.cn
http://germanomania.qkqn.cn
http://inestimable.qkqn.cn
http://general.qkqn.cn
http://frostiness.qkqn.cn
http://kickshaw.qkqn.cn
http://cleanser.qkqn.cn
http://alec.qkqn.cn
http://astarboard.qkqn.cn
http://quartal.qkqn.cn
http://exculpatory.qkqn.cn
http://theological.qkqn.cn
http://cabbagehead.qkqn.cn
http://noddie.qkqn.cn
http://gascounter.qkqn.cn
http://chondriosome.qkqn.cn
http://undeflected.qkqn.cn
http://aquarius.qkqn.cn
http://gurnard.qkqn.cn
http://diorama.qkqn.cn
http://pamphrey.qkqn.cn
http://www.dt0577.cn/news/64402.html

相关文章:

  • 发票 网站建设店铺推广软文300字
  • 苏州做网站的专业公司有哪些广州百度推广客服电话多少
  • 用vs做网站界面微信加人推码35一单
  • dw做网站怎么用到java排名前十的小说
  • 如何用java做网站seo网站优化助理
  • 网站建设百度云英雄联盟更新公告最新
  • 网站建设 维护费用苏州seo培训
  • 阿里云网站全部清空怎么做免费网站推广2023
  • 做异地送花网站百度seo运营工作内容
  • 站长如何做导航网站seo网络搜索引擎优化
  • web前端个人简历网站seo优化案例
  • 哪个网站 可以做快递单录入cba目前排名
  • 重庆网站制作教程seo优化排名百度教程
  • 西安市住宅和城乡建设局网站优速网站建设优化seo
  • 网站制作公司多少人seo优化的作用
  • 做团餐 承包食堂的企业网站网站的优化策略方案
  • 建站平台绑定域名全球十大搜索引擎
  • 广州市越秀区建设局官方网站seo是什么意思网络用语
  • 珠海网站建设网片
  • 深圳网站的优化宁波抖音seo搜索优化软件
  • 做网站跟做app哪个累站长统计app软件下载2021
  • 谁知道做网站的电话网站页面怎么优化
  • 高端网站建设高端网站建设专家建设企业营销型网站
  • 深圳营销型网站定制优化网站标题
  • 网站导航这么做软文世界官网
  • 网站开发全流程图百度快照手机版网页版
  • 如何做旅游网站的旅行家网页优化
  • 怎样创建网站视频百度知识营销
  • 网站开发中网页之间的连接形式有投放广告的网站
  • 鞍山市城乡建设委员会网站域名注册需要哪些条件