I need to profile the time taken by some functions (more than 1ms execution time) to run, but on production, not on development like the solutions I found.
Of course I could do:
time_before = get_time_ns();
my_function();
time_after = get_time_ns();
difference = time_after - time_before;
but this is boring to do on lots of functions. I thought about a macro that inserts that before and after the function, and then I could simply do
profile!(my_function(arg1, arg2, ...));
but how should I store the returned value? I think there would also be problems if the call is of the type:
profile!(object.my_function(arg1, arg2, ...))
Is it possible to create a macro that does what I want? Actually I don't think I even need to match for functions speficically, maybe match against the literal text inside the macro, an then simple execute this text as a line between the time measurements.
There's also the problem of when I need to measure
let r = object.my_function(arg1, arg2, ...)
but I think this could be translated to
let r = {
time_before = get_time_ns();
let r = my_function();
time_after = get_time_ns();
difference = time_after - time_before;
r
}
but there's still the issue of returning the time difference. Returning a pair of the function result + time difference is ok but not the best. Maybe the macro could have 2 arguments:
profile!(variable_name, object.my_function(arg1, arg2, ...))
which translates to
let variable_name: std::time::Duration;
let r = {
time_before = get_time_ns();
let r = my_function();
time_after = get_time_ns();
variable_name = time_after - time_before;
r
}
Anyone has a better idea? And is it possible to implement this with macros?