Comparison of efficiency methods to concatenate data (list, vector and data.frame)
As you can see, the use of c() to concatenate data into an object is not very efficient. It is better to create an object of the final size and replace data. However, there are no difference if the list element is replaced or the content of the list element:
For vector:
> c <- NULL
> system.time(
+ for (i in 1:100000) c <- c(c, rnorm(1, 1, 1))
+ )
utilisateur système écoulé
26.971 8.214 35.681
> c <- rep(NA, 100000)
> system.time(
+ for (i in 1:100000) c[i] <- rnorm(1, 1, 1)
+ )
utilisateur système écoulé
0.479 0.045 0.525
For list:
> library(embryogrowth)
>
> c <- list()
> system.time(
+ for (i in 1:100000) c <- c(c, list(resultNest_4p))
+ )
utilisateur système écoulé
68.412 7.460 76.696
>
> c <- rep(list(), 100000)
> system.time(
+ for (i in 1:100000) c[i] <- list(resultNest_4p)
+ )
utilisateur système écoulé
44.764 6.975 51.899
>
> c <- rep(list(), 100000)
> system.time(
+ for (i in 1:100000) c[[i]] <- resultNest_4p
+ )
utilisateur système écoulé
44.902 6.975 51.996
For data.frame
> system.time({
+ df <- data.frame(col1=numeric(), col2=character(), stringsAsFactors = FALSE);
+
+ for (i in 1:10000) {
+ df <- rbind(df, data.frame(col1=i, col2=as.character(i)))
+ }
+ }
+ )
utilisateur système écoulé
29.227 1.607 30.886
> system.time({
+ df <- data.frame(col1=rep(NA, 10000), col2=rep(NA, 10000), stringsAsFactors = FALSE);
+
+ for (i in 1:10000) {
+ df[i, "col1"] <- i
+ df[i, "col2"] <- as.character(i)
+ }
+ }
+ )
utilisateur système écoulé
1.822 0.253 2.080
Commentaires
Enregistrer un commentaire