[1] 6
[1] 6
What is this code doing?
What are some differences between C++ and R code?
Here’s another function:
What similarities do you notice between C++ and R?
What is this code doing?
Rcpp::cppFunction('double sumC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}')
x <- rnorm(1000)
bench::mark(
sum(x),
sumC(x)
)
# A tibble: 2 × 6
expression min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 sum(x) 113.33µs 113.58µs 8701. 0B 0
2 sumC(x) 2.25µs 3.12µs 320880. 2.49KB 0
;
at the end of each lineNumericVector
objects are the equivalent of vectors in RNumericMatrix
objects are the equivalent of matrices in Rhttps://sta279-f23.github.io/class_activities/ca_lecture_26.html