几何尺寸与公差论坛

 找回密码
 注册
查看: 683|回复: 0

算法加指定核运行

[复制链接]
发表于 2023-4-27 17:18:44 | 显示全部楼层 |阅读模式
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();
        }
    }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-5-7 06:32 , Processed in 0.034655 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表