Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rコードのベンチマーク
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
bob3bob3
October 20, 2022
Technology
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rコードのベンチマーク
Rコードのベンチマーク手法
bob3bob3
October 20, 2022
More Decks by bob3bob3
See All by bob3bob3
RとLLMで自然言語処理
bob3bob3
3
960
RでPSM分析
bob3bob3
1
450
Rでコンジョイント分析 2024年版
bob3bob3
0
2.5k
『改訂新版前処理大全』の話と Apache Parquet の話 #TokyoR
bob3bob3
0
1.5k
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
660
『データ可視化学入門』をPythonからRに翻訳した話(増強版)
bob3bob3
0
600
『データ可視化学入門』を PythonからRに翻訳した話
bob3bob3
1
670
qeMLパッケージの紹介
bob3bob3
0
2.8k
「国と音楽」 ~spotifyrを用いて~ #muana
bob3bob3
2
670
Other Decks in Technology
See All in Technology
プライバシー保護の理論と実践
lycorptech_jp
PRO
1
230
AWS Blocks を触ってみた/first-tach-aws-blocks
fossamagna
2
140
プロンプト_きのこカンファレンス2026_LT
yurufuwahealer
0
130
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
8k
デジタル・デザイン構想 by Sayaka Ishizuka
y150saya
0
180
どうして今サーバーサイドKotlinを選択したのか
nealle
0
190
From Prompt Engineering to Loop Engineering
shibuiwilliam
1
330
そのタスクオンスケですか?
poropinai1966
0
120
事業価値を⽣み出すSREへ SREが担うべき意思決定の5層
kenta_hi
0
130
トークン最適化のためのユーザーストーリー分析 / User Story Analysis for Token Optimization
oomatomo
0
160
CSに"SLO"は要らない、経営層に"99.9%"は伝わらない - SREを全社に"翻訳"する3原則
cscengineer
PRO
0
120
CVE-2026-20833_脆弱性対応とAES 化について
jukishiya
0
350
Featured
See All Featured
It's Worth the Effort
3n
188
29k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
3.6k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
170
Scaling GitHub
holman
464
140k
Are puppies a ranking factor?
jonoalderson
1
3.7k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
410
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
Writing Fast Ruby
sferik
630
63k
The Curse of the Amulet
leimatthew05
2
13k
How to Talk to Developers About Accessibility
jct
2
280
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Transcript
Rコードのベンチマーク Tokyo.R #102 2022/10/22 LT @bob3bob3
Rコードの実行時間を計る • Rの中級者になり、コードのいろいろな書き方が身についてくると「どんな書 き方をすると処理の効率がいいんだろう?」という疑問が湧いてくることが あります。 • 今回はRの実行時間を計る以下の三つの方法をお話しします。 ◦ base::system.time() ◦
tictocパッケージ ◦ microbenchmarkパッケージ
準備 • nycflights13のデータを使い、「月、 飛行機会社、出発地、目的地毎に 件数、飛行時間の平均値、中央 値、標準偏差を算出する」処理を dplyr、dtplyr、data.tableで書き、そ れぞれの実行速度を計る。 • デモデータは約33万行。
# ライブラリ library(tidyverse) library(dtplyr) library(data.table) library(nycflights13) # デモデータ dat <- flights |> left_join(planes, by = "tailnum")
準備:各処理の関数化 # dplyrでの処理を関数化 f_dplyr <- function(){ dat |> group_by(month, carrier,
origin, dest) |> summarise( 件数 = n(), 平均 = mean(air_time, na.rm = TRUE), 中央値 = median(air_time, na.rm = TRUE), .groups = "drop" ) } # dtplyrでの処理を関数化 f_dtplyr <- function(){ dat |> lazy_dt() |> group_by(month, carrier, origin, dest) |> summarise( 件数 = n(), 平均 = mean(air_time, na.rm = TRUE), 中央値 = median(air_time, na.rm = TRUE), .groups = "drop" ) |> as_tibble() }
準備:各処理の関数化 # data.tableでの処理を関数化 f_data.table <- function(){ dt <- dat |>
data.table( key=c("month", "carrier", "origin", "dest") ) dt[ , .( 件数 = .N, 平均 = air_time |> mean(na.rm = TRUE), 中央値 = air_time |> median(na.rm = TRUE) ), by = list(month, carrier, origin, dest) ] |> as_tibble() }
base::system.time() system.time()の第一引数に測定したい 処理を入れるだけ。
tictocパッケージ # tic()とtoc()で測定したい処理を挟む library(tictoc) tic() f_dplyr() toc() tic() f_dtplyr() toc()
tic() f_data.table() toc()
microbenchmarkパッケージ (1/3) • 複数の処理を一度に測定できる。 • 同じ処理を複数回試行して測定できる。 • 結果を可視化できる。
library(microbenchmark) res_micro <- microbenchmark( "dplyr"= f_dplyr(), "dtplyr" = f_dtplyr(), "data.table" = f_data.table(), times = 30 )
microbenchmarkパッケージ (2/3)
microbenchmarkパッケージ (3/3) # 測定結果の可視化 res_micro |> autoplot() # 素のdata.tableよりdtplyrの方が速いのは #
どこか書き方が悪いのだろうか?
Enjoy!