[Rcpp-devel] Why inline function is much faster than .Call?

Douglas Bates bates at stat.wisc.edu
Mon Aug 27 22:27:37 CEST 2012


On Mon, Aug 27, 2012 at 3:17 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi Dirk,
>
>> inlines uses .Call, so there is a slight logical problem here...  And yes,
>
> What is the logical problem?

Dirk is trying to point out that the two calls are using the same
mechanism.  All that the inline package does is wrap the process of
compiling, linking and dynamically loading the C++ function.
Executing a function compiled this way is exactly the same as
executing a function compiled separately.

Benchmarking a function like this only once is liable to be unreliable
because the current state of the garbage collection and heap
allocation can change running times.  It is better to run the function
a couple of times before benchmarking.

>> functions have overheads.
>
> If that is the case, .Call("test", xx) should be faster than test(xx).
> But it is not (see below). This looks wired to me.
>
>>
>> | library(microbenchmark)
>> | microbenchmark(test(xx))
>> | microbenchmark(test_inline(xx))
>> | microbenchmark(.Call('test', xx))
>>
>> The normal idiom is a _single_ call so that you can compare:
>>
>>   microbenchmark(test(xx), test_inline(xx), .Call('test', xx))
>
> Here is the output.
>
>> microbenchmark(test(xx), test_inline(xx), .Call('test', xx))
> Unit: microseconds
>                expr                      min                       lq
>                  median                       uq
> max
> 1 .Call("test", xx) 94.831999999999993633537 96.090499999999991587174
> 97.557000000000002160050 99.762499999999988631316
> 128.83099999999998885869
> 2   test_inline(xx)  4.440000000000000390799  5.101499999999999701572
> 6.393000000000000682121  6.615000000000000213163
> 30.33899999999999863576
> 3          test(xx) 69.683000000000006934897 70.387500000000002842171
> 71.593500000000005911716 73.153500000000008185452
> 198.30899999999999749889
>
> --
> Regards,
> Peng
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


More information about the Rcpp-devel mailing list