@RYann thanks for pointing me to the tictoc library. I did some tests for both memory usage and speed. Conclusion: List takes less memory and is faster. Here is a copy of the code I used to do the testing:
library(tictoc)
# Comparing the memory usage of a Person object create using different techniques
# Example arguments
id <- 12345
firstname <- 'John'
lastname <- 'Doe'
age <- 30
country <- 'Germany'
city <- 'Berlin'
Person <- list()
# Using Data Frame
Person[["DataFrame.1"]] <- \(id, firstname, lastname, age, country, city) data.frame(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
Person[["DataFrame.2"]] <- \(id, firstname, lastname, age, country, city) {
person <- data.frame(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
}
Person[["DataFrame.3"]] <- \(id, firstname, lastname, age, country, city) {
person <- data.frame(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
return(person)
}
# Using Lists
Person[["List.1"]] <- \(id, firstname, lastname, age, country, city) list(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
Person[["List.2"]] <- \(id, firstname, lastname, age, country, city) {
person <- list(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
}
Person[["List.3"]] <- \(id, firstname, lastname, age, country, city) {
person <- list(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
return(person)
}
object.size(Person[["DataFrame.1"]]) # Results = 3064 bytes
object.size(Person[["DataFrame.3"]]) # Results = 7632 bytes
object.size(Person[["DataFrame.2"]]) # Results = 6568 bytes
object.size(Person[["List.1"]]) # Results = 3064 bytes
object.size(Person[["List.2"]]) # Results = 6568 bytes
object.size(Person[["List.3"]]) # Results = 7632 bytes
person.one <- data.frame(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
person.two <- Person[["DataFrame.1"]](id, firstname, lastname, age, country, city)
person.three <- Person[["DataFrame.2"]](id, firstname, lastname, age, country, city)
person.four <- Person[["DataFrame.3"]](id, firstname, lastname, age, country, city)
person.five <- list(
Id = id,
Firstname = firstname,
Lastname = lastname,
Age = age,
Country = country,
City = city
)
person.six <- Person[["List.1"]](id, firstname, lastname, age, country, city)
person.seven <- Person[["List.2"]](id, firstname, lastname, age, country, city)
person.eight <- Person[["List.3"]](id, firstname, lastname, age, country, city)
# Memory Usage
# Data Frame based
object.size(person.one) # Results 1616 bytes
object.size(person.two) # Results 1616 bytes
object.size(person.three) # Results 1616 bytes
object.size(person.four) # Results 1616 bytes
# List Based
object.size(person.five) # Results 1216 bytes
object.size(person.six) # Results 1216 bytes
object.size(person.seven) # Results 1216 bytes
object.size(person.eight) # Results 1216 bytes
PrintPersonInfo <- \(person) {
print(person[["Id"]])
print(person[["Firstname"]])
print(person[["Lastname"]])
print(person[["Age"]])
print(person[["Country"]])
print(person[["City"]])
}
CreatePerson <- \(createFunction,
printInfoFunction,
id, firstname, lastname, age, country, city) {
tic()
for (i in 1:100) {
person <- createFunction(id, firstname, lastname, age, country, city)
person |> printInfoFunction()
}
toc()
}
CreatePerson(Person[["DataFrame.1"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.11 sec
CreatePerson(Person[["DataFrame.2"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.11 sec
CreatePerson(Person[["DataFrame.3"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.11 sec
CreatePerson(Person[["List.1"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.05 sec
CreatePerson(Person[["List.2"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.02 sec
CreatePerson(Person[["List.3"]],PrintPersonInfo,id, firstname, lastname, age, country, city) # Results 0.04 sec