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
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
1.1k
RでPSM分析
bob3bob3
1
480
Rでコンジョイント分析 2024年版
bob3bob3
0
2.7k
『改訂新版前処理大全』の話と Apache Parquet の話 #TokyoR
bob3bob3
0
1.5k
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
660
『データ可視化学入門』をPythonからRに翻訳した話(増強版)
bob3bob3
0
620
『データ可視化学入門』を PythonからRに翻訳した話
bob3bob3
1
680
qeMLパッケージの紹介
bob3bob3
0
2.9k
「国と音楽」 ~spotifyrを用いて~ #muana
bob3bob3
2
680
Other Decks in Technology
See All in Technology
Sony-DroidKaigi2026
sony
1
320
いかに伝えるか 〜新卒エンジニアの教育のための、ライトノベル活用の一例
ikedon
1
200
生成AI時代の クレデンシャルとパーミッション設計
nrinetcom
PRO
3
1.5k
When Does a Local Qwen Start to Break
morshoto
0
180
Genie Code ワークショップ 応用編 / Genie-Code-Workshop-advanced
databricksjapan
PRO
0
350
AI-DLCって実際どう? 〜聞きたいこと全部聞いてみる〜
news_it_enj
0
230
多摩川(.dev)ランニング入門 / Tamagawa.dev#3
fujiwara3
3
360
全員がプロダクトへ向き合う組織を持続成長させるために——組織づくりのフライホイールと4象限 / The Flywheel Model and Four Quadrants for Organizational Design
hiro_torii
3
840
Azure Cost Management の FOCUS コストデータを迷わず読むための“3つの軸”
tetsuyaooooo
0
140
【Oracle AI Spotlight ウェビナー】AWSか、Azureか、Google Cloudか。その議論にオラクルを含める意義。
oracle4engineer
PRO
2
230
Sigmaユーザーのための有用リソース一挙公開 & Sigmaで使えるMCP #sigma_ucj /useful-resources-for-sigma-computing-users-and-mcps-with-sigma
shinyaa31
0
100
Kiro Crewしか勝たん!?
miu_crescent
PRO
0
180
Featured
See All Featured
Design in an AI World
tapps
1
300
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
810
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
560
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
Exploring anti-patterns in Rails
aemeredith
3
490
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
650
Ethics towards AI in product and experience design
skipperchong
2
360
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
The Language of Interfaces
destraynor
162
27k
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!