如何确定.NET中的CPU缓存大小? [英] how to determine CPU cache size in .NET?
本文介绍了如何确定.NET中的CPU缓存大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有一种方法可以确定托管代码中的CPU缓存大小吗?
I would like to know if there is a way to determine CPU cache size in managed code?
我正在编写一种Strassen算法,用于C#中的矩阵乘法,想知道我可以在缓存中放入多少个矩阵元素以提高计算速度。
I am writing a Strassen's algorithm for matrix multiplication in C# and would like to know how many elements of the matrices I could fit into cache to improve computational speed.
推荐答案
您可以使用WMI来检索缓存信息。
You can use WMI to retrieve cache information.
您首先需要在项目中添加对 System.Management.dll
的引用,然后您可以使用以下代码:
You will first need to add a reference to System.Management.dll
to your project, then you can use the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
namespace Scratch
{
public enum CacheLevel : ushort
{
Level1 = 3,
Level2 = 4,
Level3 = 5,
}
public static class CPUInfo
{
public static List<uint> GetCacheSizes(CacheLevel level)
{
ManagementClass mc = new ManagementClass("Win32_CacheMemory");
ManagementObjectCollection moc = mc.GetInstances();
List<uint> cacheSizes = new List<uint>(moc.Count);
cacheSizes.AddRange(moc
.Cast<ManagementObject>()
.Where(p => (ushort)(p.Properties["Level"].Value) == (ushort)level)
.Select(p => (uint)(p.Properties["MaxCacheSize"].Value)));
return cacheSizes;
}
}
}
<$的详细信息c $ c> Win32_CacheMemory WMI类位于:
http://msdn.microsoft.com/zh-cn/library/aa394080(v = vs.85).aspx
这篇关于如何确定.NET中的CPU缓存大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文