The stdvectors
package allows the creation and
manipulation of C++ std::vector’s in R.
std::vector’s are dynamically allocated arrays, which are especially
helpful when you need to fill a huge vector (e.g. in a loop) but you
don’t know the size in advance.
<- stdvectorCreate(type='integer')
sv for(i in 1:10){
# yes we're modify the vector in-place !
stdvectorPushBack(sv,i)
}
<- stdvectorToVector(sv) v
R: 3.3.2 64bit
OS: Window 10
CPU: i5 6600K @3.5 Ghz
RAM: 16 GB
# Note: do not increase this too much, otherwise the first test will take ages!
<- 150000
n
# R vector (concatenation)
<- system.time({
tm1 <- integer()
v for(i in 1:n){
<- i
v[i]
}
}
)
# R vector (pre-allocation -> unfair test since the assumption is not knowing the size in advance)
<- system.time({
tm2 <- rep.int(NA_integer_,n)
v for(i in 1:n){
<- i
v[i]
}
}
)
# stdvector
<- system.time({
tm3 <- stdvectorCreate(type='integer')
sv for(i in 1:n){
stdvectorPushBack(sv,i)
}<- stdvectorToVector(sv)
v
}
)
> tm1
user system elapsed 10.84 0.00 10.84
> tm2
user system elapsed 0.13 0.00 0.13
> tm3
user system elapsed 0.18 0.00 0.19
As you can see the difference is big, and it becomes bigger and bigger as the number of elements grows.
GPL (>= 2)