文章总结: 本文记录了利用AI解决微软面试题在任务管理器绘制正弦波的过程。作者通过AI生成C#代码,利用线程亲和性和忙闲循环控制CPU负载,并在参考《代码之美》代码后优化了波形平滑度。实验验证了AI在代码阅读、理解及跨语言迁移方面的能力,同时也指出了此类高精度CPU负载控制会导致系统操作卡顿的现象。 综合评分: 73 文章分类: 其他
让 AI 在任务管理器中画正弦波
原创
hyang0 hyang0
生有可恋
2026年1月24日 08:20 湖北
这是一道微软的面试题,看 AI 做得怎么样。
AI 实现的程序波形如下:
其中代码绑定在第一个核心上运行,如果让代码在所有核心上运行,表现为:
下面是从逻辑核切到整体CPU利用率的波形图,波形有小毛刺不太光滑。
AI 生成的 C# 代码为:
using System;using System.Diagnostics;using System.Runtime.InteropServices;using System.Threading;class Program{ static void Main() { var process = Process.GetCurrentProcess(); int coreCount = Environment.ProcessorCount; process.ProcessorAffinity = (IntPtr)((1L << coreCount) - 1); process.PriorityClass = ProcessPriorityClass.High; double periodSeconds = 60.0; int resolutionMs = 10; for (int i = 0; i < coreCount; i++) { int coreIndex = i; var t = new Thread(() => GenerateCpuLoad(periodSeconds, resolutionMs, coreIndex)); t.IsBackground = true; t.Priority = ThreadPriority.Highest; t.Start(); } Console.WriteLine("CPU 正在按正弦波负载运行,按 Enter 退出..."); Console.ReadLine(); } static void GenerateCpuLoad(double periodSeconds, int resolutionMs, int coreIndex) { BindCurrentThreadToCore(coreIndex); var sw = Stopwatch.StartNew(); while (true) { double t = sw.Elapsed.TotalSeconds; double u = (t % periodSeconds) / periodSeconds; double s = 2.0 * u; double x; double temp; double y; if (s < 1.0) { x = 2.0 * s - 1.0; temp = 1.0 - x * x; if (temp < 0.0) { temp = 0.0; } y = 0.5 + 0.5 * Math.Sqrt(temp); } else { double s2 = s - 1.0; x = 2.0 * s2 - 1.0; temp = 1.0 - x * x; if (temp < 0.0) { temp = 0.0; } y = 0.5 - 0.5 * Math.Sqrt(temp); } double cpuRatio = y; if (cpuRatio < 0.0) { cpuRatio = 0.0; } else if (cpuRatio > 1.0) { cpuRatio = 1.0; } int busyMs = (int)(resolutionMs * cpuRatio); int idleMs = resolutionMs - busyMs; var slice = Stopwatch.StartNew(); while (slice.ElapsedMilliseconds < busyMs) { } if (idleMs > 0) { Thread.Sleep(idleMs); } } } [DllImport("kernel32.dll")] static extern IntPtr GetCurrentThread(); [DllImport("kernel32.dll")] static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask); static void BindCurrentThreadToCore(int coreIndex) { if (coreIndex < 0 || coreIndex >= Environment.ProcessorCount) { return; } IntPtr handle = GetCurrentThread(); IntPtr mask = (IntPtr)(1L << coreIndex); SetThreadAffinityMask(handle, mask); }}
原始波形不完美,让AI 参考《代码之美》的正弦波代码,重新优化后的CPU波形如下:
差不多已经比较平滑了,对比以前的方案会大量占用CPU,在进行其它操作时系统会变卡,鼠标明显变得卡顿。
从代码理解能力上来说,用 AI 读代码或改写代码是可行的。可以用 AI 实现不同编码间的迁移,比如从 c++ 迁移到 C#。
参考的《代码之美》中的正弦波代码:
const double SPLIT = 0.01;const int COUNT = 200;const double PI = 3.14159265;const int INTERVAL = 300;int main(){ DWORD busySpan[COUNT]; //array of busy times DWORD idleSpan[COUNT]; //idleint half = INTERVAL/2;double radian = 0.0;for (int i = 0; i < COUNT; i++) { busySpan[i] = (DWORD)(half + sin(PI * radian) * half); idleSpan[i] = INTERVAL - busySpan[i]; radian += SPLIT; } DWORD startTime = 0;int j = 0;while(true) { j = j % COUNT; startTime = GetTickCount();while((GetTickCount() - startTime) <= busySpan[j]) ; Sleep(idleSpan[j]); j++; }return 0;}
给 AI 的提示词:
全文完。
免责声明:
本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。
任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。
本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我。
本文转载自:生有可恋 hyang0 hyang0《让 AI 在任务管理器中画正弦波》
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。











评论