Skip to content

Instantly share code, notes, and snippets.

@fresky
Last active September 3, 2022 05:59
Show Gist options
  • Save fresky/5734434 to your computer and use it in GitHub Desktop.
Save fresky/5734434 to your computer and use it in GitHub Desktop.
Time/Memory watcher for C#
public static double TimeWatcher(Action action)
{
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
action();
watch.Stop();
var useTime = (double) watch.ElapsedMilliseconds/1000;
return useTime;
}
public static long MemoryWatcher(Action action)
{
long start = GC.GetTotalMemory(true);
action();
GC.Collect();
GC.WaitForFullGCComplete();
long end = GC.GetTotalMemory(true);
long useMemory = (end - start)/(1024*1024);
return useMemory;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment