code_junkie

Name of the process with highest cpu usage

Nov 10th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. Get-Process | Sort-Object CPU -desc | Select-Object -first 3 | Format-Table | CPU,ProcessName -hidetableheader
  2.  
  3. 16.8041632 System
  4. 12.568072 csrss
  5. 11.9872368 powershell
  6.  
  7. using System.Diagnostics;
  8.  
  9. float GetAverageCPULoad(int procID, DateTme from, DateTime, to)
  10. {
  11. // For the current process
  12. //Process proc = Process.GetCurrentProcess();
  13. // Or for any other process given its id
  14. Process proc = Process.GetProcessById(procID);
  15. System.TimeSpan lifeInterval = (to - from);
  16. // Get the CPU use
  17. float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds /
  18. lifeInterval.TotalMilliseconds) * 100;
  19. // You need to take the number of present cores into account
  20. return CPULoad / System.Environment.ProcessorCount;
  21. }
  22.  
  23. class ProcLoad
  24. {
  25. // Last time you checked for a process
  26. public Dictionary<int, DateTime> lastCheckedDict = new Dictionary<int, DateTime>();
  27.  
  28. public float GetCPULoad(int procID)
  29. {
  30. if (lastCheckedDict.ContainsKey(procID))
  31. {
  32. DateTime last = lastCheckedDict[procID];
  33. lastCheckedDict[procID] = DateTime.Now;
  34. return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);
  35. }
  36. else
  37. {
  38. lastCheckedDict.Add(procID, DateTime.Now);
  39. return 0;
  40. }
  41. }
  42. }
  43.  
  44. Process.TotalProcessorTime
Add Comment
Please, Sign In to add comment