[Rcpp-devel] Datetime and DatetimeVector definitions

Dirk Eddelbuettel edd at debian.org
Thu May 19 15:52:03 CEST 2011


On 19 May 2011 at 15:27, Jason Lessels wrote:
| Hi Dirk,
| Thanks for your help. I am 99% confident that there is a better method to my example below, but this is the only way i was able to pass the format string to for the datetime object.
| 
| src<-'
| StringVector stringVec(dates);
| int n_stringVec=stringVec.size();
| std::string fmt =Rcpp::as<std::string>( format );
| 
| DatetimeVector  dateAll(n_stringVec);
| 
| std::string input;
| 
| for(int i=0;i<n_stringVec;i++){
|   input = stringVec[i];
|   Datetime dateTemp(input,fmt);
|   dateAll[i] = dateTemp;
| }
| 
| return wrap(dateAll);
| '
| library(inline)
| cpp_DatetimeVector<-cxxfunction(signature(dates="string",format="string"),src,plugin="Rcpp")
| 
| ####Create a fictional dataset
| foo<-seq(as.POSIXct("1960-01-01 00:00:00"),as.POSIXct("2010-01-01 00:00:00"),by='hour')
| 
| system.time(test<-cpp_DatetimeVector(foo,"%Y-%m-%d %H:%M:%OS"))
| #    user  system elapsed 
| # 98.607   0.904 101.061 
| system.time(test2<-strptime(foo,"%Y-%m-%d %H:%M:%OS"))
| #    user  system elapsed 
| # 14.089   0.261  14.501 

I see. Yes, the 101.061 is unappealing.  But dealing with timezones etc is
also very frustrating.  What I would is 

     test2 <- strptime(foo,"%Y-%m-%d %H:%M:%OS")   ## you now have POSIXct

     cpp_DatetimeVector( as.numeric(test2) )	   ## pass POSIXct down as numeric

with a rewritten cpp_DatetimeVector which does just

   src<-'
       DatetimeVector datevec(numericdates);       ## numericdates is as.numeric(POSIXct)

       // do stuff then return .....
   '

which should be very quick to execute.

In the uses I had, time always was (fractional) seconds since the epoch so I
never wrote another constructor....

Hope this helps, Dirk

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list