SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
Turbocharge your R

                    Rob Zinkov


                   July 12th, 2011




Rob Zinkov ()       Turbocharge your R   July 12th, 2011   1 / 23
Outline



1   Introduction


2   .C


3   .Call


4   Rcpp




         Rob Zinkov ()   Turbocharge your R   July 12th, 2011   2 / 23
Introduction


What is the point of this talk?




Show you how to speed up your R code




     Rob Zinkov ()            Turbocharge your R   July 12th, 2011   3 / 23
Introduction


Caveats




  • Please try to optimize your R code first
  • Some of these mechanisms will make coding harder




     Rob Zinkov ()              Turbocharge your R     July 12th, 2011   4 / 23
.C




• This is the basic mechanism
• Explicitly copies the data into C
• Only accepts integer vectors




    Rob Zinkov ()            Turbocharge your R   July 12th, 2011   5 / 23
.C


Step 1. Put function in le (foo.c)



void foo(int *nin, double *x)
{
int n = nin[0];

int i;

for (i=0; i<n; i++)
x[i] = x[i] * x[i];
}




     Rob Zinkov ()       Turbocharge your R   July 12th, 2011   6 / 23
.C




• Note this is a void function
• Note arguments are passed in as pointers
• Try to limit one function per file




    Rob Zinkov ()            Turbocharge your R   July 12th, 2011   7 / 23
.C


Step 2. Compile le with R




$ R CMD SHLIB foo.c




     Rob Zinkov ()     Turbocharge your R   July 12th, 2011   8 / 23
.C


Step 3. Load into R




> dyn.load("foo.so")




     Rob Zinkov ()     Turbocharge your R   July 12th, 2011   9 / 23
.C


Step 4. Call your code




 .C("foo", n=as.integer(5), x=as.double(rnorm(5)))




     Rob Zinkov ()       Turbocharge your R    July 12th, 2011   10 / 23
.C




• Arguments to .C are name of function followed by arguments
• Arguments must be the right type
• Touching C code runs risks of segfaults




   Rob Zinkov ()            Turbocharge your R       July 12th, 2011   11 / 23
.Call


Why?




 • Less copying of data structures (lower memory)
 • Access more of R data structures
 • Access more kinds of R data
 • Do more in C




    Rob Zinkov ()            Turbocharge your R     July 12th, 2011   12 / 23
.Call


.Call code


#include <R.h>
#include <Rinternals.h>
#include <Rmath.h>

SEXP vecSum(SEXP Rvec){
  int i, n;
  double *vec, value = 0;
  vec = REAL(Rvec);
  n = length(Rvec);
  for (i = 0; i < n; i++) value += vec[i];
  printf("The value is: %4.6f n", value);
  return R_NilValue;
}


     Rob Zinkov ()        Turbocharge your R   July 12th, 2011   13 / 23
.Call




R CMD SHLIB vecSum.c
dyn.load("vecSum.so")
.Call("vecSum", rnorm(10))




     Rob Zinkov ()       Turbocharge your R   July 12th, 2011   14 / 23
.Call




SEXP ab(SEXP Ra, SEXP Rb){
   int i, a, b;
   SEXP Rval;
   Ra = coerceVector(Ra, INTSXP);
   Rb = coerceVector(Rb, INTSXP);
   a = INTEGER(Ra)[0];
   b = INTEGER(Rb)[0];
   PROTECT(Rval = allocVector(INTSXP, b - a + 1));
   for (i = a; i <= b; i++)
       INTEGER(Rval)[i - a] = i;
   UNPROTECT(1);
   return Rval;
}



     Rob Zinkov ()       Turbocharge your R    July 12th, 2011   15 / 23
.Call




Since memory is shared explicit care must be taken not to collide with R




      Rob Zinkov ()            Turbocharge your R         July 12th, 2011   16 / 23
Rcpp


Why?




 • Use C++ instead of C
 • Ability to use objects to represent R more naturally
 • Easier to load code




     Rob Zinkov ()            Turbocharge your R          July 12th, 2011   17 / 23
Rcpp




src <- ’
    IntegerVector tmp(clone(x));
    double rate = as< double >(y);
    int tmpsize = tmp.size();
    RNGScope scope;
    for (int ii =0; ii < tmpsize; ii++) {
        tmp(ii) = Rf_rbinom(tmp(ii), rate);
    };
    return tmp;
’
require(inline)
## compile the function, inspect the process with verbose=T
testfun2 = cxxfunction(signature(x=’integer’, y=’numeric’),
                       src, plugin=’Rcpp’, verbose=T)



     Rob Zinkov ()       Turbocharge your R    July 12th, 2011   18 / 23
Rcpp




require(inline)
testfun = cxxfunction(
    signature(x="numeric",
              i="integer"),
              body = ’
                        NumericVector xx(x);
                        int ii = as<int>(i);
                        xx = xx * ii;
                        return( xx );
                     ’, plugin="Rcpp")
testfun(1:5, 3)




     Rob Zinkov ()       Turbocharge your R    July 12th, 2011   19 / 23
Rcpp


Conclusions




It is fairly easy to make R faster




      Rob Zinkov ()              Turbocharge your R   July 12th, 2011   20 / 23
Rcpp


Conclusions




Now go make your R code faster




     Rob Zinkov ()          Turbocharge your R   July 12th, 2011   21 / 23
Rcpp


References



  • http://www.stat.umn.edu/ charlie/rc/
  • http://helmingstay.blogspot.com/2011/06/efficient-loops-in-r-
    complexity-versus.html
  • http://www.sfu.ca/ sblay/R-C-interface.ppt
  • http://www.biostat.jhsph.edu/ bcaffo/statcomp/files/dotCall.pdf
  • http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-
    quickref.pdf
  • http://www.jstatsoft.org/v40/i08/paper




     Rob Zinkov ()           Turbocharge your R        July 12th, 2011   22 / 23
Rcpp




                Questions?




Rob Zinkov ()     Turbocharge your R   July 12th, 2011   23 / 23

Weitere ähnliche Inhalte

Was ist angesagt?

Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and pythonHoang Nguyen
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cflSampath Kumar S
 
Debugging Your PHP Cake Application
Debugging Your PHP Cake ApplicationDebugging Your PHP Cake Application
Debugging Your PHP Cake ApplicationJose Diaz-Gonzalez
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2Rajendran
 
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...Dr.-Ing. Thomas Hartmann
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3Syed Mustafa
 
A Multi-theory Logic Language for the World Wide Web
A Multi-theory Logic Language for the World Wide WebA Multi-theory Logic Language for the World Wide Web
A Multi-theory Logic Language for the World Wide Webgpiancastelli
 
Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you wereJean-Baptiste Mazon
 
Debugging and Profiling Rails Application
Debugging and Profiling Rails ApplicationDebugging and Profiling Rails Application
Debugging and Profiling Rails ApplicationDavid Paluy
 
Microsoft F# and functional programming
Microsoft F# and functional programmingMicrosoft F# and functional programming
Microsoft F# and functional programmingRadek Mika
 
「C++コンパイラアップデート」
「C++コンパイラアップデート」「C++コンパイラアップデート」
「C++コンパイラアップデート」Embarcadero Technologies
 
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Igalia
 
Unicode Cjk Compatible Variations
Unicode Cjk Compatible VariationsUnicode Cjk Compatible Variations
Unicode Cjk Compatible VariationsJoe Jiang
 

Was ist angesagt? (15)

LTO plugin
LTO pluginLTO plugin
LTO plugin
 
What's New in C# 6
What's New in C# 6What's New in C# 6
What's New in C# 6
 
Cobol, lisp, and python
Cobol, lisp, and pythonCobol, lisp, and python
Cobol, lisp, and python
 
3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl
 
Debugging Your PHP Cake Application
Debugging Your PHP Cake ApplicationDebugging Your PHP Cake Application
Debugging Your PHP Cake Application
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...
2014.10 - Towards Description Set Profiles for RDF Using SPARQL as Intermedia...
 
15CS664 Python Question Bank-3
15CS664 Python Question Bank-315CS664 Python Question Bank-3
15CS664 Python Question Bank-3
 
A Multi-theory Logic Language for the World Wide Web
A Multi-theory Logic Language for the World Wide WebA Multi-theory Logic Language for the World Wide Web
A Multi-theory Logic Language for the World Wide Web
 
Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you were
 
Debugging and Profiling Rails Application
Debugging and Profiling Rails ApplicationDebugging and Profiling Rails Application
Debugging and Profiling Rails Application
 
Microsoft F# and functional programming
Microsoft F# and functional programmingMicrosoft F# and functional programming
Microsoft F# and functional programming
 
「C++コンパイラアップデート」
「C++コンパイラアップデート」「C++コンパイラアップデート」
「C++コンパイラアップデート」
 
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
 
Unicode Cjk Compatible Variations
Unicode Cjk Compatible VariationsUnicode Cjk Compatible Variations
Unicode Cjk Compatible Variations
 

Andere mochten auch

Biblioteca del Congreso Nacional Argentino
Biblioteca del Congreso Nacional ArgentinoBiblioteca del Congreso Nacional Argentino
Biblioteca del Congreso Nacional Argentinomartarure
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017Chris Tankersley
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new featuresAniket Thakur
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?Colin Riley
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsVolodymyr Voytyshyn
 
Brain Computer Interface.ppt
Brain Computer Interface.pptBrain Computer Interface.ppt
Brain Computer Interface.pptAmal Sanjay
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented conceptsBG Java EE Course
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagramsbarney92
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsRaja Sekhar
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaVeerabadra Badra
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 

Andere mochten auch (13)

Biblioteca del Congreso Nacional Argentino
Biblioteca del Congreso Nacional ArgentinoBiblioteca del Congreso Nacional Argentino
Biblioteca del Congreso Nacional Argentino
 
OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017OOP Is More Then Cars and Dogs - Midwest PHP 2017
OOP Is More Then Cars and Dogs - Midwest PHP 2017
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
 
What does OOP stand for?
What does OOP stand for?What does OOP stand for?
What does OOP stand for?
 
Modern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design PatternsModern JavaScript Applications: Design Patterns
Modern JavaScript Applications: Design Patterns
 
Brain Computer Interface.ppt
Brain Computer Interface.pptBrain Computer Interface.ppt
Brain Computer Interface.ppt
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Uml diagrams
Uml diagramsUml diagrams
Uml diagrams
 
Java OOP s concepts and buzzwords
Java OOP s concepts and buzzwordsJava OOP s concepts and buzzwords
Java OOP s concepts and buzzwords
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 

Ähnlich wie Los Angeles R users group - July 12 2011 - Part 2

Dot Call interface
Dot Call interfaceDot Call interface
Dot Call interfaceHao Chai
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in RDavid Springate
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for RSeth Falcon
 
Foreman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsForeman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsStoyan Zhekov
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014Romain Francois
 
Shift rotate
Shift rotateShift rotate
Shift rotatefika sweety
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHiroshi SHIBATA
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Jimmy Schementi
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahNick Plante
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RKevin Smith
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 

Ähnlich wie Los Angeles R users group - July 12 2011 - Part 2 (20)

Dot Call interface
Dot Call interfaceDot Call interface
Dot Call interface
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp11 useR2014
Rcpp11 useR2014Rcpp11 useR2014
Rcpp11 useR2014
 
F#3.0
F#3.0 F#3.0
F#3.0
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
Native interfaces for R
Native interfaces for RNative interfaces for R
Native interfaces for R
 
Foreman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple componentsForeman - Process manager for applications with multiple components
Foreman - Process manager for applications with multiple components
 
Special topics in finance lecture 2
Special topics in finance   lecture 2Special topics in finance   lecture 2
Special topics in finance lecture 2
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Shift rotate
Shift rotateShift rotate
Shift rotate
 
How to Begin to Develop Ruby Core
How to Begin to Develop Ruby CoreHow to Begin to Develop Ruby Core
How to Begin to Develop Ruby Core
 
Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011Iron Languages - NYC CodeCamp 2/19/2011
Iron Languages - NYC CodeCamp 2/19/2011
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++
 
Building Web APIs that Scale
Building Web APIs that ScaleBuilding Web APIs that Scale
Building Web APIs that Scale
 
8051assembly language
8051assembly language8051assembly language
8051assembly language
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
IIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into RIIUG 2016 Gathering Informix data into R
IIUG 2016 Gathering Informix data into R
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 

Mehr von rusersla

LA R meetup - Nov 2013 - Eric Klusman
LA R meetup - Nov 2013 - Eric KlusmanLA R meetup - Nov 2013 - Eric Klusman
LA R meetup - Nov 2013 - Eric Klusmanrusersla
 
useR2011 - Whitcher
useR2011 - WhitcheruseR2011 - Whitcher
useR2011 - Whitcherrusersla
 
useR2011 - Rougier
useR2011 - RougieruseR2011 - Rougier
useR2011 - Rougierrusersla
 
useR2011 - Huber
useR2011 - HuberuseR2011 - Huber
useR2011 - Huberrusersla
 
useR2011 - Gromping
useR2011 - Gromping useR2011 - Gromping
useR2011 - Gromping rusersla
 
useR2011 - Edlefsen
useR2011 - EdlefsenuseR2011 - Edlefsen
useR2011 - Edlefsenrusersla
 
Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1rusersla
 
Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2rusersla
 
Los Angeles R users group - Dec 14 2010 - Part 1
Los Angeles R users group - Dec 14 2010 - Part 1Los Angeles R users group - Dec 14 2010 - Part 1
Los Angeles R users group - Dec 14 2010 - Part 1rusersla
 
Los Angeles R users group - Dec 14 2010 - Part 3
Los Angeles R users group - Dec 14 2010 - Part 3Los Angeles R users group - Dec 14 2010 - Part 3
Los Angeles R users group - Dec 14 2010 - Part 3rusersla
 
Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2rusersla
 

Mehr von rusersla (11)

LA R meetup - Nov 2013 - Eric Klusman
LA R meetup - Nov 2013 - Eric KlusmanLA R meetup - Nov 2013 - Eric Klusman
LA R meetup - Nov 2013 - Eric Klusman
 
useR2011 - Whitcher
useR2011 - WhitcheruseR2011 - Whitcher
useR2011 - Whitcher
 
useR2011 - Rougier
useR2011 - RougieruseR2011 - Rougier
useR2011 - Rougier
 
useR2011 - Huber
useR2011 - HuberuseR2011 - Huber
useR2011 - Huber
 
useR2011 - Gromping
useR2011 - Gromping useR2011 - Gromping
useR2011 - Gromping
 
useR2011 - Edlefsen
useR2011 - EdlefsenuseR2011 - Edlefsen
useR2011 - Edlefsen
 
Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1
 
Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2Los Angeles R users group - Nov 17 2010 - Part 2
Los Angeles R users group - Nov 17 2010 - Part 2
 
Los Angeles R users group - Dec 14 2010 - Part 1
Los Angeles R users group - Dec 14 2010 - Part 1Los Angeles R users group - Dec 14 2010 - Part 1
Los Angeles R users group - Dec 14 2010 - Part 1
 
Los Angeles R users group - Dec 14 2010 - Part 3
Los Angeles R users group - Dec 14 2010 - Part 3Los Angeles R users group - Dec 14 2010 - Part 3
Los Angeles R users group - Dec 14 2010 - Part 3
 
Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2Los Angeles R users group - Dec 14 2010 - Part 2
Los Angeles R users group - Dec 14 2010 - Part 2
 

KĂźrzlich hochgeladen

Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot Model
Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot ModelBhubaneswar Call Girls 8250077686 Service Offer VIP Hot Model
Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot ModelDeiva Sain Call Girl
 
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service Available
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service AvailableKolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service Available
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service AvailableNitya salvi
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sampleCasey Keith
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sampleCasey Keith
 
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot Model
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot ModelPapi kondalu Call Girls 8250077686 Service Offer VIP Hot Model
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot ModelDeiva Sain Call Girl
 
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot ModelAndheri Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000Ahmedabad Escort Service Ahmedabad Call Girl 0000000000
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000mountabuangels4u
 
IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM1112022472524
 
Top travel agency in panchkula - Best travel agents in panchkula
Top  travel agency in panchkula - Best travel agents in panchkulaTop  travel agency in panchkula - Best travel agents in panchkula
Top travel agency in panchkula - Best travel agents in panchkulauseyourbrain1122
 
WhatsApp Chat: 📞 8617697112 Independent Call Girls in Darjeeling
WhatsApp Chat: 📞 8617697112 Independent Call Girls in DarjeelingWhatsApp Chat: 📞 8617697112 Independent Call Girls in Darjeeling
WhatsApp Chat: 📞 8617697112 Independent Call Girls in DarjeelingNitya salvi
 
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...Nitya salvi
 
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.Nitya salvi
 
Ooty call girls 📞 8617697112 At Low Cost Cash Payment Booking
Ooty call girls 📞 8617697112 At Low Cost Cash Payment BookingOoty call girls 📞 8617697112 At Low Cost Cash Payment Booking
Ooty call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh HaldighatiApsara Of India
 
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...Apsara Of India
 
Ooty Call Girls 8250077686 Service Offer VIP Hot Model
Ooty Call Girls 8250077686 Service Offer VIP Hot ModelOoty Call Girls 8250077686 Service Offer VIP Hot Model
Ooty Call Girls 8250077686 Service Offer VIP Hot ModelDeiva Sain Call Girl
 
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls Agency
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls AgencyHire 💕 8617697112 Reckong Peo Call Girls Service Call Girls Agency
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls AgencyNitya salvi
 
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...mountabuangels4u
 
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot ModelAndheri East Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...Abortion pills in Riyadh +966572737505 get cytotec
 

KĂźrzlich hochgeladen (20)

Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot Model
Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot ModelBhubaneswar Call Girls 8250077686 Service Offer VIP Hot Model
Bhubaneswar Call Girls 8250077686 Service Offer VIP Hot Model
 
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service Available
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service AvailableKolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service Available
Kolkata Call Girls - 📞 8617697112 🔝 Top Class Call Girls Service Available
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sample
 
sample sample sample sample sample sample
sample sample sample sample sample samplesample sample sample sample sample sample
sample sample sample sample sample sample
 
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot Model
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot ModelPapi kondalu Call Girls 8250077686 Service Offer VIP Hot Model
Papi kondalu Call Girls 8250077686 Service Offer VIP Hot Model
 
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot ModelAndheri Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000Ahmedabad Escort Service Ahmedabad Call Girl 0000000000
Ahmedabad Escort Service Ahmedabad Call Girl 0000000000
 
IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111IATA GEOGRAPHY AREAS in the world, HM111
IATA GEOGRAPHY AREAS in the world, HM111
 
Top travel agency in panchkula - Best travel agents in panchkula
Top  travel agency in panchkula - Best travel agents in panchkulaTop  travel agency in panchkula - Best travel agents in panchkula
Top travel agency in panchkula - Best travel agents in panchkula
 
WhatsApp Chat: 📞 8617697112 Independent Call Girls in Darjeeling
WhatsApp Chat: 📞 8617697112 Independent Call Girls in DarjeelingWhatsApp Chat: 📞 8617697112 Independent Call Girls in Darjeeling
WhatsApp Chat: 📞 8617697112 Independent Call Girls in Darjeeling
 
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...
Night 7k to 12k Lahaul and Spiti Call Girls 👉👉 8617697112⭐⭐ 100% Genuine Esco...
 
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.
❤Personal Contact Number Varanasi Call Girls 8617697112💦✅.
 
Ooty call girls 📞 8617697112 At Low Cost Cash Payment Booking
Ooty call girls 📞 8617697112 At Low Cost Cash Payment BookingOoty call girls 📞 8617697112 At Low Cost Cash Payment Booking
Ooty call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati
💕📲09602870969💓Girl Escort Services Udaipur Call Girls in Chittorgarh Haldighati
 
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...
🔥HOT🔥📲9602870969🔥Prostitute Service in Udaipur Call Girls in City Palace Lake...
 
Ooty Call Girls 8250077686 Service Offer VIP Hot Model
Ooty Call Girls 8250077686 Service Offer VIP Hot ModelOoty Call Girls 8250077686 Service Offer VIP Hot Model
Ooty Call Girls 8250077686 Service Offer VIP Hot Model
 
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls Agency
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls AgencyHire 💕 8617697112 Reckong Peo Call Girls Service Call Girls Agency
Hire 💕 8617697112 Reckong Peo Call Girls Service Call Girls Agency
 
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
Vadodara Escort💋 Call Girl (Bindu) Service #Vadodara Call Girl @Independent G...
 
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot ModelAndheri East Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Andheri East Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...
CYTOTEC DUBAI ☎️ +966572737505 } Abortion pills in Abu dhabi,get misoprostal ...
 

Los Angeles R users group - July 12 2011 - Part 2

  • 1. Turbocharge your R Rob Zinkov July 12th, 2011 Rob Zinkov () Turbocharge your R July 12th, 2011 1 / 23
  • 2. Outline 1 Introduction 2 .C 3 .Call 4 Rcpp Rob Zinkov () Turbocharge your R July 12th, 2011 2 / 23
  • 3. Introduction What is the point of this talk? Show you how to speed up your R code Rob Zinkov () Turbocharge your R July 12th, 2011 3 / 23
  • 4. Introduction Caveats • Please try to optimize your R code rst • Some of these mechanisms will make coding harder Rob Zinkov () Turbocharge your R July 12th, 2011 4 / 23
  • 5. .C • This is the basic mechanism • Explicitly copies the data into C • Only accepts integer vectors Rob Zinkov () Turbocharge your R July 12th, 2011 5 / 23
  • 6. .C Step 1. Put function in le (foo.c) void foo(int *nin, double *x) { int n = nin[0]; int i; for (i=0; i<n; i++) x[i] = x[i] * x[i]; } Rob Zinkov () Turbocharge your R July 12th, 2011 6 / 23
  • 7. .C • Note this is a void function • Note arguments are passed in as pointers • Try to limit one function per le Rob Zinkov () Turbocharge your R July 12th, 2011 7 / 23
  • 8. .C Step 2. Compile le with R $ R CMD SHLIB foo.c Rob Zinkov () Turbocharge your R July 12th, 2011 8 / 23
  • 9. .C Step 3. Load into R > dyn.load("foo.so") Rob Zinkov () Turbocharge your R July 12th, 2011 9 / 23
  • 10. .C Step 4. Call your code .C("foo", n=as.integer(5), x=as.double(rnorm(5))) Rob Zinkov () Turbocharge your R July 12th, 2011 10 / 23
  • 11. .C • Arguments to .C are name of function followed by arguments • Arguments must be the right type • Touching C code runs risks of segfaults Rob Zinkov () Turbocharge your R July 12th, 2011 11 / 23
  • 12. .Call Why? • Less copying of data structures (lower memory) • Access more of R data structures • Access more kinds of R data • Do more in C Rob Zinkov () Turbocharge your R July 12th, 2011 12 / 23
  • 13. .Call .Call code #include <R.h> #include <Rinternals.h> #include <Rmath.h> SEXP vecSum(SEXP Rvec){ int i, n; double *vec, value = 0; vec = REAL(Rvec); n = length(Rvec); for (i = 0; i < n; i++) value += vec[i]; printf("The value is: %4.6f n", value); return R_NilValue; } Rob Zinkov () Turbocharge your R July 12th, 2011 13 / 23
  • 14. .Call R CMD SHLIB vecSum.c dyn.load("vecSum.so") .Call("vecSum", rnorm(10)) Rob Zinkov () Turbocharge your R July 12th, 2011 14 / 23
  • 15. .Call SEXP ab(SEXP Ra, SEXP Rb){ int i, a, b; SEXP Rval; Ra = coerceVector(Ra, INTSXP); Rb = coerceVector(Rb, INTSXP); a = INTEGER(Ra)[0]; b = INTEGER(Rb)[0]; PROTECT(Rval = allocVector(INTSXP, b - a + 1)); for (i = a; i <= b; i++) INTEGER(Rval)[i - a] = i; UNPROTECT(1); return Rval; } Rob Zinkov () Turbocharge your R July 12th, 2011 15 / 23
  • 16. .Call Since memory is shared explicit care must be taken not to collide with R Rob Zinkov () Turbocharge your R July 12th, 2011 16 / 23
  • 17. Rcpp Why? • Use C++ instead of C • Ability to use objects to represent R more naturally • Easier to load code Rob Zinkov () Turbocharge your R July 12th, 2011 17 / 23
  • 18. Rcpp src <- ’ IntegerVector tmp(clone(x)); double rate = as< double >(y); int tmpsize = tmp.size(); RNGScope scope; for (int ii =0; ii < tmpsize; ii++) { tmp(ii) = Rf_rbinom(tmp(ii), rate); }; return tmp; ’ require(inline) ## compile the function, inspect the process with verbose=T testfun2 = cxxfunction(signature(x=’integer’, y=’numeric’), src, plugin=’Rcpp’, verbose=T) Rob Zinkov () Turbocharge your R July 12th, 2011 18 / 23
  • 19. Rcpp require(inline) testfun = cxxfunction( signature(x="numeric", i="integer"), body = ’ NumericVector xx(x); int ii = as<int>(i); xx = xx * ii; return( xx ); ’, plugin="Rcpp") testfun(1:5, 3) Rob Zinkov () Turbocharge your R July 12th, 2011 19 / 23
  • 20. Rcpp Conclusions It is fairly easy to make R faster Rob Zinkov () Turbocharge your R July 12th, 2011 20 / 23
  • 21. Rcpp Conclusions Now go make your R code faster Rob Zinkov () Turbocharge your R July 12th, 2011 21 / 23
  • 22. Rcpp References • http://www.stat.umn.edu/ charlie/rc/ • http://helmingstay.blogspot.com/2011/06/ecient-loops-in-r- complexity-versus.html • http://www.sfu.ca/ sblay/R-C-interface.ppt • http://www.biostat.jhsph.edu/ bcaffo/statcomp/les/dotCall.pdf • http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp- quickref.pdf • http://www.jstatsoft.org/v40/i08/paper Rob Zinkov () Turbocharge your R July 12th, 2011 22 / 23
  • 23. Rcpp Questions? Rob Zinkov () Turbocharge your R July 12th, 2011 23 / 23