Skip to content

Instantly share code, notes, and snippets.

@jjallaire
Created November 11, 2012 16:18
Show Gist options
  • Save jjallaire/4055396 to your computer and use it in GitHub Desktop.
Save jjallaire/4055396 to your computer and use it in GitHub Desktop.
table1
library(Rcpp)
library(microbenchmark)
cppFunction('
IntegerVector table1(const CharacterVector x) {
std::map<std::string, int> counts;
std::vector<std::string> vec = as<std::vector<std::string> >(x);
int n = x.length();
for (int i = 0; i < n; i++) {
counts[vec[i]]++;
}
// Loop through each element of map and output into named vector
IntegerVector out(counts.size());
CharacterVector names(counts.size());
std::map<std::string, int>::iterator it;
int i;
for (i = 0, it = counts.begin(); it != counts.end(); i++, it++) {
names[i] = it->first;
out[i] = it->second;
}
out.attr("names") = names;
return(out);
}
')
# Take advantage of Rcpp auto-magic as/wrap for parameters and return
cppFunction('
std::map<std::string, int> table2(const std::vector<std::string>& x) {
std::map<std::string, int> counts;
for (int i = 0; i < x.size(); i++) {
counts[x[i]]++;
}
return counts;
}
')
# Eliminate the copying of inbound strings
cppFunction('
std::map<std::string, int> table3(CharacterVector x) {
std::map<std::string, int> counts;
for (int i = 0; i < x.size(); i++) {
const char* name = x[i];
counts[name]++;
}
return counts;
}
')
# Eliminate map insertion overhead by using char* as key
cppFunction('
std::map<std::string, int> table4(CharacterVector x) {
// perform the count
std::map<const char*, int> counts;
for (int i = 0; i < x.size(); i++) {
const char* name = x[i];
counts[name]++;
}
// creating a new map keyed by std::string
std::map<std::string,int> result;
for (std::map<const char*, int>::const_iterator
it = counts.begin(); it != counts.end(); ++it) {
result[it->first] = result[it->first] + it->second;
}
return result;
}
')
x <- sample(letters, 1e4, rep = T)
microbenchmark(
table(x),
table1(x),
table2(x),
table3(x),
table4(x)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment