Skip to content

Instantly share code, notes, and snippets.

@kevinushey
Last active March 3, 2019 01:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinushey/8911432 to your computer and use it in GitHub Desktop.
Save kevinushey/8911432 to your computer and use it in GitHub Desktop.
Comparing methods of checking whether a value is NA
## my results
Unit: milliseconds
expr min lq median uq max neval
R_IsNA(x) 39.21518 40.16967 40.84210 42.75894 63.77600 100
R_IsNA_alternate(x) 22.69085 23.66232 24.26383 25.78633 49.38843 100
## sessionInfo
R Under development (unstable) (2014-02-01 r64910)
Platform: x86_64-apple-darwin13.0.0 (64-bit)
locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] microbenchmark_1.3-0 knitr_1.5.15 devtools_1.4.1.99 BiocInstaller_1.13.3
loaded via a namespace (and not attached):
[1] compiler_3.1.0 digest_0.6.4 evaluate_0.5.1 formatR_0.10 httr_0.2 memoise_0.1 parallel_3.1.0
[8] Rcpp_0.11.0.2 RCurl_1.95-4.1 stringr_0.6.2 tools_3.1.0 whisker_0.3-2
## compiler info
kevinushey@Kevin-MBP:~$ clang -v
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
## compiled with -g -O3 -march=native -std=c++11
// run me by installing Rcpp and calling sourceCpp on this file
#include <Rcpp.h>
using namespace Rcpp;
// some initial stuff borrowed from R sources
#ifdef WORDS_BIGENDIAN
static const int hw = 0;
static const int lw = 1;
#else /* !WORDS_BIGENDIAN */
static const int hw = 1;
static const int lw = 0;
#endif /* WORDS_BIGENDIAN */
typedef union
{
double value;
unsigned int word[2];
} ieee_double;
// R's detection of NA
int R_na(double x) {
if (isnan(x)) {
ieee_double y;
y.value = x;
return (y.word[lw] == 1954);
}
return 0;
}
// [[Rcpp::export]]
IntegerVector R_IsNA(NumericVector x) {
return sapply(x, R_na);
}
// alternate version for detecting NA
int alternate_na(double x) {
return memcmp(
(char*)(&x),
(char*)(&NA_REAL),
sizeof(double)
) == 0;
}
// [[Rcpp::export]]
IntegerVector R_IsNA_alternate(NumericVector x)
{
return sapply(x, alternate_na);
}
/*** R
library(microbenchmark)
x <- rnorm(1E7)
x[ sample(1:1E7, 1E6) ] <- NA
x[ sample(1:1E7, 1E6) ] <- NaN
microbenchmark(
R_IsNA(x),
R_IsNA_alternate(x)
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment