Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove Boost #65

Closed
dcooley opened this issue Mar 9, 2020 · 1 comment
Closed

remove Boost #65

dcooley opened this issue Mar 9, 2020 · 1 comment

Comments

@dcooley
Copy link
Collaborator

dcooley commented Mar 9, 2020

Can replace all the date_to_string functions in Rcpp, for example something like

library(Rcpp)

cppFunction(
  
  code = '
    Rcpp::StringVector posixct_to_string( Rcpp::NumericVector nv ) {
      R_xlen_t i;
      R_xlen_t n = nv.size();
      
      Rcpp::StringVector sv( n );
      
      for( i = 0; i < n; ++i ) {
        Rcpp::Datetime d = nv[ i ];
        Rcpp::Rcout << d.getYear() << "-" << d.getMonth() << "-" << d.getDay() << "T" << d.getHours() << ":" << d.getMinutes() << ":" << d.getSeconds() << std::endl;
      }
      
      return sv;
    }
  '
)
@SymbolixAU
Copy link
Collaborator

SymbolixAU commented Mar 9, 2020

some benchmarks

library(Rcpp)

cppFunction(
  code = "
    Rcpp::StringVector posixct_to_string( Rcpp::NumericVector nv ) {
      R_xlen_t i;
      R_xlen_t n = iv.size();
  
      Rcpp::StringVector sv( n );
      
      for( i = 0; i < n; ++i ) {
        Rcpp::Datetime d = iv[ i ];
        
        int yyyy = d.getYear();
        int mm = d.getMonth();
        int dd = d.getDay();
        int h = d.getHours();
        int m = d.getMinutes();
        int s = d.getSeconds();
        std::ostringstream os;
        os << std::setfill('0') << std::setw(4) << yyyy << '-';
        os << std::setfill('0') << std::setw(2) << mm << '-';
        os << std::setfill('0') << std::setw(2) << dd << 'T';
        os << std::setfill('0') << std::setw(2) << h << ':';
        os << std::setfill('0') << std::setw(2) << m << ':';
        os << std::setfill('0') << std::setw(2) << s;
          
        sv[i] = os.str();
      }
    return sv;
    }
  "
)

cppFunction(
  depends = "BH"
  , includes = '#include <boost/date_time.hpp>'
  , code = '
    Rcpp::StringVector bh_posix_to_string( Rcpp::NumericVector nv ) {
    
    R_xlen_t i;
    R_xlen_t n = nv.size();
    
    boost::local_time::tz_database tz_db;
    
    Rcpp::StringVector sv( n );
    
    for ( i = 0; i < n; ++i ) {
      Rcpp::Datetime d = nv[i];
      boost::gregorian::date dt( d.getYear(), d.getMonth(), d.getDay() );
      boost::posix_time::hours h( d.getHours() );
      boost::posix_time::minutes mins( d.getMinutes() );
      boost::posix_time::seconds sec( d.getSeconds() );
      boost::posix_time::time_duration td = h + mins + sec;
      
      boost::posix_time::ptime pt = boost::posix_time::ptime( dt, td );
      
      std::string s = boost::posix_time::to_iso_extended_string( pt );
      sv[i] = s.c_str();
    }
    return sv;
  }
  '
)


dte <- as.Date("2020-01-01")
date_to_string( dte )


psx <- as.POSIXct("2020-01-01 00:00:01")
posixct_to_string( psx )

vec <- rep(psx, 1e6)

rpp <- posixct_to_string( vec )
bh <- bh_posixct_to_string( vec )

all.equal( rpp, bh )

microbenchmark::microbenchmark(
  rcpp = {
    rpp <- posixct_to_string( vec )
  }
  , bh = {
    bh <- bh_posixct_to_string( vec )
  }, 
  times = 5
)

# Unit: milliseconds
# expr       min        lq      mean    median        uq       max neval
# rcpp  405.6936  413.4812  415.7236  416.2115  418.8226  425.8145    10
#   bh 1724.2650 1733.3073 1751.8593 1741.9584 1760.6240 1820.2224    10

SymbolixAU pushed a commit that referenced this issue Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant