Skip to content

Instantly share code, notes, and snippets.

@hadley
Created November 8, 2012 15:16
Show Gist options
  • Select an option

  • Save hadley/4039410 to your computer and use it in GitHub Desktop.

Select an option

Save hadley/4039410 to your computer and use it in GitHub Desktop.
sign1 <- function(x) {
if (x > 0) {
1
} else if (x == 0) {
0
} else {
-1
}
}
cppFunction('
int sign2(const int x) {
if (x > 0) {
return(1);
} else if (x == 0) {
return(0);
} else {
return(-1);
}
}'
)
library(microbenchmark)
microbenchmark(
sign(1),
sign1(-1),
sign2(-1L))
# Unit: nanoseconds
# expr min lq median uq max neval
# sign(1) 212 236.0 265.5 280.0 2175 100
# sign1(-1) 1549 1615.5 1654.0 1698.5 17375 100
# sign2(-1L) 1135 1207.0 1243.0 1288.5 13647 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment