Scope Guard in C -
i use scope guard in c in order profiling.
i know how time spend in function. here do:
int function() { tic(); ... stuff ... if (something) { toc(); return 0; } toc(); return 1; }
i need place toc statement each time exit function. without having copy paste toc everywhere. there generic way that, using macro or ? don't want change way function called, there many functions have profile.
thanks
this doesn't change way function called. not use if want able profile every single function, though.
static inline int real_function() { // previous contents of function(), no tic or toc } int function() { tic(); int r = real_function(); toc(); return r; }
as else says: use profiler, save lot of effort in long run. don't say: if platform has one.
if doesn't, easiest might (as coding rule) functions must have 1 exit point, , exit point must via macro. can manually instrument functions code @ entry , exit. legacy functions multiple returns can wrapped above.
also, bear in mind when you're doing compiler can mess up. might write this:
tic(); do_something(); int = something_else(); toc(); return i;
if compiler determines something_else has no side-effects, though something_else takes significant time, might turn code this:
tic(); do_something(); toc(); return something_else();
and profile data under-estimate time spent in function. reason it's have real profiler - can co-operate compiler.
Comments
Post a Comment