
Pacakges
Background
Rで行列を作るとき、matrix()でも`dim<-`()でも作れるのですが、どっちが速いのかなーと比べたくなりました。シミュレーションで1条件当たり何万回~も回すときは少しでも速い方がいいのです。
前にちょこっとやったものの条件をもっと増やしてみました。
Rの備忘録
— Takuto SAKAI (@tsakai_psych) March 22, 2026
2x2と3x3しかやってないけど、byrowで入れないならmatrixよりもdim<-の方が速そう pic.twitter.com/17emiEOTyQ
matrix()
ベクトルを行列にできます。引数nrowで行数を指定するか、引数ncolで列数を指定します。
行数や列数を指定しない場合、n行1列の行列ができます。
基本的には列方向にベクトルを入れていくようですが、引数byrowをTRUEにすると行方向にベクトルを入れられます。
引数dimnamesにlist(行名のベクトル, 列名のベクトル)を入れることで、行名と列名を設定できます。いらないところはNULLを入れると設定されません。
`dim<-`()
オブジェクトに次元を付与したりなくしたりできます。ふつうはdim(obj) <- valueで使うと思いますが、`dim<-`(obj, value)1も使えます。後者の記法はパイプラインと相性がいいです。引数valueには整数ベクトルを入れます。行列なら長さ2のベクトルを入れればいいです。
オブジェクトの長さと次元数がマッチしない場合はエラーを吐きます。
あと、matrix(..., byrow = TRUE)みたいなことはできないっぽいです、多分。
Benchmark
いつも使っているmicrobenchmark::microbenchmark()と、最近なんとなく使い方がわかってきたbench::mark()でベンチマークを回してみます。実際の場面ではintかnumericを扱う場合が多いと思うので、入力はrunif()で作ることにします。
microbenchmark
行数と列数に複数の水準を作りたいので、条件のdfを作って回したいと思います。条件は行列それぞれ1から10までです。1×1は除きます。
# A tibble: 99 × 2
nr nc
<int> <int>
1 1 2
2 1 3
3 1 4
4 1 5
5 1 6
6 1 7
7 1 8
8 1 9
9 1 10
10 2 1
11 2 2
12 2 3
13 2 4
14 2 5
15 2 6
16 2 7
17 2 8
18 2 9
19 2 10
20 3 1
21 3 2
22 3 3
23 3 4
24 3 5
25 3 6
26 3 7
27 3 8
28 3 9
29 3 10
30 4 1
31 4 2
32 4 3
33 4 4
34 4 5
35 4 6
36 4 7
37 4 8
38 4 9
39 4 10
40 5 1
41 5 2
42 5 3
43 5 4
44 5 5
45 5 6
46 5 7
47 5 8
48 5 9
49 5 10
50 6 1
51 6 2
52 6 3
53 6 4
54 6 5
55 6 6
56 6 7
57 6 8
58 6 9
59 6 10
60 7 1
61 7 2
62 7 3
63 7 4
64 7 5
65 7 6
66 7 7
67 7 8
68 7 9
69 7 10
70 8 1
71 8 2
72 8 3
73 8 4
74 8 5
75 8 6
76 8 7
77 8 8
78 8 9
79 8 10
80 9 1
81 9 2
82 9 3
83 9 4
84 9 5
85 9 6
86 9 7
87 9 8
88 9 9
89 9 10
90 10 1
91 10 2
92 10 3
93 10 4
94 10 5
95 10 6
96 10 7
97 10 8
98 10 9
99 10 10
ベンチマークを回します。
res_mb <-
df_condition |>
dplyr::mutate(
res = purrr::pmap(
.l = list(
x = nr,
y = nc
),
.f = \(x, y) {
elements <- runif(x * y)
microbenchmark::microbenchmark(
"matrix" = matrix(elements, nrow = x),
"dim" = `dim<-`(elements, c(x, y)),
check = "equal"
)
},
.progress = list(
clear = FALSE,
show_after = 0,
format = "{cli::pb_bar} ({cli::pb_percent} in {cli::pb_elapsed})|{cli::pb_eta_str}"
)
) |>
1 setNames(paste0("r", nr, "c", nc))
)- 1
-
purrr::pmapの戻り値を名前付きにしておくと、後でggplot2::autoplot()するときとかに便利。
■ ( 0% in 1ms)|
■■■■ ( 10% in 895ms)|ETA: 8s
■■■■■■■■■■■■■■■ ( 45% in 3.9s)|ETA: 5s
■■■■■■■■■■■■■■■■■■■■■■■■■■ ( 82% in 6.9s)|ETA: 2s
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ (100% in 8.5s)|ETA: 0s
結果は以下の通りです。まとめるのが面倒なのでそのまま出します。
$r1c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 1136.98 801.5 1101 11602 100 a
dim 200 202 359.00 301.0 302 4001 100 b
$r1c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 908.94 801 901 4201 100 a
dim 200 202 311.06 301 301 2801 100 b
$r1c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 943.99 901 1000.5 4901 100 a
dim 200 301 322.05 301 302.0 1701 100 b
$r1c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 1001 1206.01 1101 1102 7202 100 a
dim 201 301 411.97 401 401 2400 100 b
$r1c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 947.0 901 901 5800 100 a
dim 200 300 314.1 301 302 1701 100 b
$r1c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 900.00 801 901 4501 100 a
dim 200 301 332.04 301 302 3000 100 b
$r1c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 938.05 801 902 5901 100 a
dim 200 202 300.01 301 301 1901 100 b
$r1c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 964.91 901 1001 3602 100 a
dim 200 300 336.00 301 302 1901 100 b
$r1c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 926.02 802 901.5 4901 100 a
dim 200 300 312.03 301 301.0 1700 100 b
$r2c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 911.01 801 951.5 3501 100 a
dim 200 201 290.98 301 301.0 2301 100 b
$r2c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 922.99 802 901 3802 100 a
dim 200 202 302.05 301 302 1900 100 b
$r2c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 940.01 901 902 3900 100 a
dim 200 300 308.95 301 301 1601 100 b
$r2c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1001 1163.05 1101 1102 5702 100 a
dim 300 301 445.98 401 401 3802 100 b
$r2c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 918.94 900.5 1000 2601 100 a
dim 200 300 312.05 301.0 302 2201 100 b
$r2c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1158.99 1002 1200 7802 100 a
dim 201 301 399.00 401 401 2500 100 b
$r2c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 913.03 802 901.5 3102 100 a
dim 200 301 337.97 301 401.0 2501 100 b
$r2c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 909.03 900.5 901 3101 100 a
dim 200 300 315.93 301.0 302 1902 100 b
$r2c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1021.98 901 1001 5201 100 a
dim 200 400 638.97 401 402 18200 100 b
$r2c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 900 1087.99 901 1001 9701 100 a
dim 200 301 426.02 401 401 3201 100 b
$r3c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 908.11 851 1000 3502 100 a
dim 200 202 292.90 301 301 1301 100 b
$r3c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 900.94 851 901 2900 100 a
dim 200 300 326.05 301 302 2300 100 b
$r3c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 943.04 901 1001 3701 100 a
dim 200 301 339.06 301 302 2501 100 b
$r3c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 929.96 900 951 5101 100 a
dim 200 300 342.94 301 302 3501 100 b
$r3c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 943.08 802 901 5902 100 a
dim 200 301 323.04 301 302 1701 100 b
$r3c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1271.99 1000 1101.0 14101 100 a
dim 300 301 450.99 401 401.5 3502 100 b
$r3c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1151.02 1000 1101 10001 100 a
dim 300 301 456.91 401 401 3802 100 b
$r3c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1101 1289.13 1200.5 1202 7702 100 a
dim 300 401 762.93 500.0 501 9901 100 b
$r3c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1168.89 901 1002 10201 100 a
dim 200 301 495.99 401 401 3901 100 b
$r3c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1096.98 902 1051 4601 100 a
dim 300 301 577.96 401 451 9501 100 b
$r4c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 938.94 901 901 5500 100 a
dim 200 300 312.02 301 301 1902 100 b
$r4c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 905.97 801 901.0 3902 100 a
dim 200 300 303.10 301 301.5 1502 100 b
$r4c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801.0 909.06 900 901 3802 100 a
dim 200 300.5 317.02 301 302 1301 100 b
$r4c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 933.92 851 951 4101 100 a
dim 200 300 336.91 301 400 2901 100 b
$r4c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1033.01 901 1001.5 3502 100 a
dim 201 301 526.02 401 401.0 12401 100 b
$r4c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1107.03 901 1101 4901 100 a
dim 300 301 557.97 401 401 12801 100 b
$r4c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1212.00 902 1001 9701 100 a
dim 201 301 471.03 401 401 2601 100 b
$r4c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1198.90 1000.5 1051 12402 100 a
dim 201 301 475.04 401.0 401 4500 100 b
$r4c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1114.10 902 1001 13002 100 a
dim 201 301 445.06 401 401 3001 100 b
$r4c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 901 1101 1621.02 1201 1350.5 15701 100 a
dim 300 401 740.00 500 600.5 6402 100 b
$r5c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 900 1001 1149.03 1100 1102 5701 100 a
dim 202 301 431.02 401 401 3801 100 b
$r5c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 946.04 901 1001 4401 100 a
dim 200 300 315.89 301 301 1401 100 b
$r5c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 934.01 901 901 4601 100 a
dim 200 300 316.93 301 302 1901 100 b
$r5c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1117.97 901 1001 10301 100 a
dim 201 301 398.03 351 401 3401 100 b
$r5c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1051.98 1000 1002 6301 100 a
dim 200 301 505.99 401 401 11100 100 b
$r5c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1179.98 1000.5 1101 6801 100 a
dim 202 301 578.07 401.0 401 8701 100 b
$r5c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1088.98 1000 1100.5 6501 100 a
dim 300 301 474.07 401 401.0 8901 100 b
$r5c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1399.05 1001 1101.5 17601 100 a
dim 201 301 450.00 401 401.0 5401 100 b
$r5c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1099.06 1001 1101 4101 100 a
dim 200 351 554.97 401 401 11900 100 b
$r5c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1223.01 1000 1101 12101 100 a
dim 201 301 541.01 401 401 3301 100 b
$r6c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 1000.5 1182.03 1101 1200 6700 100 a
dim 200 301.0 410.97 401 401 1801 100 b
$r6c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1000.5 1135.05 1001 1101 5601 100 a
dim 300 301.0 433.00 401 401 4101 100 b
$r6c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901.0 1207.04 902 1101 9802 100 a
dim 300 301.5 444.02 401 401 2601 100 b
$r6c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1179.00 1001 1101 11400 100 a
dim 201 301 462.02 401 401 3001 100 b
$r6c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1130.05 902 1002 6501 100 a
dim 201 301 563.03 401 401 3601 100 b
$r6c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1035.92 902 1001 5701 100 a
dim 300 301 505.17 401 401 10801 100 b
$r6c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1073.06 1000 1100 4201 100 a
dim 201 301 553.97 401 401 8800 100 b
$r6c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1189.02 1001 1100.5 6301 100 a
dim 300 302 467.00 401 401.0 2500 100 b
$r6c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1340.00 1001 1102 9601 100 a
dim 200 301 453.95 401 401 4001 100 b
$r6c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1160.06 1001 1101.5 4901 100 a
dim 200 301 613.92 401 401.0 12401 100 b
$r7c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 700 801 937.96 901 1001 4002 100 a
dim 200 300 333.98 301 302 3201 100 b
$r7c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 962.04 802 1000 6402 100 a
dim 200 300 323.96 301 302 2200 100 b
$r7c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 900 1101 1329.96 1200.5 1300.5 5401 100 a
dim 300 401 691.98 450.5 502.0 8501 100 b
$r7c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1194.02 902 1100 7601 100 a
dim 200 301 455.02 301 401 2501 100 b
$r7c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1132.00 901.5 1002 7302 100 a
dim 201 301 486.06 401.0 401 11101 100 b
$r7c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1038.04 951 1001 4500 100 a
dim 300 301 488.03 401 401 5201 100 b
$r7c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901.0 1246.02 1000 1101 11802 100 a
dim 200 301.5 534.98 401 401 2802 100 b
$r7c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1295.98 1001 1101 10402 100 a
dim 201 301 569.03 401 401 4701 100 b
$r7c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1192.05 1001 1101 5801 100 a
dim 201 301 542.96 401 401 2701 100 b
$r7c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1193.92 1001 1100.5 6801 100 a
dim 300 301 625.01 401 401.0 8101 100 b
$r8c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 898.99 801 901 3401 100 a
dim 200 300 320.03 301 301 2401 100 b
$r8c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801 907.00 900 901 3701 100 a
dim 200 301 330.99 301 302 1601 100 b
$r8c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1066.98 901 1002 5401 100 a
dim 201 301 503.09 400 401 11901 100 b
$r8c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1123.97 901 1001 7300 100 a
dim 201 301 405.08 401 401 2401 100 b
$r8c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1001 1475.05 1102 1301 11402 100 a
dim 300 401 779.00 401 501 7600 100 b
$r8c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1100 1421.09 1102 1301 8601 100 a
dim 301 401 688.00 500 501 8701 100 b
$r8c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1279.92 1001 1151 7301 100 a
dim 201 301 672.97 401 401 15200 100 b
$r8c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1093.98 951 1051 6601 100 a
dim 300 301 504.98 401 401 8300 100 b
$r8c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1443.99 1001 1102 18301 100 a
dim 300 302 659.06 401 451 6501 100 b
$r8c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901.0 1368.95 1001 1101.5 12401 100 a
dim 300 301.5 711.99 401 501.0 6302 100 b
$r9c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 801 911.02 851 901.5 3401 100 a
dim 200 300 324.01 301 302.0 1201 100 b
$r9c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1142.04 951 1100 13300 100 a
dim 300 302 490.07 401 401 8301 100 b
$r9c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1285.01 1001 1102 8901 100 a
dim 300 301 456.06 401 401 2602 100 b
$r9c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901.0 1174.01 1001 1100.5 11201 100 a
dim 201 301.5 419.09 401 401.0 2801 100 b
$r9c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 1001 1151 2450.02 1201 1400 88300 100 a
dim 300 401 678.99 501 502 9001 100 b
$r9c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1195.94 1001 1151.0 6700 100 a
dim 201 301 662.95 401 401.5 8701 100 b
$r9c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 1002 1469.02 1200.0 1301 7901 100 a
dim 300 401 568.00 401.5 501 5201 100 b
$r9c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1326.02 1001 1101 17201 100 a
dim 300 301 568.01 401 401 4801 100 b
$r9c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901.0 1296.97 1001.5 1200 5701 100 a
dim 300 351.5 821.01 401.0 500 14001 100 b
$r9c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1176.96 1001 1101 4802 100 a
dim 200 302 783.03 401 500 13201 100 b
$r10c1
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 701 801.0 940.04 901 901.5 5202 100 a
dim 200 300.5 356.05 301 302.0 4601 100 b
$r10c2
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1349.96 1001 1101 16100 100 a
dim 200 301 843.93 401 601 31902 100 a
$r10c3
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1558.02 1101 1901 8501 100 a
dim 300 351 663.03 401 701 5901 100 b
$r10c4
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1292.01 1001 1101 10401 100 a
dim 200 301 548.00 401 402 5500 100 b
$r10c5
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1238.09 1001 1101 7401 100 a
dim 200 301 685.03 401 500 5701 100 b
$r10c6
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1119.99 1000 1101.0 5101 100 a
dim 200 301 602.00 401 401.5 7301 100 b
$r10c7
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1236.04 1001 1101.5 6301 100 a
dim 202 301 635.96 401 401.0 6802 100 b
$r10c8
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1484.03 1001 1102 7601 100 a
dim 202 302 746.99 401 501 8100 100 b
$r10c9
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 800 901 1256.02 1001 1102 5701 100 a
dim 201 301 786.94 401 501 14101 100 b
$r10c10
Unit: nanoseconds
expr min lq mean median uq max neval cld
matrix 801 901 1406.94 1001 1202 8202 100 a
dim 300 302 595.04 401 501 5901 100 b
どの条件でもmatrix()に比べて`dim<-`()の方が2~3倍くらい速いですが、結果の単位はナノ秒なので、1回実行するときには体感できる差はないですね。何万回以上も回すとなると地味に変わってくると思います。
結果のグラフ化も可能です。列数ごとに分割してggplot2::autoplotを使います。microbenchmark::microbenchmark()の戻り値に対してはmicrobenchmark:::autoplot.microbenchmark()が適用されます。
- 1
-
autoplot.microbenchmark()内部の関数がggplot2側でdeprecatedになったものがあるらしく、警告が出ます。 - 2
-
purrr::map()にすると、戻り値がレンダリングされるので、purrr::walk()にしてplotだけ無理やり表示させました。










どの条件でも`dim<-`()の方が速いように見受けられます。
bench
最近使い方をなんとなく覚えてきました。bench::mark()はベンチマーク時間を設定できたり、メモリアロケーションも出してくれたりするところがいいです。あと、デフォルトで出力が一致しているかどうかチェックしてくれます。bench::mark()を複数条件で実行したいときはbench::press()を併用します。bench::press()では、パラメーター(条件)を名前付きで指定して、名前なしのexprとして{}の中でbench::mark()を実行するという感じです。普通にやるならこんな感じになると思います。
- 1
-
条件を列挙する。
tidyr::expand_grid()っぽく展開される(けど、中で使っているのはexpand.grid()だった)。 - 2
-
{}の中では単発のbench::mark()っぽく記述すればいいです。
今回は条件のdfをすでに作ってあるので、それを引数.gridに渡せばいいです。
結果は以下の通りです。まとめるのが面倒なのでそのまま出します。
# A tibble: 198 × 15
expression nr nc min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time
<bch:expr> <int> <int> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm>
1 matrix 1 2 900ns 1.1µs 701901. 0B 70.2 9999 1 14.25ms
2 dim 1 2 300ns 400ns 2231097. 0B 0 10000 0 4.48ms
3 matrix 1 3 900ns 1.1µs 854168. 0B 0 10000 0 11.71ms
4 dim 1 3 300ns 400ns 2221877. 0B 0 10000 0 4.5ms
5 matrix 1 4 900ns 1µs 781665. 0B 0 10000 0 12.79ms
6 dim 1 4 300ns 400ns 2188950. 0B 0 10000 0 4.57ms
7 matrix 1 5 900ns 1.1µs 750698. 0B 75.1 9999 1 13.32ms
8 dim 1 5 300ns 400ns 2167317. 0B 0 10000 0 4.61ms
9 matrix 1 6 900ns 1µs 860178. 0B 0 10000 0 11.63ms
10 dim 1 6 300ns 400ns 2161321. 0B 0 10000 0 4.63ms
11 matrix 1 7 900ns 1µs 866048. 0B 0 10000 0 11.55ms
12 dim 1 7 300ns 400ns 2127886. 0B 0 10000 0 4.7ms
13 matrix 1 8 900ns 1.1µs 743100. 0B 74.3 9999 1 13.46ms
14 dim 1 8 300ns 500ns 1980355. 0B 0 10000 0 5.05ms
15 matrix 1 9 900ns 1.1µs 771409. 0B 0 10000 0 12.96ms
16 dim 1 9 300ns 400ns 2112869. 0B 0 10000 0 4.73ms
17 matrix 1 10 900ns 1µs 824328. 0B 0 10000 0 12.13ms
18 dim 1 10 300ns 400ns 2159547. 0B 0 10000 0 4.63ms
19 matrix 2 1 900ns 1.1µs 675394. 0B 67.5 9999 1 14.8ms
20 dim 2 1 300ns 400ns 2059138. 0B 0 10000 0 4.86ms
21 matrix 2 2 900ns 1.1µs 824701. 0B 0 10000 0 12.13ms
22 dim 2 2 300ns 400ns 2177226. 0B 0 10000 0 4.59ms
23 matrix 2 3 900ns 1.1µs 844238. 0B 0 10000 0 11.85ms
24 dim 2 3 300ns 400ns 1972503. 0B 0 10000 0 5.07ms
25 matrix 2 4 900ns 1.2µs 668186. 0B 66.8 9999 1 14.96ms
26 dim 2 4 300ns 600ns 1501840. 0B 0 10000 0 6.66ms
27 matrix 2 5 900ns 1.2µs 736529. 0B 0 10000 0 13.58ms
28 dim 2 5 300ns 400ns 1965100. 0B 0 10000 0 5.09ms
29 matrix 2 6 900ns 1.1µs 849156. 0B 0 10000 0 11.78ms
30 dim 2 6 300ns 400ns 2143026. 0B 0 10000 0 4.67ms
31 matrix 2 7 900ns 1.1µs 846711. 0B 0 10000 0 11.81ms
32 dim 2 7 300ns 400ns 2243712. 0B 0 10000 0 4.46ms
33 matrix 2 8 900ns 1.1µs 803123. 0B 0 10000 0 12.45ms
34 dim 2 8 300ns 600ns 1528958. 0B 0 10000 0 6.54ms
35 matrix 2 9 900ns 1.3µs 647119. 192B 0 10000 0 15.45ms
36 dim 2 9 300ns 500ns 1972192. 192B 0 10000 0 5.07ms
37 matrix 2 10 900ns 1.1µs 813398. 208B 0 10000 0 12.29ms
38 dim 2 10 300ns 500ns 2021509. 208B 0 10000 0 4.95ms
39 matrix 3 1 900ns 1.1µs 877894. 0B 0 10000 0 11.39ms
40 dim 3 1 300ns 400ns 2206191. 0B 0 10000 0 4.53ms
41 matrix 3 2 900ns 1.1µs 846568. 0B 0 10000 0 11.81ms
42 dim 3 2 300ns 400ns 2170704. 0B 0 10000 0 4.61ms
43 matrix 3 3 900ns 1.7µs 575307. 0B 57.5 9999 1 17.38ms
44 dim 3 3 300ns 400ns 2176847. 0B 0 10000 0 4.59ms
45 matrix 3 4 900ns 1.1µs 844096. 0B 0 10000 0 11.85ms
46 dim 3 4 300ns 400ns 2096964. 0B 0 10000 0 4.77ms
47 matrix 3 5 900ns 1.1µs 829187. 0B 0 10000 0 12.06ms
48 dim 3 5 300ns 400ns 2185745. 0B 0 10000 0 4.58ms
49 matrix 3 6 900ns 1.1µs 836148. 192B 0 10000 0 11.96ms
50 dim 3 6 300ns 400ns 2001161. 192B 0 10000 0 5ms
51 matrix 3 7 900ns 1.7µs 553618. 216B 55.4 9999 1 18.06ms
52 dim 3 7 300ns 500ns 1998361. 216B 0 10000 0 5ms
53 matrix 3 8 900ns 1.2µs 799949. 240B 0 10000 0 12.5ms
54 dim 3 8 300ns 500ns 1996287. 240B 0 10000 0 5.01ms
55 matrix 3 9 900ns 1.2µs 812137. 264B 0 10000 0 12.31ms
56 dim 3 9 300ns 500ns 1948520. 264B 0 10000 0 5.13ms
57 matrix 3 10 900ns 1.2µs 805542. 288B 0 10000 0 12.41ms
58 dim 3 10 300ns 500ns 2030251. 288B 0 10000 0 4.93ms
59 matrix 4 1 900ns 1.6µs 593117. 0B 59.3 9999 1 16.86ms
60 dim 4 1 300ns 400ns 2122106. 0B 0 10000 0 4.71ms
61 matrix 4 2 900ns 1.1µs 855074. 0B 0 10000 0 11.7ms
62 dim 4 2 300ns 400ns 2163472. 0B 0 10000 0 4.62ms
63 matrix 4 3 900ns 1.1µs 857574. 0B 0 10000 0 11.66ms
64 dim 4 3 300ns 400ns 2257336. 0B 0 10000 0 4.43ms
65 matrix 4 4 900ns 1.1µs 860689. 0B 0 10000 0 11.62ms
66 dim 4 4 300ns 400ns 2144404. 0B 0 10000 0 4.66ms
67 matrix 4 5 900ns 1.7µs 567483. 208B 56.8 9999 1 17.62ms
68 dim 4 5 300ns 500ns 2008355. 208B 0 10000 0 4.98ms
69 matrix 4 6 900ns 1.2µs 776512. 240B 0 10000 0 12.88ms
70 dim 4 6 300ns 500ns 1909600. 240B 0 10000 0 5.24ms
71 matrix 4 7 900ns 1.1µs 826918. 272B 0 10000 0 12.09ms
72 dim 4 7 300ns 500ns 1953545. 272B 0 10000 0 5.12ms
73 matrix 4 8 900ns 1.1µs 823757. 304B 0 10000 0 12.14ms
74 dim 4 8 300ns 600ns 1555030. 304B 156. 9999 1 6.43ms
75 matrix 4 9 900ns 1.4µs 619702. 336B 0 10000 0 16.14ms
76 dim 4 9 300ns 500ns 1994973. 336B 0 10000 0 5.01ms
77 matrix 4 10 900ns 1.2µs 805354. 368B 0 10000 0 12.42ms
78 dim 4 10 300ns 500ns 1986334. 368B 0 10000 0 5.03ms
79 matrix 5 1 900ns 1.1µs 862723. 0B 0 10000 0 11.59ms
80 dim 5 1 300ns 600ns 1506342. 0B 0 10000 0 6.64ms
81 matrix 5 2 900ns 1.2µs 690884. 0B 0 10000 0 14.47ms
82 dim 5 2 300ns 400ns 2161274. 0B 0 10000 0 4.63ms
83 matrix 5 3 900ns 1.1µs 841694. 0B 0 10000 0 11.88ms
84 dim 5 3 300ns 400ns 2261574. 0B 0 10000 0 4.42ms
85 matrix 5 4 900ns 1.1µs 818532. 208B 0 10000 0 12.22ms
86 dim 5 4 300ns 500ns 2010656. 208B 0 10000 0 4.97ms
87 matrix 5 5 900ns 1.3µs 642514. 248B 64.3 9999 1 15.56ms
88 dim 5 5 300ns 700ns 1379158. 248B 0 10000 0 7.25ms
89 matrix 5 6 900ns 1.2µs 769213. 288B 0 10000 0 13ms
90 dim 5 6 300ns 500ns 2020529. 288B 0 10000 0 4.95ms
91 matrix 5 7 900ns 1.2µs 804039. 328B 0 10000 0 12.44ms
92 dim 5 7 300ns 500ns 1952248. 328B 0 10000 0 5.12ms
93 matrix 5 8 900ns 1.6µs 576532. 368B 0 10000 0 17.34ms
94 dim 5 8 300ns 500ns 1990129. 368B 0 10000 0 5.03ms
95 matrix 5 9 900ns 1.2µs 784203. 408B 0 10000 0 12.75ms
96 dim 5 9 300ns 500ns 1945444. 408B 195. 9999 1 5.14ms
97 matrix 5 10 900ns 1.6µs 565764. 448B 0 10000 0 17.68ms
98 dim 5 10 300ns 500ns 1835064. 448B 0 10000 0 5.45ms
99 matrix 6 1 900ns 1.1µs 848752. 0B 0 10000 0 11.78ms
100 dim 6 1 300ns 400ns 2240394. 0B 0 10000 0 4.46ms
101 matrix 6 2 900ns 1µs 887530. 0B 88.8 9999 1 11.27ms
102 dim 6 2 300ns 400ns 2581245. 0B 0 10000 0 3.87ms
103 matrix 6 3 900ns 1µs 893919. 192B 0 10000 0 11.19ms
104 dim 6 3 300ns 400ns 1908215. 192B 0 10000 0 5.24ms
105 matrix 6 4 900ns 1µs 856311. 240B 0 10000 0 11.68ms
106 dim 6 4 300ns 400ns 2139037. 240B 0 10000 0 4.67ms
107 matrix 6 5 900ns 1.1µs 835233. 288B 0 10000 0 11.97ms
108 dim 6 5 300ns 400ns 2078916. 288B 0 10000 0 4.81ms
109 matrix 6 6 900ns 1µs 842865. 336B 0 10000 0 11.86ms
110 dim 6 6 300ns 400ns 2098988. 336B 0 10000 0 4.76ms
111 matrix 6 7 900ns 1.1µs 833583. 384B 0 10000 0 12ms
112 dim 6 7 300ns 400ns 2086811. 384B 0 10000 0 4.79ms
113 matrix 6 8 900ns 1.1µs 837052. 432B 83.7 9999 1 11.95ms
114 dim 6 8 300ns 400ns 2017919. 432B 0 10000 0 4.96ms
115 matrix 6 9 900ns 1.1µs 857207. 480B 0 10000 0 11.67ms
116 dim 6 9 300ns 400ns 2083637. 480B 0 10000 0 4.8ms
117 matrix 6 10 900ns 1.1µs 857765. 528B 0 10000 0 11.66ms
118 dim 6 10 300ns 400ns 2041275. 528B 0 10000 0 4.9ms
119 matrix 7 1 900ns 1µs 937488. 0B 0 10000 0 10.67ms
120 dim 7 1 300ns 400ns 2362949. 0B 0 10000 0 4.23ms
121 matrix 7 2 900ns 1µs 957818. 0B 0 10000 0 10.44ms
122 dim 7 2 300ns 400ns 2340002. 0B 0 10000 0 4.27ms
123 matrix 7 3 900ns 1.1µs 764631. 216B 0 10000 0 13.08ms
124 dim 7 3 300ns 500ns 1797753. 216B 0 10000 0 5.56ms
125 matrix 7 4 900ns 1.1µs 828370. 272B 82.8 9999 1 12.07ms
126 dim 7 4 300ns 400ns 2198334. 272B 0 10000 0 4.55ms
127 matrix 7 5 900ns 1.1µs 838385. 328B 0 10000 0 11.93ms
128 dim 7 5 300ns 400ns 2004771. 328B 0 10000 0 4.99ms
129 matrix 7 6 900ns 1.1µs 874508. 384B 0 10000 0 11.44ms
130 dim 7 6 300ns 400ns 2129835. 384B 0 10000 0 4.7ms
131 matrix 7 7 900ns 1.1µs 831076. 440B 0 10000 0 12.03ms
132 dim 7 7 300ns 500ns 1913949. 440B 0 10000 0 5.22ms
133 matrix 7 8 900ns 1.1µs 827671. 496B 0 10000 0 12.08ms
134 dim 7 8 300ns 500ns 1820300. 496B 0 10000 0 5.49ms
135 matrix 7 9 900ns 1.1µs 833820. 552B 0 10000 0 11.99ms
136 dim 7 9 300ns 500ns 1926590. 552B 193. 9999 1 5.19ms
137 matrix 7 10 900ns 1.1µs 849553. 608B 0 10000 0 11.77ms
138 dim 7 10 300ns 400ns 2093452. 608B 0 10000 0 4.78ms
139 matrix 8 1 900ns 1µs 941797. 0B 0 10000 0 10.62ms
140 dim 8 1 300ns 400ns 2572281. 0B 0 10000 0 3.89ms
141 matrix 8 2 900ns 1µs 957671. 0B 0 10000 0 10.44ms
142 dim 8 2 300ns 400ns 2314761. 0B 0 10000 0 4.32ms
143 matrix 8 3 900ns 1.1µs 794976. 240B 0 10000 0 12.58ms
144 dim 8 3 300ns 400ns 2066372. 240B 0 10000 0 4.84ms
145 matrix 8 4 900ns 1.1µs 846360. 304B 0 10000 0 11.81ms
146 dim 8 4 300ns 500ns 1897353. 304B 0 10000 0 5.27ms
147 matrix 8 5 900ns 1.1µs 833250. 368B 0 10000 0 12ms
148 dim 8 5 300ns 500ns 1962705. 368B 196. 9999 1 5.09ms
149 matrix 8 6 900ns 1µs 889150. 432B 0 10000 0 11.25ms
150 dim 8 6 300ns 400ns 2142658. 432B 0 10000 0 4.67ms
151 matrix 8 7 900ns 1.1µs 865928. 496B 0 10000 0 11.55ms
152 dim 8 7 300ns 400ns 1868775. 496B 0 10000 0 5.35ms
153 matrix 8 8 900ns 1.1µs 791127. 560B 0 10000 0 12.64ms
154 dim 8 8 300ns 500ns 1854806. 560B 0 10000 0 5.39ms
155 matrix 8 9 900ns 1.1µs 791621. 624B 0 10000 0 12.63ms
156 dim 8 9 300ns 500ns 1772998. 624B 177. 9999 1 5.64ms
157 matrix 8 10 900ns 1.1µs 790733. 688B 0 10000 0 12.65ms
158 dim 8 10 300ns 500ns 1824351. 688B 0 10000 0 5.48ms
159 matrix 9 1 900ns 1µs 887642. 0B 0 10000 0 11.27ms
160 dim 9 1 300ns 400ns 2254283. 0B 0 10000 0 4.44ms
161 matrix 9 2 900ns 1.1µs 831801. 192B 0 10000 0 12.02ms
162 dim 9 2 300ns 500ns 1798561. 192B 0 10000 0 5.56ms
163 matrix 9 3 900ns 1.1µs 784911. 264B 0 10000 0 12.74ms
164 dim 9 3 300ns 500ns 1848053. 264B 0 10000 0 5.41ms
165 matrix 9 4 900ns 1.1µs 750345. 336B 0 10000 0 13.33ms
166 dim 9 4 300ns 500ns 1798593. 336B 0 10000 0 5.56ms
167 matrix 9 5 900ns 1.1µs 790229. 408B 79.0 9999 1 12.65ms
168 dim 9 5 300ns 400ns 2005093. 408B 0 10000 0 4.99ms
169 matrix 9 6 900ns 1.1µs 812176. 480B 0 10000 0 12.31ms
170 dim 9 6 300ns 600ns 1378721. 480B 0 10000 0 7.25ms
171 matrix 9 7 900ns 1.1µs 799910. 552B 0 10000 0 12.5ms
172 dim 9 7 300ns 400ns 1978905. 552B 0 10000 0 5.05ms
173 matrix 9 8 900ns 1.1µs 839306. 624B 0 10000 0 11.91ms
174 dim 9 8 300ns 500ns 1693996. 624B 0 10000 0 5.9ms
175 matrix 9 9 900ns 1.1µs 800093. 696B 80.0 9999 1 12.5ms
176 dim 9 9 300ns 500ns 1803622. 696B 0 10000 0 5.54ms
177 matrix 9 10 900ns 1.1µs 797327. 768B 0 10000 0 12.54ms
178 dim 9 10 300ns 500ns 1832945. 768B 0 10000 0 5.46ms
179 matrix 10 1 900ns 1µs 938782. 0B 0 10000 0 10.65ms
180 dim 10 1 300ns 400ns 2211607. 0B 0 10000 0 4.52ms
181 matrix 10 2 900ns 1.1µs 752049. 208B 0 10000 0 13.3ms
182 dim 10 2 300ns 400ns 2084202. 208B 0 10000 0 4.8ms
183 matrix 10 3 900ns 1.1µs 789137. 288B 78.9 9999 1 12.67ms
184 dim 10 3 300ns 400ns 2044321. 288B 0 10000 0 4.89ms
185 matrix 10 4 900ns 1.1µs 793537. 368B 0 10000 0 12.6ms
186 dim 10 4 300ns 400ns 1962054. 368B 0 10000 0 5.1ms
187 matrix 10 5 900ns 1.1µs 785114. 448B 0 10000 0 12.74ms
188 dim 10 5 300ns 400ns 1963903. 448B 0 10000 0 5.09ms
189 matrix 10 6 900ns 1.1µs 779277. 528B 0 10000 0 12.83ms
190 dim 10 6 300ns 500ns 1758798. 528B 0 10000 0 5.69ms
191 matrix 10 7 900ns 1.1µs 804360. 608B 80.4 9999 1 12.43ms
192 dim 10 7 300ns 400ns 1926077. 608B 0 10000 0 5.19ms
193 matrix 10 8 900ns 1.1µs 799527. 688B 0 10000 0 12.51ms
194 dim 10 8 300ns 500ns 1829759. 688B 0 10000 0 5.46ms
195 matrix 10 9 900ns 1.1µs 823005. 768B 0 10000 0 12.15ms
196 dim 10 9 300ns 500ns 1897091. 768B 190. 9999 1 5.27ms
197 matrix 10 10 900ns 1.1µs 793626. 848B 0 10000 0 12.6ms
198 dim 10 10 300ns 500ns 1904182. 848B 0 10000 0 5.25ms
# ℹ 4 more variables: result <list>, memory <list>, time <list>, gc <list>
結果は先ほどと変わらず、単位は相変わらずナノ秒とか1桁マイクロ秒ですが、どの条件でもmatrix()より`dim<-`()の方が2~3倍くらい速いです。
こちらも結果のグラフ化が可能です。先ほどと同じくautoplot()で自動的に出来上がります。bench::mark()の戻り値にはbench:::autoplot.bench_mark()が適用されます。
res_bm |>
dplyr::filter(nr == 1) |>
1 ggplot2::autoplot()- 1
-
引数
typeで描画方法をいろいろ指定できます。ただし、デフォルトの"beeswarm"はggbeeswarmパッケージ、"ridge"はggridgesパッケージが必要です。インストールされてない場合は、インストールするかどうか聞いてきます。

条件がいくつかある場合は自動でfacetに分けてくれるようです。ただこのまま列に条件がすべて入るとちょっと見にくいので、これも行ごとに分割してもう少し見やすくしてみます。内部ですでにggplot2::facet_*()が使われているのですが、上書きしてしまいます。2
$r1

$r2

$r3

$r4

$r5

$r6

$r7

$r8

$r9

$r10

どの条件でも`dim<-`()の方が速いですね。
Supplyment
chr
念のため文字型でもやってみます。時間削減で10行だけやります。文字列は一文字のベクトルであるlettersでもよかったんですが、せっかくなので短めの文章が集められているstringr::sentencesを使ってみたいと思います。
# A tibble: 20 × 8
expression nr nc min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <int> <int> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 matrix 10 1 900ns 1.2µs 679538. 0B 68.0
2 dim 10 1 300ns 500ns 1939413. 0B 0
3 matrix 10 2 1.2µs 1.5µs 635469. 208B 0
4 dim 10 2 300ns 500ns 1631508. 208B 0
5 matrix 10 3 1.2µs 1.5µs 586820. 288B 0
6 dim 10 3 400ns 500ns 1562061. 288B 0
7 matrix 10 4 1µs 1.3µs 695652. 368B 0
8 dim 10 4 300ns 500ns 1646145. 368B 0
9 matrix 10 5 1µs 1.2µs 712261. 448B 0
10 dim 10 5 300ns 500ns 1853396. 448B 0
11 matrix 10 6 1.1µs 1.4µs 623375. 528B 62.3
12 dim 10 6 300ns 500ns 1619722. 528B 0
13 matrix 10 7 1.3µs 1.6µs 580616. 608B 0
14 dim 10 7 400ns 500ns 1666667. 608B 0
15 matrix 10 8 1.3µs 1.6µs 563812. 688B 0
16 dim 10 8 400ns 500ns 1639882. 688B 0
17 matrix 10 9 1.2µs 1.4µs 628472. 768B 0
18 dim 10 9 300ns 500ns 1744348. 768B 0
19 matrix 10 10 1.2µs 1.4µs 619276. 848B 0
20 dim 10 10 400ns 500ns 1675154. 848B 0
文字型でも`dim<-`()の方が速いですね。
logical
論理値も念のため。
# A tibble: 20 × 8
expression nr nc min median `itr/sec` mem_alloc `gc/sec`
<bch:expr> <int> <int> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
1 matrix 10 1 900ns 1.7µs 542576. 0B 0
2 dim 10 1 300ns 500ns 1700362. 0B 0
3 matrix 10 2 1µs 1.4µs 618624. 0B 0
4 dim 10 2 400ns 500ns 1501163. 0B 0
5 matrix 10 3 900ns 1.3µs 637259. 0B 0
6 dim 10 3 300ns 400ns 1824818. 0B 0
7 matrix 10 4 900ns 1.2µs 688629. 208B 0
8 dim 10 4 300ns 500ns 1696410. 208B 0
9 matrix 10 5 900ns 1.5µs 538585. 248B 53.9
10 dim 10 5 300ns 600ns 1469767. 248B 0
11 matrix 10 6 1.1µs 1.5µs 505410. 288B 0
12 dim 10 6 400ns 500ns 1546145. 288B 0
13 matrix 10 7 1.1µs 1.4µs 579287. 328B 0
14 dim 10 7 400ns 500ns 1641524. 328B 0
15 matrix 10 8 900ns 1.1µs 780043. 368B 0
16 dim 10 8 300ns 500ns 1954461. 368B 0
17 matrix 10 9 900ns 1.1µs 818371. 408B 0
18 dim 10 9 300ns 500ns 1928380. 408B 0
19 matrix 10 10 900ns 1.3µs 721267. 448B 72.1
20 dim 10 10 400ns 500ns 1741826. 448B 0
論理値型でも`dim<-`()の方が速いですね。
Conclusion
ベクトルから行列を作るときの速さをmatrix()と`dim<-`()で比べてみました。1×1を除く10×10の行列の作成までベンチマークを回してみましたが、matrix()は大体1~1.1マイクロ秒、`dim<-`()は大体0.4~0.5マイクロ秒ということで、後者の方が速かったです。
…といっても普段の分析で使うときに体感できる差ではないので、ふつうに使うなら分かりやすいし行方向にも値を入れられるmatrix()を使うのがいいと思います。シミュレーションで行列を大量に作る際に、要素をbyrowで入れる必要がなく少しでも時間を節約したいなら`dim<-`()を使うのもいいかもしれません。(でも、おそらく他の部分とか関数の方がボトルネックになると思います…。)
Session Infomation
R version 4.5.3 (2026-03-11 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=Japanese_Japan.utf8 LC_CTYPE=Japanese_Japan.utf8 LC_MONETARY=Japanese_Japan.utf8
[4] LC_NUMERIC=C LC_TIME=Japanese_Japan.utf8
time zone: Asia/Tokyo
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] bench_1.1.4 microbenchmark_1.5.0 lubridate_1.9.5 forcats_1.0.1
[5] stringr_1.6.0 dplyr_1.2.1 purrr_1.2.1 readr_2.2.0
[9] tidyr_1.3.2 tibble_3.3.1 ggplot2_4.0.2 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] utf8_1.2.6 sandwich_3.1-1 generics_0.1.4 lattice_0.22-9 stringi_1.8.7
[6] hms_1.1.4 digest_0.6.39 magrittr_2.0.5 evaluate_1.0.5 grid_4.5.3
[11] timechange_0.4.0 RColorBrewer_1.1-3 mvtnorm_1.3-6 fastmap_1.2.0 Matrix_1.7-5
[16] jsonlite_2.0.0 survival_3.8-6 multcomp_1.4-30 scales_1.4.0 TH.data_1.1-5
[21] codetools_0.2-20 textshaping_1.0.5 cli_3.6.5 rlang_1.1.7 splines_4.5.3
[26] withr_3.0.2 yaml_2.3.12 otel_0.2.0 ggbeeswarm_0.7.3 tools_4.5.3
[31] tzdb_0.5.0 profmem_0.7.0 pacman_0.5.1 vctrs_0.7.2 R6_2.6.1
[36] png_0.1-9 zoo_1.8-15 lifecycle_1.0.5 htmlwidgets_1.6.4 vipor_0.4.7
[41] MASS_7.3-65 ragg_1.5.2 beeswarm_0.4.0 pkgconfig_2.0.3 pillar_1.11.1
[46] gtable_0.3.6 glue_1.8.0 systemfonts_1.3.2 xfun_0.57 tidyselect_1.2.1
[51] rstudioapi_0.18.0 knitr_1.51 farver_2.1.2 htmltools_0.5.9 patchwork_1.3.2
[56] rmarkdown_2.31 labeling_0.4.3 compiler_4.5.3 S7_0.2.1
Footnotes
f(x) <- valueみたいな書き方をする関数には、結構この記法(`f<-`(x, value))に対応しているものがあります。代入演算子(<-)まで含めて関数の名前になっているので、関数名を必ずバッククォート記号(`、backtickとも言うみたい)で囲む必要があります。コード補完をオンにしていると、候補に出てきます。↩︎ほとんどの条件で実行回数(n_iter)が上限の1万回に達しているからか、beeswarmの処理にやたら時間がかかる気がします。
"violin"とか"boxplot"だと処理が速いです。"jitter"も重かったです。"ridge"は割と速かったのですが、今回のデータだと"violin"の方が見やすかったです。↩︎