From @zwj|08 @end|ng |rom gm@||@com  Wed Jun 19 07:42:31 2019
From: @zwj|08 @end|ng |rom gm@||@com (King Jiefei)
Date: Wed, 19 Jun 2019 01:42:31 -0400
Subject: [Rd] Fast way to call an R function from C++?
In-Reply-To: <CALEXWq3q3uVXKsiOE=XTT5UxEWbCPF38JNbB8H+RTyioQOEN9w@mail.gmail.com>
References: <CAGiFhPONJoEeWWdtTyHw=Y5or1ad3pXtydLNQyONzU3FPatgPA@mail.gmail.com>
 <CALEXWq2g2XFAZ4tzjWuP65PDKXVx1HFK3DvsWVhtV18T61vKEQ@mail.gmail.com>
 <CALEXWq3q3uVXKsiOE=XTT5UxEWbCPF38JNbB8H+RTyioQOEN9w@mail.gmail.com>
Message-ID: <CAGiFhPOxoTvvG4hffr8deBsQX6RUaNuk0+guZLpkdSoNkRBgeA@mail.gmail.com>

Hello Kevin and I?aki,

Thanks for your quick responses. I sincerely appreciate them! I can see how
complicated it is to interact with R in C. I?aki's suggestion is very
helpful, I saw there is a lot of performance gain by turning the flag on,
but sadly the best performance it can offer still cannot beat R itself. It
is interesting to see that C++ is worse than R in this special case despite
there is a common belief that C++ code is the fast one... Anyway, thanks
again for your suggestions and reference!

Best,
Jiefei


On Tue, Jun 18, 2019 at 2:39 PM I?aki Ucar <iucar at fedoraproject.org> wrote:

> For reference, your benchmark using UNWIND_PROTECT:
>
> > system.time(test(testFunc, evn$x))
>    user  system elapsed
>   0.331   0.000   0.331
> > system.time(test(C_test1, testFunc, evn$x))
>    user  system elapsed
>   2.029   0.000   2.036
> > system.time(test(C_test2, expr, evn))
>    user  system elapsed
>   2.307   0.000   2.313
> > system.time(test(C_test3, testFunc, evn$x))
>    user  system elapsed
>   2.131   0.000   2.138
>
> I?aki
>
> On Tue, 18 Jun 2019 at 20:35, I?aki Ucar <iucar at fedoraproject.org> wrote:
> >
> > On Tue, 18 Jun 2019 at 19:41, King Jiefei <szwjf08 at gmail.com> wrote:
> > >
> > > [...]
> > >
> > > It is clear to see that calling an R function in R is the fast one, it
> is
> > > about 5X faster than ` R_forceAndCall ` and ` Rf_eval`. the latter two
> > > functions have a similar performance and using Rcpp is the worst one.
> Is it
> > > expected? Why is calling an R function from C++ much slower than
> calling
> > > the function from R? Is there any faster way to do the function call
> in C++?
> >
> > Yes, there is: enable fast evaluation by setting
> > -DRCPP_USE_UNWIND_PROTECT, or alternatively, use
> >
> > // [[Rcpp::plugins(unwindProtect)]]
> >
> > I?aki
>
>
>
> --
> I?aki ?car
>

	[[alternative HTML version deleted]]



[Rd] Fast way to call an R function from C++?
King Jiefei