vignettes/progress-benchmark.Rmd
progress-benchmark.Rmd
We make sure that the timer is not TRUE
, by setting it
to ten hours.
library(cli)
# 10 hours
cli:::cli_tick_set(10 * 60 * 60 * 1000)
cli_tick_reset()
#> NULL
`__cli_update_due`
#> [1] FALSE
fun <- function() NULL
ben_st <- bench::mark(
`__cli_update_due`,
fun(),
.Call(ccli_tick_reset),
interactive(),
check = FALSE
)
ben_st
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 __cli_update_due 5.94ns 10ns 73214059. 0B 0
#> 2 fun() 92.08ns 101ns 5166993. 0B 517.
#> 3 .Call(ccli_tick_reset) 93.02ns 105ns 8374424. 0B 0
#> 4 interactive() 13.97ns 19ns 41034412. 0B 0
ben_st2 <- bench::mark(
if (`__cli_update_due`) foobar()
)
ben_st2
#> # A tibble: 1 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:t> <dbl> <bch:byt> <dbl>
#> 1 if (`__cli_update_due`) foobar() 36.9ns 44ns 19674948. 0B 0
cli_progress_along()
seq <- 1:100000
ta <- cli_progress_along(seq)
bench::mark(seq[[1]], ta[[1]])
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 seq[[1]] 95ns 103ns 7505948. 0B 0
#> 2 ta[[1]] 109ns 121ns 6044900. 0B 0
for
loop
This is the baseline:
f0 <- function(n = 1e5) {
x <- 0
seq <- 1:n
for (i in seq) {
x <- x + i %% 2
}
x
}
With progress bars:
fp <- function(n = 1e5) {
x <- 0
seq <- 1:n
for (i in cli_progress_along(seq)) {
x <- x + seq[[i]] %% 2
}
x
}
Overhead per iteration:
ben_taf <- bench::mark(f0(), fp())
ben_taf
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0() 9.66ms 11.1ms 86.0 21.6KB 402.
#> 2 fp() 10.88ms 12.9ms 73.2 82.3KB 305.
(ben_taf$median[2] - ben_taf$median[1]) / 1e5
#> [1] 17.5ns
ben_taf2 <- bench::mark(f0(1e6), fp(1e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_taf2
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+06) 121ms 160ms 6.05 0B 48.4
#> 2 fp(1e+06) 207ms 210ms 4.75 1.88KB 38.0
(ben_taf2$median[2] - ben_taf2$median[1]) / 1e6
#> [1] 50.4ns
ben_taf3 <- bench::mark(f0(1e7), fp(1e7))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_taf3
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+07) 1.2s 1.2s 0.834 0B 68.4
#> 2 fp(1e+07) 1.21s 1.21s 0.826 1.88KB 67.7
(ben_taf3$median[2] - ben_taf3$median[1]) / 1e7
#> [1] 1.15ns
ben_taf4 <- bench::mark(f0(1e8), fp(1e8))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_taf4
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+08) 11.6s 11.6s 0.0859 0B 45.5
#> 2 fp(1e+08) 13.2s 13.2s 0.0756 1.88KB 37.6
(ben_taf4$median[2] - ben_taf4$median[1]) / 1e8
#> [1] 15.9ns
lapply()
This is the baseline:
With an index vector:
f01 <- function(n = 1e5) {
seq <- 1:n
ret <- lapply(seq_along(seq), function(i) {
seq[[i]] %% 2
})
invisible(ret)
}
With progress bars:
fp <- function(n = 1e5) {
seq <- 1:n
ret <- lapply(cli_progress_along(seq), function(i) {
seq[[i]] %% 2
})
invisible(ret)
}
Overhead per iteration:
ben_tam <- bench::mark(f0(), f01(), fp())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_tam
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0() 69.2ms 85.5ms 9.34 781KB 18.7
#> 2 f01() 74ms 89.8ms 11.1 781KB 18.5
#> 3 fp() 94.4ms 125.2ms 8.26 783KB 9.91
(ben_tam$median[3] - ben_tam$median[1]) / 1e5
#> [1] 397ns
ben_tam2 <- bench::mark(f0(1e6), f01(1e6), fp(1e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_tam2
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+06) 832.69ms 832.69ms 1.20 7.63MB 8.41
#> 2 f01(1e+06) 1.83s 1.83s 0.547 7.63MB 2.74
#> 3 fp(1e+06) 968.48ms 968.48ms 1.03 7.63MB 5.16
(ben_tam2$median[3] - ben_tam2$median[1]) / 1e6
#> [1] 136ns
(ben_tam2$median[3] - ben_tam2$median[2]) / 1e6
#> [1] 1ns
This is the baseline:
f0 <- function(n = 1e5) {
seq <- 1:n
ret <- purrr::map(seq, function(x) {
x %% 2
})
invisible(ret)
}
With index vector:
f01 <- function(n = 1e5) {
seq <- 1:n
ret <- purrr::map(seq_along(seq), function(i) {
seq[[i]] %% 2
})
invisible(ret)
}
With progress bars:
fp <- function(n = 1e5) {
seq <- 1:n
ret <- purrr::map(cli_progress_along(seq), function(i) {
seq[[i]] %% 2
})
invisible(ret)
}
Overhead per iteration:
ben_pur <- bench::mark(f0(), f01(), fp())
ben_pur
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0() 51.2ms 52.1ms 18.9 1.39MB 9.45
#> 2 f01() 60.5ms 64.3ms 15.6 781.3KB 11.7
#> 3 fp() 64.3ms 66.2ms 15.1 783.24KB 15.1
(ben_pur$median[3] - ben_pur$median[1]) / 1e5
#> [1] 141ns
(ben_pur$median[3] - ben_pur$median[2]) / 1e5
#> [1] 19.1ns
ben_pur2 <- bench::mark(f0(1e6), f01(1e6), fp(1e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_pur2
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+06) 664.71ms 664.71ms 1.50 7.63MB 3.01
#> 2 f01(1e+06) 972.68ms 972.68ms 1.03 7.63MB 4.11
#> 3 fp(1e+06) 1.41s 1.41s 0.710 7.63MB 2.84
(ben_pur2$median[3] - ben_pur2$median[1]) / 1e6
#> [1] 745ns
(ben_pur2$median[3] - ben_pur2$median[2]) / 1e6
#> [1] 437ns
ticking()
f0 <- function(n = 1e5) {
i <- 0
x <- 0
while (i < n) {
x <- x + i %% 2
i <- i + 1
}
x
}
ben_tk <- bench::mark(f0(), fp())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_tk
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0() 8.57ms 8.91ms 104. 39.3KB 5.99
#> 2 fp() 3.43s 3.43s 0.292 99.9KB 3.21
(ben_tk$median[2] - ben_tk$median[1]) / 1e5
#> [1] 34.2µs
f0 <- function(n = 1e5) {
x <- 0
for (i in 1:n) {
x <- x + i %% 2
}
x
}
fp <- function(n = 1e5) {
cli_progress_bar(total = n)
x <- 0
for (i in 1:n) {
x <- x + i %% 2
cli_progress_update()
}
x
}
ff <- function(n = 1e5) {
cli_progress_bar(total = n)
x <- 0
for (i in 1:n) {
x <- x + i %% 2
if (`__cli_update_due`) cli_progress_update()
}
x
}
ben_api <- bench::mark(f0(), ff(), fp())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_api
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0() 8.97ms 9.56ms 85.2 18.7KB 9.91
#> 2 ff() 15.76ms 16.32ms 53.6 27.6KB 5.74
#> 3 fp() 1.88s 1.88s 0.532 25.1KB 2.66
(ben_api$median[3] - ben_api$median[1]) / 1e5
#> [1] 18.7µs
(ben_api$median[2] - ben_api$median[1]) / 1e5
#> [1] 67.6ns
ben_api2 <- bench::mark(f0(1e6), ff(1e6), fp(1e6))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
ben_api2
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 f0(1e+06) 110.4ms 112.6ms 8.32 0B 8.32
#> 2 ff(1e+06) 173.6ms 174.4ms 5.41 1.9KB 5.41
#> 3 fp(1e+06) 18.9s 18.9s 0.0528 1.9KB 3.01
(ben_api2$median[3] - ben_api2$median[1]) / 1e6
#> [1] 18.8µs
(ben_api2$median[2] - ben_api2$median[1]) / 1e6
#> [1] 61.8ns
Baseline function:
SEXP test_baseline() {
int i;
int res = 0;
for (i = 0; i < 2000000000; i++) {
res += i % 2;
}
return ScalarInteger(res);
}
Switch + modulo check:
SEXP test_modulo(SEXP progress) {
int i;
int res = 0;
int progress_ = LOGICAL(progress)[0];
for (i = 0; i < 2000000000; i++) {
if (i % 10000 == 0 && progress_) cli_progress_set(R_NilValue, i);
res += i % 2;
}
return ScalarInteger(res);
}
cli progress bar API:
SEXP test_cli() {
int i;
int res = 0;
SEXP bar = PROTECT(cli_progress_bar(2000000000, NULL));
for (i = 0; i < 2000000000; i++) {
if (CLI_SHOULD_TICK) cli_progress_set(bar, i);
res += i % 2;
}
cli_progress_done(bar);
UNPROTECT(1);
return ScalarInteger(res);
}
SEXP test_cli_unroll() {
int i = 0;
int res = 0;
SEXP bar = PROTECT(cli_progress_bar(2000000000, NULL));
int s, final, step = 2000000000 / 100000;
for (s = 0; s < 100000; s++) {
if (CLI_SHOULD_TICK) cli_progress_set(bar, i);
final = (s + 1) * step;
for (i = s * step; i < final; i++) {
res += i % 2;
}
}
cli_progress_done(bar);
UNPROTECT(1);
return ScalarInteger(res);
}
library(progresstest)
ben_c <- bench::mark(
test_baseline(),
test_modulo(),
test_cli(),
test_cli_unroll()
)
ben_c
#> # A tibble: 4 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 test_baseline() 854ms 854ms 1.17 2.08KB 0
#> 2 test_modulo() 1.21s 1.21s 0.828 2.23KB 0
#> 3 test_cli() 1.22s 1.22s 0.820 23.89KB 0
#> 4 test_cli_unroll() 844.88ms 844.88ms 1.18 3.58KB 0
(ben_c$median[3] - ben_c$median[1]) / 2000000000
#> [1] 1ns
We only update the display a fixed number of times per second. (Currently maximum five times per second.)
Let’s measure how long a single update takes.
cli_progress_bar(total = 100000)
bench::mark(cli_progress_update(force = TRUE), max_iterations = 10000)
#> ■ 0% | ETA: 4m
#> ■ 0% | ETA: 4h
#> ■ 0% | ETA: 3h
#> ■ 0% | ETA: 2h
#> ■ 0% | ETA: 2h
#> ■ 0% | ETA: 1h
#> ■ 0% | ETA: 1h
#> ■ 0% | ETA: 1h
#> ■ 0% | ETA: 1h
#> ■ 0% | ETA: 1h
#> ■ 0% | ETA: 48m
#> ■ 0% | ETA: 44m
#> ■ 0% | ETA: 42m
#> ■ 0% | ETA: 40m
#> ■ 0% | ETA: 38m
#> ■ 0% | ETA: 36m
#> ■ 0% | ETA: 34m
#> ■ 0% | ETA: 33m
#> ■ 0% | ETA: 32m
#> ■ 0% | ETA: 31m
#> ■ 0% | ETA: 30m
#> ■ 0% | ETA: 30m
#> ■ 0% | ETA: 29m
#> ■ 0% | ETA: 28m
#> ■ 0% | ETA: 28m
#> ■ 0% | ETA: 27m
#> ■ 0% | ETA: 26m
#> ■ 0% | ETA: 26m
#> ■ 0% | ETA: 25m
#> ■ 0% | ETA: 25m
#> ■ 0% | ETA: 24m
#> ■ 0% | ETA: 24m
#> ■ 0% | ETA: 23m
#> ■ 0% | ETA: 23m
#> ■ 0% | ETA: 22m
#> ■ 0% | ETA: 22m
#> ■ 0% | ETA: 22m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 21m
#> ■ 0% | ETA: 20m
#> ■ 0% | ETA: 20m
#> ■ 0% | ETA: 20m
#> ■ 0% | ETA: 20m
#> ■ 0% | ETA: 20m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 19m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 18m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 17m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> ■ 0% | ETA: 16m
#> # A tibble: 1 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:> <dbl> <bch:byt> <dbl>
#> 1 cli_progress_update(force = TRUE) 5.23ms 5.83ms 160. 1.35MB 2.05
cli_progress_done()
cli_progress_bar(total = NA)
bench::mark(cli_progress_update(force = TRUE), max_iterations = 10000)
#> ⠙ 1 done (347/s) | 4ms
#> ⠹ 2 done (56/s) | 36ms
#> ⠸ 3 done (70/s) | 43ms
#> ⠼ 4 done (81/s) | 50ms
#> ⠴ 5 done (89/s) | 57ms
#> ⠦ 6 done (95/s) | 63ms
#> ⠧ 7 done (101/s) | 70ms
#> ⠇ 8 done (104/s) | 78ms
#> ⠏ 9 done (103/s) | 88ms
#> ⠋ 10 done (107/s) | 94ms
#> ⠙ 11 done (110/s) | 101ms
#> ⠹ 12 done (112/s) | 107ms
#> ⠸ 13 done (115/s) | 114ms
#> ⠼ 14 done (117/s) | 120ms
#> ⠴ 15 done (118/s) | 128ms
#> ⠦ 16 done (120/s) | 134ms
#> ⠧ 17 done (122/s) | 141ms
#> ⠇ 18 done (123/s) | 147ms
#> ⠏ 19 done (124/s) | 154ms
#> ⠋ 20 done (125/s) | 160ms
#> ⠙ 21 done (126/s) | 167ms
#> ⠹ 22 done (127/s) | 173ms
#> ⠸ 23 done (128/s) | 180ms
#> ⠼ 24 done (125/s) | 192ms
#> ⠴ 25 done (126/s) | 200ms
#> ⠦ 26 done (126/s) | 207ms
#> ⠧ 27 done (126/s) | 214ms
#> ⠇ 28 done (127/s) | 222ms
#> ⠏ 29 done (127/s) | 229ms
#> ⠋ 30 done (127/s) | 237ms
#> ⠙ 31 done (127/s) | 245ms
#> ⠹ 32 done (127/s) | 252ms
#> ⠸ 33 done (127/s) | 260ms
#> ⠼ 34 done (128/s) | 267ms
#> ⠴ 35 done (128/s) | 274ms
#> ⠦ 36 done (129/s) | 281ms
#> ⠧ 37 done (129/s) | 287ms
#> ⠇ 38 done (130/s) | 294ms
#> ⠏ 39 done (130/s) | 300ms
#> ⠋ 40 done (131/s) | 307ms
#> ⠙ 41 done (131/s) | 313ms
#> ⠹ 42 done (132/s) | 320ms
#> ⠸ 43 done (132/s) | 326ms
#> ⠼ 44 done (132/s) | 333ms
#> ⠴ 45 done (133/s) | 340ms
#> ⠦ 46 done (133/s) | 346ms
#> ⠧ 47 done (134/s) | 352ms
#> ⠇ 48 done (134/s) | 359ms
#> ⠏ 49 done (134/s) | 365ms
#> ⠋ 50 done (135/s) | 372ms
#> ⠙ 51 done (135/s) | 378ms
#> ⠹ 52 done (135/s) | 385ms
#> ⠸ 53 done (136/s) | 391ms
#> ⠼ 54 done (136/s) | 398ms
#> ⠴ 55 done (136/s) | 404ms
#> ⠦ 56 done (136/s) | 411ms
#> ⠧ 57 done (137/s) | 418ms
#> ⠇ 58 done (137/s) | 424ms
#> ⠏ 59 done (137/s) | 431ms
#> ⠋ 60 done (137/s) | 438ms
#> ⠙ 61 done (138/s) | 444ms
#> ⠹ 62 done (138/s) | 451ms
#> ⠸ 63 done (138/s) | 457ms
#> ⠼ 64 done (138/s) | 464ms
#> ⠴ 65 done (138/s) | 470ms
#> ⠦ 66 done (139/s) | 477ms
#> ⠧ 67 done (139/s) | 483ms
#> ⠇ 68 done (136/s) | 502ms
#> ⠏ 69 done (135/s) | 511ms
#> ⠋ 70 done (135/s) | 517ms
#> ⠙ 71 done (136/s) | 524ms
#> ⠹ 72 done (136/s) | 531ms
#> # A tibble: 1 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:> <dbl> <bch:byt> <dbl>
#> 1 cli_progress_update(force = TRUE) 6.31ms 6.59ms 143. 278KB 2.05
cli_progress_done()