🫶🏻 U1FAF6-project
  • Welcome to 🫶🏻 U1FAF6-project!
  • 一種半自動的最小可行寫作方案
  • 20250214 ch2
  • 20250214 ch3
  • Weekly Chellange
  • R Performance Benchmark Results
    • Performance Summary (Sorted by Speed)
    • Key Insights
      • 🏆 Top Performers
      • 💻 Intel Performance
      • 🐌 Slowest Systems
      • 🔍 Platform Observations
    • Apple Silicon vs Intel 效能比較
      • M2 vs Intel i5-12400 (Ubuntu)
    • M4 效能測試 (2025-09-01)
    • ⚠️ 重要提醒:R 安裝方式的影響
    • 歷史對比:Raspberry Pi 4B 效能 (2023-10-06)
    • 總結
    • 以下這是用 colab 的範例
  • 關於 COLAB 使用的小技巧
  • 教育測量學期刊觀察清單
  • 20260313 DINA pymc
  • 20260313 r gdina
  • 長格式 (lme4) 和寬格式 (lavaan) 的估計是相同的
🫶🏻 U1FAF6-project
  • R Performance Benchmark Results

R Performance Benchmark Results¶

  • JW
  • 2025-09-01

這是一個長期的比較結果。

Performance Summary (Sorted by Speed)¶

Rank Device Time (min) Time (sec) Speed vs M2 Mini CPU RAM OS R Version
1 Mac mini M4 0.563 33.8 0.75x M4 16 GB macOS Sequoia 15.6.1 R 4.5.1
2 MacBook Air M3 0.633 38.0 0.85x M3 8 GB macOS Ventura R 4.4.0
3 Mac mini M2 (baseline) 0.748 44.9 1.00x M2 16 GB macOS Ventura 13.3 R 4.3.1
4 Intel i5-12400 (Ubuntu) 1.068 64.1 1.43x i5-12400 64 GB Ubuntu 22.04.3 LTS R 4.3.1
5 Intel i5-12400 (Windows) 1.278 76.7 1.71x i5-12400 64 GB Windows 11 x64 R 4.3.1
6 Intel i7-10700 (Windows) 1.885 113.1 2.52x i7-10700 2.90GHz 16 GB Windows 10 x64 R 4.3.1
7 Intel i5-10500 (Windows) 2.230 133.8 2.98x i5-10500 3.10GHz 16 GB Windows 10 x64 R 4.3.1
8 Google Colab 2.868 172.1 3.83x x86_64 - Ubuntu 22.04.4 LTS R 4.5.1
9 MacBook Air (Intel) 5.563 333.8 7.44x Intel i5 1.6GHz 8 GB macOS Ventura 13.6 R 4.3.1

Key Insights¶

🏆 Top Performers¶

  • Mac mini M4: 25% faster than M2, best overall performance
  • Apple Silicon dominance: M4/M3/M2 占據前三名
  • M3 vs M2: 僅管 M3 只有 8GB RAM,仍比 M2 (16GB) 快 15%

💻 Intel Performance¶

  • Same CPU, different OS: i5-12400 在 Ubuntu 比 Windows 快 20%
  • RAM impact: 64GB RAM 的 i5-12400 仍比 Apple Silicon 慢 43%+
  • Older Intel: i7-10700/i5-10500 明顯落後於新 i5-12400

🐌 Slowest Systems¶

  • Old Intel MacBook Air: 比 M2 慢 7.4 倍
  • Google Colab: 免費但效能有限,比 M2 慢 3.8 倍

🔍 Platform Observations¶

  • macOS: Apple Silicon 展現絕對優勢
  • Linux vs Windows: 相同硬體下 Ubuntu 比 Windows 快
  • R Version: 不同 R 版本對效能影響較小

Apple Silicon vs Intel 效能比較¶

M2 vs Intel i5-12400 (Ubuntu)¶

M2 晶片的執行時間約為 12 代 i5 (Ubuntu) 的 2/3,具體來說 M2 大約需要 40 秒,而 Intel i5-12400 (Ubuntu) 則需要 60 秒,速率上可以提升約 30% 左右。

當使用 cmdstanr 套件時,Apple Silicon 的優勢更加明顯。Intel 平台需要 12 秒的工作,在 Apple Silicon 上僅需 4 秒,幾乎可以提升 60%,展現了同樣模式下的顯著效能差異。

M4 效能測試 (2025-09-01)¶

今天測試新的 Mac mini M4,結果令人驚艷。同樣的工作只用了 33.76 秒,這大約只花費當年 M2 的 3/4 的時間,比 M2 快了 25%。

⚠️ 重要提醒:R 安裝方式的影響¶

千萬注意,在蘋果系統上安裝 R 時,一定要從 CRAN 上去下載,千萬不要用 Homebrew。Homebrew 的版本可能會非常慢。例如,同樣 M4 的機器,使用 Homebrew 安裝的 R 需要 1.4 分鐘,但 CRAN 官方版本只需要 33.76 秒,效能差異高達 2.5 倍。

這個差異主要是因為 Homebrew 版本缺乏針對 Apple Silicon 的優化,而 CRAN 官方版本則充分利用了硬體的性能特性。因此 macOS 用戶務必從 CRAN 下載官方版本。

歷史對比:Raspberry Pi 4B 效能 (2023-10-06)¶

2023-10-06 用 Raspberry Pi 4B 和 MacBook Air 比較同一段程式(Total graph size: 4131),Pi 4B 大約需要 109.98 秒,而 MacBook Air 只需 28.13 秒,相差大約 3.9 倍。

進一步比較發現,Pi 4B 跟 M2 的速度大約相差 29.1 倍,和目前使用的 Ubuntu 系統相差約 20.4 倍。這意味著用 M2 跑 1 小時的工作量,用 Raspberry Pi 4B 要跑 30 小時才能完成。

總結¶

Apple Silicon 在 R 運算上展現壓倒性優勢,M4 更是持續提升效能,比 M2 再快 25%。安裝方式對效能影響極大,務必使用 CRAN 官方版本而非 Homebrew。cmdstanr 在 Apple Silicon 上有額外的效能加成,而 Raspberry Pi 雖然適合學習,但不適合大規模運算需求。

同樣跑下列這段程式,這是用 jags 測試,(runjags) 這個模式的規模是 Total graph size: 5109 74個樣本,

library(blavaan)
library(runjags)
library(lavaan)

model <- '
   # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + y2 + y3 + y4
     dem65 =~ y5 + y6 + y7 + y8
   # regressions
     dem60 ~ ind60
     dem65 ~ ind60 + dem60
   # residual covariances
     y1 ~~ y5
     y2 ~~ y4 + y6
     y3 ~~ y7
     y4 ~~ y8
     y6 ~~ y8
'

sys_time <- Sys.time()
fit <- bsem(model, data = PoliticalDemocracy, target = 'jags')
Sys.time() - sys_time

以下這是用 colab 的範例¶

In [5]:
Copied!
system("sudo apt update && sudo apt upgrade")
system("sudo apt install jags")
system("sudo apt install r-cran-runjags")
#system("sudo apt install r-cran-lavaan")
system("sudo apt install r-cran-blavaan")
system("sudo apt update && sudo apt upgrade") system("sudo apt install jags") system("sudo apt install r-cran-runjags") #system("sudo apt install r-cran-lavaan") system("sudo apt install r-cran-blavaan")
In [13]:
Copied!
install.packages("lavaan") ## need lavaan > 0.6-18
install.packages("lavaan") ## need lavaan > 0.6-18
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)

also installing the dependency ‘quadprog’


In [14]:
Copied!
installed.packages()["lavaan", "Version"]
installed.packages()["lavaan", "Version"]
'0.6-19'
In [15]:
Copied!
system("sudo apt install r-cran-blavaan")
system("sudo apt install r-cran-blavaan")
In [16]:
Copied!
library(blavaan)
library(runjags)
library(lavaan)
library(blavaan) library(runjags) library(lavaan)
This is blavaan 0.5-8

On multicore systems, we suggest use of future::plan("multicore") or
  future::plan("multisession") for faster post-MCMC computations.

This is lavaan 0.6-19
lavaan is FREE software! Please report any bugs.

In [17]:
Copied!
model <- '
   # latent variable definitions
     ind60 =~ x1 + x2 + x3
     dem60 =~ y1 + y2 + y3 + y4
     dem65 =~ y5 + y6 + y7 + y8
   # regressions
     dem60 ~ ind60
     dem65 ~ ind60 + dem60
   # residual covariances
     y1 ~~ y5
     y2 ~~ y4 + y6
     y3 ~~ y7
     y4 ~~ y8
     y6 ~~ y8
'

sys_time <- Sys.time()
fit <- bsem(model, data = PoliticalDemocracy, target = 'jags')
Sys.time() - sys_time
model <- ' # latent variable definitions ind60 =~ x1 + x2 + x3 dem60 =~ y1 + y2 + y3 + y4 dem65 =~ y5 + y6 + y7 + y8 # regressions dem60 ~ ind60 dem65 ~ ind60 + dem60 # residual covariances y1 ~~ y5 y2 ~~ y4 + y6 y3 ~~ y7 y4 ~~ y8 y6 ~~ y8 ' sys_time <- Sys.time() fit <- bsem(model, data = PoliticalDemocracy, target = 'jags') Sys.time() - sys_time
Calling the simulation...
Welcome to JAGS 4.3.2 on Mon Sep  1 03:04:43 2025
JAGS is free software and comes with ABSOLUTELY NO WARRANTY
Loading module: basemod: ok
Loading module: bugs: ok
. . Reading data file data.txt
. Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Graph information:
   Observed stochastic nodes: 825
   Unobserved stochastic nodes: 717
   Total graph size: 5109
. Reading parameter file inits1.txt
. Reading parameter file inits2.txt
. Reading parameter file inits3.txt
. Initializing model
. Adapting 1000
-------------------------------------------------| 1000
++++++++++++++++++++++++++++++++++++++++++++++++++ 100%
Adaptation successful
. Updating 4000
-------------------------------------------------| 4000
************************************************** 100%
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Updating 10000
-------------------------------------------------| 10000
************************************************** 100%
. . . . . . Updating 0
. Deleting model
. 
Simulation complete.  Reading coda files...
Coda files loaded successfully
Calculating summary statistics...
Note: The monitored variables 'lambda[1,1,1]', 'lambda[4,2,1]',
'lambda[8,3,1]', 'alpha[1,1,1]', 'alpha[2,1,1]', 'alpha[3,1,1]',
'psi[4,4,1]', 'psi[5,5,1]', 'psi[6,6,1]', 'psi[7,7,1]', 'psi[8,8,1]'
and 'psi[9,9,1]' appear to be non-stochastic; they will not be included
in the convergence diagnostic
Calculating the Gelman-Rubin statistic for 74 variables....
Finished running the simulation
Computing post-estimation metrics (including lvs if requested)...
Time difference of 2.867637 mins
In [18]:
Copied!
sessionInfo()
sessionInfo()
R version 4.5.1 (2025-06-13)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: Etc/UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] future_1.67.0   lavaan_0.6-19   runjags_2.2.2-5 blavaan_0.5-8  
[5] Rcpp_1.1.0     

loaded via a namespace (and not attached):
 [1] sandwich_3.1-1        generics_0.1.4        lattice_0.22-7       
 [4] listenv_0.9.1         digest_0.6.37         magrittr_2.0.3       
 [7] evaluate_1.0.5        grid_4.5.1            RColorBrewer_1.1-3   
[10] pbdZMQ_0.3-14         CompQuadForm_1.4.4    mvtnorm_1.3-3        
[13] fastmap_1.2.0         Matrix_1.7-4          jsonlite_2.0.0       
[16] pkgbuild_1.4.8        gridExtra_2.3         QuickJSR_1.8.0       
[19] scales_1.4.0          pbivnorm_0.6.0        codetools_0.2-20     
[22] textshaping_1.0.1     mnormt_2.1.1          cli_3.6.5            
[25] rlang_1.1.6           crayon_1.5.3          parallelly_1.45.1    
[28] future.apply_1.20.0   base64enc_0.1-3       repr_1.1.7           
[31] StanHeaders_2.32.10   parallel_4.5.1        tools_4.5.1          
[34] rstan_2.32.7          inline_0.3.21         uuid_1.2-1           
[37] rstantools_2.4.0      coda_0.19-4.1         dplyr_1.1.4          
[40] ggplot2_3.5.2         tmvnsim_1.0-2         globals_0.18.0       
[43] IRdisplay_1.1         vctrs_0.6.5           R6_2.6.1             
[46] matrixStats_1.5.0     stats4_4.5.1          zoo_1.8-14           
[49] lifecycle_1.0.4       ragg_1.4.0            pkgconfig_2.0.3      
[52] RcppParallel_5.1.11-1 pillar_1.11.0         gtable_0.3.6         
[55] loo_2.8.0             glue_1.8.0            systemfonts_1.2.3    
[58] tibble_3.3.0          tidyselect_1.2.1      IRkernel_1.3.2       
[61] bayesplot_1.13.0      farver_2.1.2          htmltools_0.5.8.1    
[64] compiler_4.5.1        quadprog_1.5-8        nonnest2_0.5-8       
In [ ]:
Copied!

Previous Next

Built with MkDocs using a theme provided by Read the Docs.