Comparing ifelse with C++ for loop with ifs

I currently am reading a bit about using Rcpp and its potential for speeding up R. I found one unexpected example in the lecture from Hadley Wickham:

require(Rcpp)
signR <- function(x) {
  if (x > 0) {
    1
  } else if (x == 0) {
    0
  } else {
    -1
  }
}

cppFunction('int signC(int x) {
  if (x > 0) {
    return 1;
  } else if (x == 0) {
    return 0;
  } else {
    return -1;
  }
}')

require(microbenchmark)
microbenchmark(signC(rnorm(1)), signR(rnorm(1)),times = 1e5)
## Unit: microseconds
##             expr   min    lq median   uq  max neval
##  signC(rnorm(1)) 2.832 3.186  3.540 3.54 4130 1e+05
##  signR(rnorm(1)) 2.478 3.186  3.186 3.54 2641 1e+05

As expected, the two versions perform nearly identical. Now for the surprise: I changed the scalar version of signC into a vectorized version:

library(Rcpp)

cppFunction('IntegerVector signCVec(NumericVector x){
int n = x.size();
IntegerVector out(n);
for(int i = 0; i < n; i++){
            if(x[i] > 0 ){
out[i] = 1;
            } else if(x[i] == 0){
out[i] = 0;
            } else {
out[i] = -1;
            }
}
return out;
}

            ')

signRVec <- function(x) {
  ifelse(x > 0,1, ifelse(x == 0,0,-1))
}

Now I would have expected the two functions also to be rather similar in execution, but see for yourself:

x = rnorm(1e6)

microbenchmark(signCVec(x), signRVec(x),times = 10)
## Unit: milliseconds
##         expr    min      lq  median      uq     max neval
##  signCVec(x)   8.07   8.103   8.311   8.761   8.952    10
##  signRVec(x) 571.91 581.988 607.664 620.322 743.546    10

Wow: A 60-odd-times reduction using Rcpp.

Shoutout for Atlassian

In the last two days I was able to download and install a great stack of software from Atlassian, which would theoretically allow a small team of 5-10 people to organize software development on enterprise-grade tools:

  • JIRA for issue tracking and planning, with support for Agile project management
  • STASH for hosting and integrating your git repositories
  • Crucible + Fish Eye for Code review
  • Bamboo for Continuous Integration
  • Confluence for Documentation

As a novice to server administration I was able to install the software, set the packages up for integrated work and import my projects from github – including issues, tags and so on, in about 10 working hours. Using the software is rather easy, maybe with the exception of the CI-server, which probable is more a sign of my lack of knowledge in this area, and a lack of templates in the R domain. And the price is a snap: for each package there is a starter licence setting you back 10$, covering 5-10 seats, which Atlassian will donate. So, if you are thinking of starting a startup, you could probably do worse than locking yourself into the Atlassian stack.

SAS on the road

I like some of the ideas what SAS might need that truck for:

https://twitter.com/kjhealy/status/423829146113155072

PS: Clustering whiskies

I played some more with the app from yesterday, and deployed it with a more usefull user interface and some new functionality on http://ojessen.shinyapps.io/whiskyTastingsApp/. By the way, thanks to the guys at rstudio.com for hosting the app on their servers.

 

Clustering whiskies by taste

Lately I was wondering how to integrate my wordpress blog with a shiny app. This post is an example for the collaboration between these platforms following the advice from this thread in the shiny google group. Looking for a good example for an app, I stumbled upon this article from Luba Gloukhov on the Revolution blog. I shamelessly copied most of the code from Luba. Obviously the layout of this blog is not ideal for the layout of the app, but I will work this out on another day.

You can find the code for the app on github. I also made a standalone-app with some more functions on http://ojessen.shinyapps.io/whiskyTastingsApp/