问题
I'm new to the language F# and currently I'm doing a short study on the F# performance. So what I would like to do is benchmark my F# code. I've already found the Stopwatch class, which was pretty obvious, but as for the rest of what you should be able to test, I found no clear leads.
What I would wish to do is to find out memory and cpu usage of the F# code when I run it. Is there anyone here that might be able to give me some advices on how to do this?
回答1:
For quick prototyping, fsi's timing directive provides good indications:
#if INTERACTIVE
#time "on"
#endif
It prints out something like:
Real: 00:00:00.199, CPU: 00:00:00.120, GC gen0: 0, gen1: 0, gen2: 0
Beside execution time, other statistics show amounts of garbage collected in three levels of the generational GC, which are helpful to understand memory usage in your programs.
For serious benchmarking, you should use Visual Studio profiler which provides function call counts, timing, and memory allocation measurement, etc or even use third-party memory profilers. You can find some hints of using these profilers in my answer at Good F# Performance Profiling Tool.
回答2:
You may also find this blog posting from Dave Thomas (formerly Moirae Software) helpful:
https://7sharp9.github.io/2011/12/11/2011-12-11-fixing-a-hole/
Normally I'd identify the specific portion of the blog posting that I'm thinking answers the question but the entire blog post is as an answer to this question.
EDIT: Updated link since old link was broken.
来源:https://stackoverflow.com/questions/10045591/how-to-measure-cpu-and-memory-usage-of-f-code