[phobos] Is it possible to get Function Time in ticks like profiler give, but at runtime?

Jonathan M Davis jmdavisProg at gmx.com
Sat Aug 20 16:58:26 PDT 2011

On Saturday, August 20, 2011 14:10:44 unDEFER wrote:
> Hello, again!
> I want one very strange feature. I even don't know if it is really
> possible with C for any OS.
> But I sure that in D it must be not more complicate to get this
> information.
> So I want to get how much ticks of CPU was wasted on some function
> (including all calls) in real time.
> Seems profiler give this information but only when the program was end.
> Is it possible to get the same at runtime?
> Also is it possible to get profiler info from other process before end of
> program?
> I know with core.time or std.c.time I can measure real time of function
> execution, but it will too much different depend on system loading.
> But I want to get approximately the same numbers at the same point of
> program.
> Thank you in advance!
> Sorry if I send this question to not right mail list.

If you're looking to time how long a function takes to call, then use 
std.datetime.benchmark. If you're looking to determine how much your specific 
program calls a particular function and how much time it spends in that 
function, then compile with -profile, run the program, and then look at the 
files that it generates. If you want to do the equivalent of -profile at 
runtime, I don't think that that's really possible, and I'm not quite sure why 
you'd want to. But even if you could, it would affect the performance of the 
program and probably skew the results anyway. It would certainly slow down the 
program a lot. I suppose that you could write a wrapper function for your 
specific function, time the exact time that it takes on each call with 
std.datetime.StopWatch, and then send that information to another process via 
std.socket if you really want something like that. But that sort of thing 
sounds rather complicated to me, and I have no idea what good the information 
would do you at runtime anyway. Usually, the reason that people want to profile 
code is to determine what code they need to optimize make the program faster, 
and -profile is plenty for that.

- Jonathan M Davis

More information about the phobos mailing list