Skip to content

Introduction

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

R benchmarks

The timer

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              0     10ns    1.30e8        0B        0
#> 2 fun()                  130.04ns  160.1ns    4.42e6        0B        0
#> 3 .Call(ccli_tick_reset) 110.01ns  121.1ns    7.71e6        0B        0
#> 4 interactive()            8.96ns   10.1ns    7.28e7        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> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 if (`__cli_update_due`) fo…  40ns 50.1ns 18881463.        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]]      110ns    130ns  7212659.        0B        0
#> 2 ta[[1]]       130ns    150ns  6087297.        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()         22.4ms   22.5ms      44.6    21.6KB     238.
#> 2 fp()         24.9ms   25.1ms      39.8    82.3KB     199.
(ben_taf$median[2] - ben_taf$median[1]) / 1e5
#> [1] 26.9ns
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)     251ms    252ms      3.97        0B     31.8
#> 2 fp(1e+06)     273ms    275ms      3.64    1.87KB     30.9
(ben_taf2$median[2] - ben_taf2$median[1]) / 1e6
#> [1] 23.2ns
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)     2.54s    2.54s     0.394        0B     33.1
#> 2 fp(1e+07)     2.72s    2.72s     0.368    1.87KB     30.6
(ben_taf3$median[2] - ben_taf3$median[1]) / 1e7
#> [1] 17.7ns
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)     23.7s    23.7s    0.0421        0B     20.6
#> 2 fp(1e+08)     25.9s    25.9s    0.0387    1.87KB     18.8
(ben_taf4$median[2] - ben_taf4$median[1]) / 1e8
#> [1] 21.2ns

Mapping with lapply()

This is the baseline:

f0 <- function(n = 1e5) {
  seq <- 1:n
  ret <- lapply(seq, function(x) {
    x %% 2
  })
  invisible(ret)
}

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()         91.1ms   91.8ms     10.2      781KB     10.2
#> 2 f01()       126.6ms  131.4ms      7.42     781KB     13.0
#> 3 fp()        120.8ms  132.9ms      6.73     783KB     10.1
(ben_tam$median[3] - ben_tam$median[1]) / 1e5
#> [1] 411ns
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)  958.49ms 958.49ms     1.04     7.63MB     7.30
#> 2 f01(1e+06)    1.92s    1.92s     0.522    7.63MB     2.09
#> 3 fp(1e+06)     1.19s    1.19s     0.842    7.63MB     4.21
(ben_tam2$median[3] - ben_tam2$median[1]) / 1e6
#> [1] 230ns
(ben_tam2$median[3] - ben_tam2$median[2]) / 1e6
#> [1] 1ns

Mapping with purrr

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()         76.6ms   77.1ms      12.8     1.4MB     5.12
#> 2 f01()          91ms   91.6ms      10.7   781.3KB     5.37
#> 3 fp()         93.2ms   96.5ms      10.4   783.2KB     2.59
(ben_pur$median[3] - ben_pur$median[1]) / 1e5
#> [1] 194ns
(ben_pur$median[3] - ben_pur$median[2]) / 1e5
#> [1] 49.7ns
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)  969.93ms 969.93ms     1.03     7.63MB     3.09
#> 2 f01(1e+06)    1.41s    1.41s     0.710    7.63MB     2.13
#> 3 fp(1e+06)     1.37s    1.37s     0.730    7.63MB     2.92
(ben_pur2$median[3] - ben_pur2$median[1]) / 1e6
#> [1] 401ns
(ben_pur2$median[3] - ben_pur2$median[2]) / 1e6
#> [1] 1ns

ticking()

f0 <- function(n = 1e5) {
  i <- 0
  x <- 0 
  while (i < n) {
    x <- x + i %% 2
    i <- i + 1
  }
  x
}
fp <- function(n = 1e5) {
  i <- 0
  x <- 0 
  while (ticking(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()        22.67ms  22.81ms    40.0      39.3KB     4.00
#> 2 fp()          4.21s    4.21s     0.238   100.4KB     2.61
(ben_tk$median[2] - ben_tk$median[1]) / 1e5
#> [1] 41.9µs

Traditional API

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()         21.5ms  21.66ms    41.4      18.7KB     3.94
#> 2 ff()        31.32ms  31.79ms     6.74     27.6KB     1.23
#> 3 fp()          2.27s    2.27s     0.440    25.1KB     2.64
(ben_api$median[3] - ben_api$median[1]) / 1e5
#> [1] 22.5µs
(ben_api$median[2] - ben_api$median[1]) / 1e5
#> [1] 101ns
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)   222.4ms  242.7ms    4.22          0B     4.22
#> 2 ff(1e+06)   347.8ms  350.1ms    2.86      1.88KB     4.28
#> 3 fp(1e+06)     24.2s    24.2s    0.0413    1.88KB     2.48
(ben_api2$median[3] - ben_api2$median[1]) / 1e6
#> [1] 24µs
(ben_api2$median[2] - ben_api2$median[1]) / 1e6
#> [1] 107ns

C benchmarks

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()   626.52ms 626.52ms     1.60     2.08KB        0
#> 2 test_modulo()        1.25s    1.25s     0.802    2.24KB        0
#> 3 test_cli()           1.25s    1.25s     0.803   23.88KB        0
#> 4 test_cli_unroll() 641.39ms 641.39ms     1.56     3.56KB        0
(ben_c$median[3] - ben_c$median[1]) / 2000000000
#> [1] 1ns

Display update

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.

Iterator with a bar

cli_progress_bar(total = 100000)
bench::mark(cli_progress_update(force = TRUE), max_iterations = 10000)
#>    0% | ETA:  4m
#>    0% | ETA:  2h
#>    0% | ETA:  1h
#>    0% | ETA:  1h
#>    0% | ETA:  1h
#>    0% | ETA: 45m
#>    0% | ETA: 40m
#>    0% | ETA: 36m
#>    0% | ETA: 34m
#>    0% | ETA: 31m
#>    0% | ETA: 29m
#>    0% | ETA: 28m
#>    0% | ETA: 26m
#>    0% | ETA: 25m
#>    0% | ETA: 24m
#>    0% | ETA: 23m
#>    0% | ETA: 23m
#>    0% | ETA: 22m
#>    0% | ETA: 21m
#>    0% | ETA: 21m
#>    0% | ETA: 20m
#>    0% | ETA: 20m
#>    0% | ETA: 19m
#>    0% | ETA: 19m
#>    0% | ETA: 19m
#>    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: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 16m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 15m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#>    0% | ETA: 13m
#> # A tibble: 1 × 6
#>   expression                    min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                 <bch:> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 cli_progress_update(force… 5.99ms 6.15ms      162.     1.4MB     4.20
cli_progress_done()

Iterator without a bar

cli_progress_bar(total = NA)
bench::mark(cli_progress_update(force = TRUE), max_iterations = 10000)
#> ⠙ 1 done (506/s) | 3ms
#> ⠹ 2 done (70/s) | 29ms
#> ⠸ 3 done (83/s) | 37ms
#> ⠼ 4 done (92/s) | 44ms
#> ⠴ 5 done (98/s) | 52ms
#> ⠦ 6 done (103/s) | 59ms
#> ⠧ 7 done (107/s) | 66ms
#> ⠇ 8 done (110/s) | 74ms
#> ⠏ 9 done (112/s) | 81ms
#> ⠋ 10 done (114/s) | 88ms
#> ⠙ 11 done (116/s) | 95ms
#> ⠹ 12 done (118/s) | 103ms
#> ⠸ 13 done (119/s) | 110ms
#> ⠼ 14 done (120/s) | 117ms
#> ⠴ 15 done (121/s) | 125ms
#> ⠦ 16 done (122/s) | 132ms
#> ⠧ 17 done (123/s) | 139ms
#> ⠇ 18 done (123/s) | 146ms
#> ⠏ 19 done (124/s) | 154ms
#> ⠋ 20 done (125/s) | 161ms
#> ⠙ 21 done (125/s) | 168ms
#> ⠹ 22 done (126/s) | 176ms
#> ⠸ 23 done (126/s) | 183ms
#> ⠼ 24 done (127/s) | 190ms
#> ⠴ 25 done (127/s) | 197ms
#> ⠦ 26 done (128/s) | 204ms
#> ⠧ 27 done (128/s) | 212ms
#> ⠇ 28 done (128/s) | 219ms
#> ⠏ 29 done (129/s) | 226ms
#> ⠋ 30 done (129/s) | 233ms
#> ⠙ 31 done (129/s) | 241ms
#> ⠹ 32 done (129/s) | 248ms
#> ⠸ 33 done (130/s) | 255ms
#> ⠼ 34 done (130/s) | 262ms
#> ⠴ 35 done (130/s) | 269ms
#> ⠦ 36 done (130/s) | 277ms
#> ⠧ 37 done (131/s) | 284ms
#> ⠇ 38 done (131/s) | 291ms
#> ⠏ 39 done (131/s) | 298ms
#> ⠋ 40 done (131/s) | 305ms
#> ⠙ 41 done (131/s) | 312ms
#> ⠹ 42 done (132/s) | 320ms
#> ⠸ 43 done (132/s) | 327ms
#> ⠼ 44 done (132/s) | 335ms
#> ⠴ 45 done (132/s) | 342ms
#> ⠦ 46 done (132/s) | 350ms
#> ⠧ 47 done (130/s) | 361ms
#> ⠇ 48 done (131/s) | 368ms
#> ⠏ 49 done (131/s) | 375ms
#> ⠋ 50 done (131/s) | 383ms
#> ⠙ 51 done (131/s) | 390ms
#> ⠹ 52 done (131/s) | 397ms
#> ⠸ 53 done (131/s) | 404ms
#> ⠼ 54 done (131/s) | 411ms
#> ⠴ 55 done (132/s) | 419ms
#> ⠦ 56 done (132/s) | 426ms
#> ⠧ 57 done (132/s) | 433ms
#> ⠇ 58 done (132/s) | 440ms
#> ⠏ 59 done (132/s) | 447ms
#> ⠋ 60 done (132/s) | 455ms
#> ⠙ 61 done (132/s) | 462ms
#> ⠹ 62 done (132/s) | 469ms
#> ⠸ 63 done (132/s) | 476ms
#> ⠼ 64 done (132/s) | 484ms
#> ⠴ 65 done (133/s) | 491ms
#> ⠦ 66 done (133/s) | 498ms
#> ⠧ 67 done (133/s) | 505ms
#> ⠇ 68 done (133/s) | 512ms
#> ⠏ 69 done (133/s) | 520ms
#> ⠋ 70 done (133/s) | 527ms
#> # A tibble: 1 × 6
#>   expression                    min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                 <bch:> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 cli_progress_update(force… 7.02ms 7.22ms      138.     265KB     2.03
cli_progress_done()