This is how common profiling tools (such as gprof) actually work.
"Profiling also involves watching your program as it runs, and keeping a histogram of where the program counter happens to be every now and then."
"The run-time figures that gprof gives you are based on a sampling process, so they are subject to statistical inaccuracy."
-- http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html#SEC11