通过网络我可以读到我应该使用data.table和fread来加载我的数据.
但是当我运行基准测试时,我得到以下结果
Unit: milliseconds
expr min lq mean median uq max neval
test1 1.229782 1.280000 1.382249 1.366277 1.460483 1.580176 10
test3 1.294726 1.355139 1.765871 1.391576 1.542041 4.770357 10
test2 23.115503 23.345451 42.307979 25.492186 57.772522 125.941734 10
Run Code Online (Sandbox Code Playgroud)
代码可以在下面看到.
loadpath <- readRDS("paths.rds")
microbenchmark(
test1 = read.csv(paste0(loadpath,"data.csv"),header=TRUE,sep=";", stringsAsFactors = FALSE,colClasses = "character"),
test2 = data.table::fread(paste0(loadpath,"data.csv"), sep=";"),
test3 = read.csv(paste0(loadpath,"data.csv")),
times = 10
) %>%
print(order = "min")
Run Code Online (Sandbox Code Playgroud)
我明白这fread()应该比read.csv()因为它试图首先将行作为字符读入内存然后尝试将它们转换为整数和因子作为数据类型而更快.另一方面,fread()简单地将所有内容都读作字符.
如果这是真的,不test2应该快于test3?
有人可以解释我,我为什么不archieve一个加速或者至少相同的速度test2为test1 …
public class Card {
private Rank rank;
private Suit suit;
public Card(Rank r, Suit s) {
this.suit = s;
this.rank = r;
}
@Override
public String toString() {
return rank + " of " + suit;
}
public static void main (String[] args) {
Card test = new Card(Rank.A, Suit.Clubs);
System.out.println(test);
}
}
Run Code Online (Sandbox Code Playgroud)
所以在我的输出中我打印了一个俱乐部.但我没有使用toString()我只是从我的构造函数中定义一个新的卡.那么有人可以向我解释为什么我得到这个输出?