I have some C-Sharp code, running on MacOS(Sequoia), and I measure its resource usage by calling these two functions before and after, and subtracting.
public static long GetClockTime()
{
var timespan = DateTime.Now - DateTime.MinValue;
return (long)timespan.TotalMilliseconds;
}
public static long GetCpuTime()
{
return (long)Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
}
In one particular case, in which I'm 99% sure that no i/o is happening, it's reporting 30 seconds of clock time and 1 second of CPU time. At a guess, 30 seconds of CPU time seems about right.
Any guesses as to why GetCpuTime is reporting a number that is too low?
If my code spawns more threads, which do the actual work, would that time get lost?
Is there a more correct way to get the total cpu?