R: Microbenchmark library, how to best create a table of multiple timing results where the results also need to be modified. Ideally functionised

I wish to compare the speed between data.table and dplyr functions.

Namely, I wish to rename a column with each library and see what the median and relative timings are for each.

The following code uses the inbuilt dataset “swiss”. What I’ve done works however it will bloat the script if I want to do it multiple times so I’m after a approach that could functionise what I’ve done or a different but more efficient approach;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>library(microbenchmark)
library(dplyr)
library(data.table)
DT <- as.data.table(datasets::swiss, keep.rownames=TRUE)
Tib <- tibble::as_tibble(tibble::rownames_to_column(datasets::swiss))
res <-
microbenchmark(
data.table = setnames(copy(DT), old = "rn", new = "Region"),
copyOverhead = copy(DT),
dplyr = rename(Tib, Region = rowname),
unit = "ms",
times = 100
)
</code>
<code>library(microbenchmark) library(dplyr) library(data.table) DT <- as.data.table(datasets::swiss, keep.rownames=TRUE) Tib <- tibble::as_tibble(tibble::rownames_to_column(datasets::swiss)) res <- microbenchmark( data.table = setnames(copy(DT), old = "rn", new = "Region"), copyOverhead = copy(DT), dplyr = rename(Tib, Region = rowname), unit = "ms", times = 100 ) </code>
library(microbenchmark)
library(dplyr)
library(data.table)


DT <- as.data.table(datasets::swiss, keep.rownames=TRUE)
Tib <- tibble::as_tibble(tibble::rownames_to_column(datasets::swiss))

res <-
microbenchmark(
  data.table = setnames(copy(DT), old = "rn", new = "Region"),
  copyOverhead = copy(DT),
  dplyr = rename(Tib, Region = rowname),
  unit = "ms",
  times = 100
)

Note that since data.table modifies the object by reference I had to create a copy of it within the benchmark otherwise it would fail after the first iteration since the ‘setnames’ old value would be changed. As such I need to record the time that ‘copy()’ takes and subtract this from the data.table setnames() timing.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>res <- setDT(summary(res))
res <- res[1, median := median - res[2, median]][c(1,3), .(expr, median)] # subtract the time it takes to copy and delete the copyOverhead row afterwards
res[which.max(res[, median]), relative := res[which.max(res[, median]), median] / res[which.min(res[, median]), median]] # calculate the relative timings
res[which.min(res[, median]), relative := 1]
res
</code>
<code>res <- setDT(summary(res)) res <- res[1, median := median - res[2, median]][c(1,3), .(expr, median)] # subtract the time it takes to copy and delete the copyOverhead row afterwards res[which.max(res[, median]), relative := res[which.max(res[, median]), median] / res[which.min(res[, median]), median]] # calculate the relative timings res[which.min(res[, median]), relative := 1] res </code>
res <- setDT(summary(res))

res <- res[1, median := median - res[2, median]][c(1,3), .(expr, median)] # subtract the time it takes to copy and delete the copyOverhead row afterwards

res[which.max(res[, median]), relative := res[which.max(res[, median]), median] / res[which.min(res[, median]), median]] # calculate the relative timings

res[which.min(res[, median]), relative := 1]

res

As I had to subtract the time of the copy() operation, the microbenchmark object is now a data.table object so I cant just print the relative units with print(res, unit = “relative), I had to do it manually.

So, this works. But I want to create many different comparisons of this nature. Is there somehow I could functionise the approach to remove the timings of the copy() operation and somehow wrap the microbenchmark function within this? I thought it might involve something with the … argument however I dont actually know how this properly works and cant make the arguments passed to … in the wrapper function use the arguments from the microbenchmark function.

I’d also be open to a different approach alltogether!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật