0

I am trying to find how long a specific subset of code takes in cmprsk and so I would like to place t0 = Sys.time() at one location, t1 = Sys.time() in another location, and then print out the difference via cat(sprintf("time taken %f seconds", as.numeric(t1-t0, units="secs")).

I know from print(as.list(body(crr))) that the positions for timing would be at=2 and at=28. I use the following lines to place the traces in:

trace(crr, 't0 = Sys.time();', at=2);
trace(crr, 't1 = Sys.time();', at=28);
trace(crr, 'cat(sprintf("time taken = %f", as.numeric(t1-t0, units="secs")));', at=40);

However, when I run the code, it looks like only the last trace is being used. When I run the script and grep for Trace, I get the following:

Loading required package: splines
(loaded the methods namespace)
Tracing function "crr" in package "cmprsk"
Tracing function "crr" in package "cmprsk"
Tracing function "crr" in package "cmprsk"
    .doTrace("cat(sprintf(\"time taken = %f\", as.numeric(t1-t0, units=\"secs\")));",

Does this indicate that only the last trace is being kept? If so, how do I put more than one trace in the original function?

drjrm3
  • 4,474
  • 10
  • 53
  • 91
  • Might you be able to use `trace(crr, edit=TRUE)`? – Josh O'Brien Feb 08 '16 at 21:47
  • Not quite what I was hoping for. This opens up the function and allows me to make edits. I can make the edits to the original source code, but I am specifically looking for a way to have another user run this and and see the effects without saying "edit this line here, and this line here, and this line here, etc." which I think is what `edit=TRUE` does, right? – drjrm3 Feb 08 '16 at 21:50
  • Yup, that's what it does, so it won't fix your problem. You may just need to make those edits to the sources and then compile the modified package to send to the other user. (If the function were unexported from the namespace, you could use `assignInNamespace()`, [like this](http://stackoverflow.com/questions/15505607/diagonal-labels-orientation-on-x-axis-in-heatmaps/15506652#15506652), but there doesn't seem to be any equivalent for exported functions.) – Josh O'Brien Feb 08 '16 at 22:18

0 Answers0