| 
 | 
 
class CPUTest 
    { 
        //获取系统运行时间毫秒级别 
        [DllImport("kernel32.dll")] 
        static extern uint GetTickCount(); 
 
        //SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask 
        [DllImport("kernel32.dll")] 
        static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,UIntPtr dwThreadAffinityMask); 
 
        //得到当前线程的handler 
        [DllImport("kernel32.dll")] 
        static extern IntPtr GetCurrentThread(); 
 
        static int busyTime = 10; 
        static int idleTime = busyTime; 
        //设置线程数目 
        static int threads = 8; 
 
        public static void changeValue(object pid) 
        { 
 
            int core = (int)pid; 
            int len = 100000000; 
            uint[] data = new uint[len]; 
 
            //将当前线程绑定到指定的cpu核心上 
            SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(SetCpuID(core))); 
            Console.WriteLine("运行线程:" + SetCpuID(core).ToString()); 
            uint startTime = GetTickCount(); 
            Stopwatch stopwatch = new Stopwatch(); 
            stopwatch.Start(); 
 
            for (int i = 0; i < len; ++i) 
            { 
                data[i] = GetTickCount(); 
                //   Thread.Sleep(10); 
            } 
 
 
            stopwatch.Stop(); 
            Console.WriteLine("运行时间" + stopwatch.ElapsedMilliseconds.ToString()); 
 
        } 
        //获取cpu的id号 
        static ulong SetCpuID(int id) 
        { 
            ulong cpuid = 0; 
            if (id < 0 || id >= System.Environment.ProcessorCount) 
            { 
                id = 0; 
            } 
            cpuid |= 1UL << id; 
 
            return cpuid; 
        } 
 
        public static void Test() 
        { 
            //ManagementClass c = new ManagementClass(new ManagementPath("Win32_Processor")); 
            //ManagementObjectCollection moc = c.GetInstances(); 
 
            //foreach (ManagementObject mo in moc) 
            //{ 
            //    PropertyDataCollection properties = mo.Properties; 
            //    //获取内核数代码 
            //    string cpu = "物理内核数:" + properties["NumberOfCores"].Value + "\r"; 
            //    string total = "逻辑内核数:" + properties["NumberOfLogicalProcessors"].Value + "\r"; 
            //    Console.WriteLine(cpu); 
            //    //其他属性获取代码 
            //    //foreach (PropertyData property in properties) 
            //    //{ 
            //    //    Console.WriteLine( property.Name + ":" + property.Value + "\r"); 
            //    //} 
            //} 
            //线程数目 
            int num = threads; 
            Console.WriteLine("线程数目" + num); 
            Stopwatch stopwatch = new Stopwatch(); 
            stopwatch.Start(); 
            List<Task> tasks = new List<Task>(); 
            for (int i = 3; i < num; i++) 
            { 
                int k = i; 
                tasks.Add(Task.Run(() => 
                { 
                    changeValue(k); 
                })); 
            } 
            Task.WaitAll(tasks.ToArray()); 
            stopwatch.Stop(); 
            Console.WriteLine("总运行时间" + stopwatch.ElapsedMilliseconds.ToString()); 
            Console.ReadKey(); 
        } 
    } |   
 
 
 
 |