如何用C编写一个程序来衡量缓存的速度? [英] How to write a program in C to measure the speed of cache?

查看:154
本文介绍了如何用C编写一个程序来衡量缓存的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写一个程序,并尝试比较(度量,如果你能)主存储器和高速缓存访​​问数据的时间。

Write a program and try to compare(measure, if you can) the time of accessing data from main memory and cache.

如果你能做到这一点,那么如何衡量缓存每个级别的速度?

If you can do that, then how to measure the speed of each level of cache?

推荐答案

这通常需要缓存和它的其他方面的几何的一些知识。这也是有帮助的超越简单的用户访问到它,并实现相关的事物的系统的一些控制,如可能比通过标准的C 时钟机制来提供更细的时间。

This generally requires some knowledge of the "geometry" of cache and other aspects of it. It is also helpful to have some control of the system beyond simple user access to it and implementation-dependent things such as finer timing than might be supplied through the standard C clock mechanism.

下面是一个最初的方法:

Here is an initial approach:


  • 写例程采用指向存储器,长度,以及重复次数和读出所有的存储器的连续顺序,反复进行。

  • 写例程采用指向存储器,长度,以及重复次数并写入所有连续顺序存储器的,反复进行。

  • 上面的程序可能有他们的指针转换挥发性来prevent从优化掉编译器访问,否则没有效果。

  • 分配大量内存。

  • 呼叫每个上述例程,获取当前的时间每次调用之前和之后,用各种长度的打电话给该时间不同的长度。

  • Write a routine that takes a pointer to memory, a length, and a number of repetitions and reads all of that memory in consecutive order, repeatedly.
  • Write a routine that takes a pointer to memory, a length, and a number of repetitions and writes to all of that memory in consecutive order, repeatedly.
  • The above routines may have to convert their pointers to volatile to prevent the compiler from optimizing away accesses that otherwise have no effect.
  • Allocate a large amount of memory.
  • Call each of the above routines, getting the current time before and after each call, and calling with a variety of lengths to see the times for different lengths.

当你这样做,你将通常看到快的速度(读取的字节数/每秒写入)对于小的长度和速度较慢较长的长度。其中不同级别的高速缓存的大小超过将发生的速度降低。所以,你很可能会看到反映在使用上述方法收集的数据L1和L2高速缓存的大小。

When you do this, you will typically see fast speeds (number of bytes read/written per second) for small lengths and slower speeds for longer lengths. The speed decreases will occur where the sizes of the different levels of cache are exceeded. So you are quite likely to see the sizes of L1 and L2 cache reflected in data collected using the above technique.

下面是一些原因,这种方法是不够的:

Here are some reasons that approach is inadequate:


  • 它不控制用于读取或写入高速缓存中的说明。 C编译器可能产生的负载字和存储字指令,但许多现代处理器具有可加载和一次存储16个字节,和读出和写入可以是与这些指令的速度比与四字节字指令。

  • 缓存将表现不同,当你按​​照顺序比如果你随机访问存取。最高速缓存做某种尝试时使用的数据,以使最近使用数据保存在高速缓存中,而其他数据被投出跟踪。实际程序的访问零件一般不同于上述的连续操作。

  • 特别是,连续写入存储器可以是能够填充整个高速缓存线,所以,没有什么需要从存储器中读出,而写入只有一个字到特定位置的真实世界的使用模式可能必须通过从存储器中读取的高速缓存行,并在改变的字节合并执行。

  • 从系统上的其他进程的竞争将是什么在缓存中,并与干扰测量。

这篇关于如何用C编写一个程序来衡量缓存的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆