0

I have a Rcpp code inside of which I have a message to be printed out as:

"The no. of iteration is ---" after each 100 iteration. For this I used the code:

 Rcout << "The number of iteration is : " << t << "\n";

The problem with this code is that it delays to show the message and shows all of it after the program ends. How can I solve this?

gultu
  • 143
  • 10
  • @coatless, adding printing message slows down the code? According to you, I should remove the print trace. – gultu Nov 06 '17 at 16:22
  • Don't add print traces. This is especially the case if you are doing any form of parallelization. _R_ is single threaded. By adding a print trace inside a parallel segment bad things become possible. In addition, print traces will _slow_ down your routine as _C++_ has to return control to _R_ to print to console before resuming. – coatless Nov 06 '17 at 16:24
  • @coatless, yes I am using parallelisation. Okay, then I am removing the print traces. But I wanted to trace the speed of my code. How can I do that inside Rcpp then? – gultu Nov 06 '17 at 16:26
  • If you want to output something when using parallelism, use ``makeCluster(no_cores, outfile = "")``. – F. Privé Nov 06 '17 at 19:53
  • @F.Privé I just want to print the no. of iterations. Shall I use "makeCluster" for that? – gultu Nov 07 '17 at 11:09
  • @coatless, Surprisingly I found that the code without any print message takes longer than that with print message. Could you please explain what kind of bad things may happen with print message (except : R is single threaded)? – gultu Nov 07 '17 at 12:09
  • It would be easier to answer if you provided a reproducible example with for example, the way you parallelize. – F. Privé Nov 07 '17 at 14:55
  • @gultu This information is found on [RcppParellel's Thread Safety Discussion](https://rcppcore.github.io/RcppParallel/#thread_safety). Prior discussion on printing folies can be found here: https://stackoverflow.com/questions/37676929/why-rcppwarning-is-slower-than-r-warning – coatless Nov 07 '17 at 17:00

0 Answers0