Discussion:
[Rcpp-devel] Question on performance
Jordi Molins
2018-09-22 07:02:38 UTC
Permalink
Hello, I am new in this distribution list. I am using Rcpp for my project,
and I enjoy the Rcpp implementation quite a lot.

I have read the paper
https://www.sas.upenn.edu/~jesusfv/Update_March_23_2018.pdf and I see the
performance of Rcpp is about 4x times slower than C++ proper code, or 3x
times slower than Numba/Cython Python code.

Do you think that these figures are realistic? Of course, performance
calculations are always dependent on many factors (very often, not
explicit). But I am surprised that Rcpp is that slow in comparison to C++
code. I do not understand that Rcpp may be so slow, unless there are many
calls back and forth between Rcpp and R. The same for compiled Python code
vis a vis Rcpp.

Or maybe I am wrong?

Jordi Molins i Coronado
+34 69 38 000 59
Georgi Boshnakov
2018-09-22 09:18:01 UTC
Permalink
Hi,

I don’t see any code going with the revised version, but
looking at the code coming with the original paper, see http://economics.sas.upenn.edu/~jesusfv/RBC_codes.zip,
the following points come up:

1)There is no Rcpp code, so there is no way to verify the claims about Rcpp.

2)It is not at all obvious that the comparison is about computation – the following chunk for diagnostic printing is inside a double for loop (this is the .R version):

===
iteration = iteration+1;
if ((iteration %% 10)==0 | iteration ==1){
cat(" Iteration = ", iteration," Sup Diff = ", maxDifference,"\n");
}

===

Printing to the R console may well take significant proportion of the time.


3)The R version is a direct translation of the cpp version. This in itself is not a fair comparison. Moreover, note that the snipped in the cpp version of the above chunk uses the ‘||’ operator, while the R chunk above for some reason uses the ‘|’ operator, so the second condition in the ‘if’ statement is executed at every iteration.

4)The assignments of the data are done at compile time, while in R at execution time. Moreover there are man redundant operations in the R code, eg unnecessary conversions by as.matrix in the R code, which are absent in the cpp code.

5)The cpp function is for fixed data. Hence, the compilation and linking time need to be added to the overall time.

In summary, I would say that these results show, at best, that if a C++ function is converted to an interpreted language it will be slower, hardly surprising. The exact timings do not seem relevant since they mix computation and output. There is also evidence that the authors have been more careful with their C++ code than the R code (I haven’t looked at the others). The claim about Rcpp is completely unverifiable. Moreover, for a fixed task, the time with the C++ executable should have included compilation, linking and invoking the executable.

I would ignore these comparisons and look at more reliable sources, such as examples by Dirk in the Rcpp package and other materials and papers by him.


Georgi Boshnakov

From: Rcpp-devel [mailto:rcpp-devel-***@lists.r-forge.r-project.org] On Behalf Of Jordi Molins
Sent: 22 September 2018 08:03
To: rcpp-***@lists.r-forge.r-project.org; Jordi Molins
Subject: [Rcpp-devel] Question on performance

Hello, I am new in this distribution list. I am using Rcpp for my project, and I enjoy the Rcpp implementation quite a lot.

I have read the paper https://www.sas.upenn.edu/~jesusfv/Update_March_23_2018.pdf and I see the performance of Rcpp is about 4x times slower than C++ proper code, or 3x times slower than Numba/Cython Python code.

Do you think that these figures are realistic? Of course, performance calculations are always dependent on many factors (very often, not explicit). But I am surprised that Rcpp is that slow in comparison to C++ code. I do not understand that Rcpp may be so slow, unless there are many calls back and forth between Rcpp and R. The same for compiled Python code vis a vis Rcpp.

Or maybe I am wrong?

Jordi Molins i Coronado
+34 69 38 000 59
Dirk Eddelbuettel
2018-09-22 13:11:40 UTC
Permalink
Jordi,

On 22 September 2018 at 09:18, Georgi Boshnakov wrote:
| I don’t see any code going with the revised version, but
| looking at the code coming with the original paper, see http://economics.sas.upenn.edu/~jesusfv/RBC_codes.zip,
| the following points come up:

All excellent points. Jordi, you should ask for the code. It really sounds
like something done either naively, or with a goal in mind. "Rcpp code is
C++ code" so any "N times" is silly -- setup costs, extra copies, ... etc pp.

We may of course copy at the end from, say, bespoke C++ data structures to
ours. But for any non-trivial example the cost of that will be well under, say, 1%
while still getting a "many times" speedup over alternative R solutions. So
a win for most people. Hence 1444 packages on CRAN using it. Those people
aren't all fools ...

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Kyle Baron
2018-09-22 16:56:32 UTC
Permalink
Post by Dirk Eddelbuettel
But for any non-trivial example the cost of that will be well under, say,
1%
while still getting a "many times" speedup over alternative R solutions.
So
a win for most people. Hence 1444 packages on CRAN using it. Those people
aren't all fools ...
I try to think of the bigger picture here too. My package relies
on Rcpp (and FORTRAN) to simulate data ... potentially millions of rows in
a data
frame (or matrix). But we also need to do things with that data, like
summarize
it, make plots, generate summary tables in TeX and the like.

If I simulated in a stand-alone C++ program, I *might* save some time ...
maybe several
seconds. But then I'd either need to generate the summaries, plots, tables
in my C++
program (no thanks) or read all of that data into a program like ... R.
Well, there
goes any time savings I might have gained plus it's way less convenient.

That's why we use Rcpp: inputs get generated and formatted in R, I get fast
computation, and the results come back in memory and I never have to read a
ton of
data in to keep going with the analysis.

So everyone has different problems to solve and whatever we might farm out
to
compiled code is just one piece of the puzzle / picture. For what I do,
R/Rcpp
is hands-down winner to get me from start to finish.

Kyle
Iñaki Ucar
2018-09-22 17:15:16 UTC
Permalink
FYI, you can find the code here:

https://github.com/jesusfv/Comparison-Programming-Languages-Economics


El sáb., 22 sept. 2018 a las 15:11, Dirk Eddelbuettel
Post by Dirk Eddelbuettel
Jordi,
| I don’t see any code going with the revised version, but
| looking at the code coming with the original paper, see http://economics.sas.upenn.edu/~jesusfv/RBC_codes.zip,
All excellent points. Jordi, you should ask for the code. It really sounds
like something done either naively, or with a goal in mind. "Rcpp code is
C++ code" so any "N times" is silly -- setup costs, extra copies, ... etc pp.
We may of course copy at the end from, say, bespoke C++ data structures to
ours. But for any non-trivial example the cost of that will be well under, say, 1%
while still getting a "many times" speedup over alternative R solutions. So
a win for most people. Hence 1444 packages on CRAN using it. Those people
aren't all fools ...
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Iñaki Ucar
Dirk Eddelbuettel
2018-09-22 17:31:51 UTC
Permalink
On 22 September 2018 at 19:15, Iñaki Ucar wrote:
| FYI, you can find the code here:
|
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics

Nice catch!

Have not had time to look. Any volunteers willing to dive in, ie fork or
clone the thing and clean up? Happy to help.

Dirk

|
| El sáb., 22 sept. 2018 a las 15:11, Dirk Eddelbuettel
| (<***@debian.org>) escribió:
| >
| >
| > Jordi,
| >
| > On 22 September 2018 at 09:18, Georgi Boshnakov wrote:
| > | I don’t see any code going with the revised version, but
| > | looking at the code coming with the original paper, see http://economics.sas.upenn.edu/~jesusfv/RBC_codes.zip,
| > | the following points come up:
| >
| > All excellent points. Jordi, you should ask for the code. It really sounds
| > like something done either naively, or with a goal in mind. "Rcpp code is
| > C++ code" so any "N times" is silly -- setup costs, extra copies, ... etc pp.
| >
| > We may of course copy at the end from, say, bespoke C++ data structures to
| > ours. But for any non-trivial example the cost of that will be well under, say, 1%
| > while still getting a "many times" speedup over alternative R solutions. So
| > a win for most people. Hence 1444 packages on CRAN using it. Those people
| > aren't all fools ...
| >
| > Dirk
| >
| > --
| > http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
| > _______________________________________________
| > Rcpp-devel mailing list
| > Rcpp-***@lists.r-forge.r-project.org
| > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
|
|
|
| --
| Iñaki Ucar
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Michael Weylandt
2018-09-22 17:36:37 UTC
Permalink
I don't have time right now, but the offending line is here:

https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62

which calls `sourceCpp` repeatedly on each loop iteration.

Should be a one line change to move it outside the loop and un-sully the
good name of Rcpp.

M
Post by Dirk Eddelbuettel
|
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics
Nice catch!
Have not had time to look. Any volunteers willing to dive in, ie fork or
clone the thing and clean up? Happy to help.
Dirk
|
| El sáb., 22 sept. 2018 a las 15:11, Dirk Eddelbuettel
| >
| >
| > Jordi,
| >
| > | I don’t see any code going with the revised version, but
| > | looking at the code coming with the original paper, see
http://economics.sas.upenn.edu/~jesusfv/RBC_codes.zip,
| >
| > All excellent points. Jordi, you should ask for the code. It really sounds
| > like something done either naively, or with a goal in mind. "Rcpp code is
| > C++ code" so any "N times" is silly -- setup costs, extra copies, ... etc pp.
| >
| > We may of course copy at the end from, say, bespoke C++ data structures to
| > ours. But for any non-trivial example the cost of that will be well under, say, 1%
| > while still getting a "many times" speedup over alternative R solutions. So
| > a win for most people. Hence 1444 packages on CRAN using it. Those people
| > aren't all fools ...
| >
| > Dirk
| >
| > --
| > _______________________________________________
| > Rcpp-devel mailing list
| > https://lists.r-forge.r-project.org/cgi-bin/mailman/
listinfo/rcpp-devel
|
|
|
| --
| Iñaki Ucar
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Dirk Eddelbuettel
2018-09-22 18:21:33 UTC
Permalink
On 22 September 2018 at 12:36, Michael Weylandt wrote:
| I don't have time right now, but the offending line is here:
|
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and un-sully the
| good name of Rcpp.

Nice catch!! You should _definitely_ send him a PR. I also suggest to move
the sourceCpp() outside the timed segment as I do not think he accounts for
compilation in the other approaches (eg C++).

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Kyle Baron
2018-09-23 10:38:53 UTC
Permalink
It seems like the sourceCpp thing is the biggie. That dropped about a
second of the time from something like 2.7 to 1.7 seconds

They also create mOutput and then re-create it every time it goes into
InsideLoop
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12

I just passed mOutput into InsideLoop and modified it there
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
Post by Michael Weylandt
|
|
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and un-sully the
| good name of Rcpp.
Nice catch!! You should _definitely_ send him a PR. I also suggest to move
the sourceCpp() outside the timed segment as I do not think he accounts for
compilation in the other approaches (eg C++).
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Kyle Baron | Principal Scientist I

Metrum Research Group

2 Tunxis Road | Tariffville, CT 06081

p: 860-735-7043 ext. 202 | e: ***@metrumrg.com

<https://info.metrumrg.com/contact>
<https://www.linkedin.com/company/metrum-research-group-llc/>
<https://twitter.com/MetrumRG>
Kyle Baron
2018-09-23 10:45:34 UTC
Permalink
I got mOutput mixed up with mResults in the last message; it mResults that
gets created up front and then every iteration.

https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L45
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
Post by Kyle Baron
It seems like the sourceCpp thing is the biggie. That dropped about a
second of the time from something like 2.7 to 1.7 seconds
They also create mOutput and then re-create it every time it goes into
InsideLoop
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
I just passed mOutput into InsideLoop and modified it there
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
Post by Michael Weylandt
|
|
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and un-sully the
| good name of Rcpp.
Nice catch!! You should _definitely_ send him a PR. I also suggest to move
the sourceCpp() outside the timed segment as I do not think he accounts for
compilation in the other approaches (eg C++).
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Iñaki Ucar
2018-09-23 11:01:13 UTC
Permalink
Also you can try adding

// [[Rcpp::plugins(unwindProtect)]]

to InsideLoop.cpp. That should boost the performance too with the
latest version of Rcpp.

Iñaki
I got mOutput mixed up with mResults in the last message; it mResults that gets created up front and then every iteration.
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L45
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
It seems like the sourceCpp thing is the biggie. That dropped about a second of the time from something like 2.7 to 1.7 seconds
They also create mOutput and then re-create it every time it goes into InsideLoop
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
I just passed mOutput into InsideLoop and modified it there
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
Post by Dirk Eddelbuettel
|
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and un-sully the
| good name of Rcpp.
Nice catch!! You should _definitely_ send him a PR. I also suggest to move
the sourceCpp() outside the timed segment as I do not think he accounts for
compilation in the other approaches (eg C++).
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Iñaki Ucar
Dirk Eddelbuettel
2018-09-23 12:39:00 UTC
Permalink
On 23 September 2018 at 13:01, Iñaki Ucar wrote:
| Also you can try adding
|
| // [[Rcpp::plugins(unwindProtect)]]
|
| to InsideLoop.cpp. That should boost the performance too with the
| latest version of Rcpp.

Maybe, maybe not. Are there lots of calls happening here? Should be easy
enough to add and then (micro)benchmark() from R.

Another endeavour may be to take the rewritten C++ version by MattDB in
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_CPP_2.cpp
and wrap that.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Iñaki Ucar
2018-09-23 12:50:52 UTC
Permalink
Post by Dirk Eddelbuettel
| Also you can try adding
|
| // [[Rcpp::plugins(unwindProtect)]]
|
| to InsideLoop.cpp. That should boost the performance too with the
| latest version of Rcpp.
Maybe, maybe not. Are there lots of calls happening here? Should be easy
enough to add and then (micro)benchmark() from R.
The Rcpp function is called inside a loop, so I would expect some gain.

Iñaki
Post by Dirk Eddelbuettel
Another endeavour may be to take the rewritten C++ version by MattDB in
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_CPP_2.cpp
and wrap that.
Dirk
--
Dirk Eddelbuettel
2018-09-23 13:19:54 UTC
Permalink
On 23 September 2018 at 14:50, Iñaki Ucar wrote:
| El dom., 23 sept. 2018 14:39, Dirk Eddelbuettel <***@debian.org> escribió:
|
| >
| > On 23 September 2018 at 13:01, Iñaki Ucar wrote:
| > | Also you can try adding
| > |
| > | // [[Rcpp::plugins(unwindProtect)]]
| > |
| > | to InsideLoop.cpp. That should boost the performance too with the
| > | latest version of Rcpp.
| >
| > Maybe, maybe not. Are there lots of calls happening here? Should be easy
| > enough to add and then (micro)benchmark() from R.
| >
|
| The Rcpp function is called inside a loop, so I would expect some gain.

True, but alternatively any one of us with a modicum of Rcpp and C++ skill
could wrap the whole subroutine so that only one call is needed.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Kyle Baron
2018-09-23 13:29:49 UTC
Permalink
It didn't change anything in my testing. I wondered about wrapping the
whole thing too ... but maybe that's premature? I can't tell were the
slow-down is.

My ratio is around 2.4 or 2.5 with this:

https://github.com/jesusfv/Comparison-Programming-Languages-Economics/compare/master...kylebmetrum:master
Post by Dirk Eddelbuettel
|
| >
| > | Also you can try adding
| > |
| > | // [[Rcpp::plugins(unwindProtect)]]
| > |
| > | to InsideLoop.cpp. That should boost the performance too with the
| > | latest version of Rcpp.
| >
| > Maybe, maybe not. Are there lots of calls happening here? Should be easy
| > enough to add and then (micro)benchmark() from R.
| >
|
| The Rcpp function is called inside a loop, so I would expect some gain.
True, but alternatively any one of us with a modicum of Rcpp and C++ skill
could wrap the whole subroutine so that only one call is needed.
Dirk
--
Dirk Eddelbuettel
2018-09-23 13:59:22 UTC
Permalink
On 23 September 2018 at 08:29, Kyle Baron wrote:
| It didn't change anything in my testing. I wondered about wrapping the
| whole thing too ... but maybe that's premature? I can't tell were the
| slow-down is.
|
| My ratio is around 2.4 or 2.5 with this:
|
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics/compare/master...kylebmetrum:master

I think it is worth poking around. Ie for C++ (either original or MattPD
version) just takes the clock at beginning and end. _Arguably we could do
the same inside the Rcpp call_ as he assumes away startup cost of the binary
etc pp.

Anything that leaves a whole factor on the table does something weird. Maybe
we can learn something, maybe something odd is going on. But we should be
ball-park comparable to other _compiled code_ examples.

Dirk
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Iñaki Ucar
2018-09-24 07:24:07 UTC
Permalink
Post by Iñaki Ucar
Also you can try adding
// [[Rcpp::plugins(unwindProtect)]]
to InsideLoop.cpp. That should boost the performance too with the
latest version of Rcpp.
Taking a look now, and I realize there are just calls to C++, not to R. So,
please, forget about what I've said. The dangers of skimming through code
in a mobile device and speaking too soon.

Iñaki
Post by Iñaki Ucar
Iñaki
Post by Kyle Baron
I got mOutput mixed up with mResults in the last message; it mResults
that gets created up front and then every iteration.
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L45
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
Post by Kyle Baron
Post by Kyle Baron
It seems like the sourceCpp thing is the biggie. That dropped about a
second of the time from something like 2.7 to 1.7 seconds
Post by Kyle Baron
Post by Kyle Baron
They also create mOutput and then re-create it every time it goes into
InsideLoop
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
Post by Kyle Baron
Post by Kyle Baron
I just passed mOutput into InsideLoop and modified it there
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
|
|
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and un-sully
the
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
| good name of Rcpp.
Nice catch!! You should _definitely_ send him a PR. I also suggest to
move
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
the sourceCpp() outside the timed segment as I do not think he
accounts for
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
compilation in the other approaches (eg C++).
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Post by Kyle Baron
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Iñaki Ucar
Kyle Baron
2018-09-24 13:48:45 UTC
Permalink
Here's an updated version of the Rcpp code with a single call:
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/compare/master...kylebmetrum:master

The comparison runs are here:
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/issues/1

For typical a single run ....

Original Rcpp: 2.762
New Rcpp: 0.878
testclang: 0.686582


Original Rcpp / testclang: 4.026239
New Rcpp / testclang: 1.279883

Moving sourceCpp was a big improvement as suggested. Also, changing from
parens to brackets to access NumericVector sped things up. And some
improvement too from re-factoring so that there was just a
single call to the compiled code.

Kyle
Post by Iñaki Ucar
Post by Iñaki Ucar
Also you can try adding
// [[Rcpp::plugins(unwindProtect)]]
to InsideLoop.cpp. That should boost the performance too with the
latest version of Rcpp.
Taking a look now, and I realize there are just calls to C++, not to R.
So, please, forget about what I've said. The dangers of skimming through
code in a mobile device and speaking too soon.
Iñaki
Post by Iñaki Ucar
Iñaki
Post by Kyle Baron
I got mOutput mixed up with mResults in the last message; it mResults
that gets created up front and then every iteration.
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L45
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
Post by Kyle Baron
Post by Kyle Baron
It seems like the sourceCpp thing is the biggie. That dropped about a
second of the time from something like 2.7 to 1.7 seconds
Post by Kyle Baron
Post by Kyle Baron
They also create mOutput and then re-create it every time it goes into
InsideLoop
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
Post by Kyle Baron
Post by Kyle Baron
I just passed mOutput into InsideLoop and modified it there
https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
|
|
https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
|
| which calls `sourceCpp` repeatedly on each loop iteration.
|
| Should be a one line change to move it outside the loop and
un-sully the
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
| good name of Rcpp.
Nice catch!! You should _definitely_ send him a PR. I also suggest
to move
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
the sourceCpp() outside the timed segment as I do not think he
accounts for
Post by Kyle Baron
Post by Kyle Baron
Post by Dirk Eddelbuettel
compilation in the other approaches (eg C++).
Dirk
--
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Post by Kyle Baron
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Iñaki Ucar
Dirk Eddelbuettel
2018-09-24 14:20:17 UTC
Permalink
On 24 September 2018 at 08:48, Kyle Baron wrote:
| Here's an updated version of the Rcpp code with a single call:
| https://github.com/jesusfv/Comparison-Programming-Languages-Economics/compare/master...kylebmetrum:master
|
| The comparison runs are here:
| https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/issues/1

Very nice! Will you file a PR?

| For typical a single run ....
|
| Original Rcpp: 2.762
| New Rcpp: 0.878
| testclang: 0.686582
|
|
| Original Rcpp / testclang: 4.026239
| New Rcpp / testclang: 1.279883
|
| Moving sourceCpp was a big improvement as suggested. Also, changing from
| parens to brackets to access NumericVector sped things up. And some
| improvement too from re-factoring so that there was just a
| single call to the compiled code.

Still puzzled by the remaining 27% ...

Dirk
|
| Kyle
|
|
|
| On Mon, Sep 24, 2018 at 2:24 AM Iñaki Ucar <***@fedoraproject.org> wrote:
|
| >
| >
| > El dom., 23 sept. 2018 13:01, Iñaki Ucar <***@fedoraproject.org>
| > escribió:
| >
| >> Also you can try adding
| >>
| >> // [[Rcpp::plugins(unwindProtect)]]
| >>
| >> to InsideLoop.cpp. That should boost the performance too with the
| >> latest version of Rcpp.
| >>
| >
| > Taking a look now, and I realize there are just calls to C++, not to R.
| > So, please, forget about what I've said. The dangers of skimming through
| > code in a mobile device and speaking too soon.
| >
| > Iñaki
| >
| >
| >> Iñaki
| >
| >
| >> El dom., 23 sept. 2018 a las 12:46, Kyle Baron (<***@metrumrg.com>)
| >> escribió:
| >> >
| >> >
| >> > I got mOutput mixed up with mResults in the last message; it mResults
| >> that gets created up front and then every iteration.
| >> >
| >> >
| >> https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L45
| >> >
| >> https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
| >> >
| >> >
| >> >
| >> >
| >> >
| >> > On Sun, Sep 23, 2018 at 5:38 AM Kyle Baron <***@metrumrg.com> wrote:
| >> >>
| >> >>
| >> >> It seems like the sourceCpp thing is the biggie. That dropped about a
| >> second of the time from something like 2.7 to 1.7 seconds
| >> >>
| >> >> They also create mOutput and then re-create it every time it goes into
| >> InsideLoop
| >> >>
| >> https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L41
| >> >>
| >> https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/InsideLoop.cpp#L12
| >> >>
| >> >> I just passed mOutput into InsideLoop and modified it there
| >> >>
| >> https://github.com/kylebmetrum/Comparison-Programming-Languages-Economics/commit/89015a72a0cf673739e83e38d4a545db4886b879
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >>
| >> >> On Sat, Sep 22, 2018 at 1:21 PM Dirk Eddelbuettel <***@debian.org>
| >> wrote:
| >> >>>
| >> >>>
| >> >>> On 22 September 2018 at 12:36, Michael Weylandt wrote:
| >> >>> | I don't have time right now, but the offending line is here:
| >> >>> |
| >> >>> |
| >> https://github.com/jesusfv/Comparison-Programming-Languages-Economics/blob/master/RBC_Rcpp.R#L62
| >> >>> |
| >> >>> | which calls `sourceCpp` repeatedly on each loop iteration.
| >> >>> |
| >> >>> | Should be a one line change to move it outside the loop and
| >> un-sully the
| >> >>> | good name of Rcpp.
| >> >>>
| >> >>> Nice catch!! You should _definitely_ send him a PR. I also suggest
| >> to move
| >> >>> the sourceCpp() outside the timed segment as I do not think he
| >> accounts for
| >> >>> compilation in the other approaches (eg C++).
| >> >>>
| >> >>> Dirk
| >> >>>
| >> >>> --
| >> >>> http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
| >> >>> _______________________________________________
| >> >>> Rcpp-devel mailing list
| >> >>> Rcpp-***@lists.r-forge.r-project.org
| >> >>>
| >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| >> >>
| >> >>
| >> >>
| >> >>
| >> > _______________________________________________
| >> > Rcpp-devel mailing list
| >> > Rcpp-***@lists.r-forge.r-project.org
| >> > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| >>
| >>
| >>
| >> --
| >> Iñaki Ucar
| >>
| >
--
http://dirk.eddelbuettel.com | @eddelbuettel | ***@debian.org
Continue reading on narkive:
Loading...