I wrote something that does this (to a certain extent), and posted it here -- it was originally intended for big apps that are split into multiple source files, but it works fine on a single script.
Comment on Re: Profiling Code (Subroutine Documenting.)