に移行するという Ramnath の提案をフォローアップするためjsonlite
に、2 つのアプローチのベンチマークを行いました。
##RJSONIO vs. jsonlite for a simple example
require(RJSONIO)
require(jsonlite)
require(microbenchmark)
json <- "{\"ID\":\"id1\",\"VALUE\":\"15\"},{\"ID\":\"id2\",\"VALUE\":\"10\"}"
test <- rep(json,1000)
test <- paste(test,collapse=",")
test <- paste0("[",test,"]")
func1 <- function(x){
temp <- jsonlite::fromJSON(x)
}
func2 <- function(x){
temp <- RJSONIO::fromJSON(x)
temp <- do.call(rbind,temp)
temp <- as.data.frame(temp,stringsAsFactors=FALSE)
}
> microbenchmark(func1(test),func2(test))
Unit: milliseconds
expr min lq median uq max neval
func1(test) 204.05228 221.46047 233.93321 246.90815 341.95684 100
func2(test) 21.60289 22.36368 22.70935 23.75409 27.41851 100
少なくとも今のところ、jsonlite
パッケージがまだ新しく、パフォーマンスよりも精度に重点を置いていることはわかっていますが、リストをデータ フレームに変換しても、この単純な例では古い RJSONIO の方が高速に実行されます。
含む更新rjson
:
require(rjson)
func3 <- function(x){
temp <- rjson::fromJSON(x)
temp <- do.call(rbind,lapply(temp,unlist))
temp <- as.data.frame(temp,stringsAsFactors=FALSE)
}
> microbenchmark(func1(test),func2(test),func3(test))
Unit: milliseconds
expr min lq median uq max neval
func1(test) 205.34603 220.85428 234.79492 249.87628 323.96853 100
func2(test) 21.76972 22.67311 23.11287 23.56642 32.97469 100
func3(test) 14.16942 15.96937 17.29122 20.19562 35.63004 100
> microbenchmark(func1(test),func2(test),func3(test),times=500)
Unit: milliseconds
expr min lq median uq max neval
func1(test) 206.48986 225.70693 241.16301 253.83269 336.88535 500
func2(test) 21.75367 22.53256 23.06782 23.93026 103.70623 500
func3(test) 14.21577 15.61421 16.86046 19.27347 95.13606 500
> identical(func1(test),func2(test)) & identical(func1(test),func3(test))
[1] TRUE
少なくとも私のマシンrjson
ではわずかにRJSONIO
速いだけですが、ラムナスが提案した大きなパフォーマンスの向上が見られる場所と比較して、どのようにスケーリングするかはテストしていません。