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.13e8        0B        0
#> 2 fun()                  120.02ns  140.2ns    4.87e6        0B        0
#> 3 .Call(ccli_tick_reset) 110.01ns  120.1ns    8.02e6        0B        0
#> 4 interactive()            8.96ns   10.1ns    6.69e7        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 21128997.        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]]      120ns    131ns  6809880.        0B       0 
#> 2 ta[[1]]       130ns    150ns  5909510.        0B     591.

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()           22ms   22.2ms      45.2    21.6KB     256.
#> 2 fp()         24.5ms   24.6ms      40.7    82.3KB     217.
(ben_taf$median[2] - ben_taf$median[1]) / 1e5
#> [1] 24ns
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)     240ms    240ms      4.15        0B     33.2
#> 2 fp(1e+06)     262ms    262ms      3.82    1.87KB     32.5
(ben_taf2$median[2] - ben_taf2$median[1]) / 1e6
#> [1] 22ns
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.55s    2.55s     0.392        0B     32.9
#> 2 fp(1e+07)     2.62s    2.62s     0.382    1.87KB     31.7
(ben_taf3$median[2] - ben_taf3$median[1]) / 1e7
#> [1] 6.74ns
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.0422        0B     20.6
#> 2 fp(1e+08)     25.3s    25.3s    0.0396    1.87KB     19.2
(ben_taf4$median[2] - ben_taf4$median[1]) / 1e8
#> [1] 16ns

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()         83.3ms   85.8ms     11.2      781KB     14.9
#> 2 f01()       116.6ms    123ms      7.69     781KB     11.5
#> 3 fp()        130.8ms  134.5ms      7.41     783KB     13.0
(ben_tam$median[3] - ben_tam$median[1]) / 1e5
#> [1] 487ns
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)  935.05ms 935.05ms     1.07     7.63MB     4.28
#> 2 f01(1e+06)     1.1s     1.1s     0.907    7.63MB     4.54
#> 3 fp(1e+06)     1.97s    1.97s     0.508    7.63MB     2.54
(ben_tam2$median[3] - ben_tam2$median[1]) / 1e6
#> [1] 1.03µs
(ben_tam2$median[3] - ben_tam2$median[2]) / 1e6
#> [1] 865ns

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()         75.4ms   75.8ms      13.2    1.41MB     9.90
#> 2 f01()        92.2ms   92.4ms      10.7   781.3KB     5.36
#> 3 fp()         94.5ms   97.6ms      10.2  783.23KB     6.81
(ben_pur$median[3] - ben_pur$median[1]) / 1e5
#> [1] 218ns
(ben_pur$median[3] - ben_pur$median[2]) / 1e5
#> [1] 51.5ns
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)  877.16ms 877.16ms     1.14     7.63MB     3.42
#> 2 f01(1e+06)    1.13s    1.13s     0.888    7.63MB     2.67
#> 3 fp(1e+06)     1.42s    1.42s     0.703    7.63MB     2.11
(ben_pur2$median[3] - ben_pur2$median[1]) / 1e6
#> [1] 545ns
(ben_pur2$median[3] - ben_pur2$median[2]) / 1e6
#> [1] 297ns

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()        28.55ms  32.62ms    30.7      39.3KB     1.92
#> 2 fp()          4.46s    4.46s     0.224   100.4KB     2.69
(ben_tk$median[2] - ben_tk$median[1]) / 1e5
#> [1] 44.3µ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()           23ms   43.5ms    24.6      18.7KB     3.78
#> 2 ff()        33.44ms  52.07ms    20.1      27.6KB     1.82
#> 3 fp()          2.42s    2.42s     0.413    25.1KB     2.48
(ben_api$median[3] - ben_api$median[1]) / 1e5
#> [1] 23.8µs
(ben_api$median[2] - ben_api$median[1]) / 1e5
#> [1] 85.7ns
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)   228.2ms  228.3ms    4.35          0B     4.35
#> 2 ff(1e+06)   332.2ms  332.3ms    3.01      1.88KB     3.01
#> 3 fp(1e+06)     21.9s    21.9s    0.0456    1.88KB     2.42
(ben_api2$median[3] - ben_api2$median[1]) / 1e6
#> [1] 21.7µs
(ben_api2$median[2] - ben_api2$median[1]) / 1e6
#> [1] 104ns

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()   622.01ms 622.01ms     1.61     2.08KB        0
#> 2 test_modulo()        1.25s    1.25s     0.803    2.24KB        0
#> 3 test_cli()           1.24s    1.24s     0.804    23.9KB        0
#> 4 test_cli_unroll() 623.33ms 623.33ms     1.60     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: 44m
#>    0% | ETA: 40m
#>    0% | ETA: 36m
#>    0% | ETA: 33m
#>    0% | ETA: 31m
#>    0% | ETA: 29m
#>    0% | ETA: 27m
#>    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: 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: 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: 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: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    0% | ETA: 14m
#>    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… 6.33ms 6.43ms      153.     1.4MB     2.04
cli_progress_done()

Iterator without a bar

cli_progress_bar(total = NA)
bench::mark(cli_progress_update(force = TRUE), max_iterations = 10000)
#> ⠙ 1 done (495/s) | 3ms
#> ⠹ 2 done (70/s) | 29ms
#> ⠸ 3 done (82/s) | 37ms
#> ⠼ 4 done (91/s) | 45ms
#> ⠴ 5 done (97/s) | 52ms
#> ⠦ 6 done (102/s) | 59ms
#> ⠧ 7 done (106/s) | 67ms
#> ⠇ 8 done (109/s) | 74ms
#> ⠏ 9 done (111/s) | 82ms
#> ⠋ 10 done (113/s) | 89ms
#> ⠙ 11 done (115/s) | 96ms
#> ⠹ 12 done (117/s) | 104ms
#> ⠸ 13 done (118/s) | 111ms
#> ⠼ 14 done (119/s) | 118ms
#> ⠴ 15 done (120/s) | 126ms
#> ⠦ 16 done (121/s) | 133ms
#> ⠧ 17 done (121/s) | 141ms
#> ⠇ 18 done (121/s) | 149ms
#> ⠏ 19 done (122/s) | 157ms
#> ⠋ 20 done (122/s) | 165ms
#> ⠙ 21 done (122/s) | 172ms
#> ⠹ 22 done (123/s) | 180ms
#> ⠸ 23 done (123/s) | 187ms
#> ⠼ 24 done (124/s) | 195ms
#> ⠴ 25 done (124/s) | 202ms
#> ⠦ 26 done (124/s) | 210ms
#> ⠧ 27 done (125/s) | 217ms
#> ⠇ 28 done (125/s) | 225ms
#> ⠏ 29 done (125/s) | 232ms
#> ⠋ 30 done (125/s) | 240ms
#> ⠙ 31 done (125/s) | 248ms
#> ⠹ 32 done (126/s) | 255ms
#> ⠸ 33 done (126/s) | 263ms
#> ⠼ 34 done (126/s) | 270ms
#> ⠴ 35 done (126/s) | 278ms
#> ⠦ 36 done (126/s) | 285ms
#> ⠧ 37 done (127/s) | 293ms
#> ⠇ 38 done (127/s) | 300ms
#> ⠏ 39 done (127/s) | 308ms
#> ⠋ 40 done (127/s) | 315ms
#> ⠙ 41 done (127/s) | 323ms
#> ⠹ 42 done (127/s) | 330ms
#> ⠸ 43 done (128/s) | 338ms
#> ⠼ 44 done (128/s) | 345ms
#> ⠴ 45 done (128/s) | 353ms
#> ⠦ 46 done (128/s) | 361ms
#> ⠧ 47 done (128/s) | 368ms
#> ⠇ 48 done (128/s) | 376ms
#> ⠏ 49 done (128/s) | 384ms
#> ⠋ 50 done (127/s) | 396ms
#> ⠙ 51 done (127/s) | 403ms
#> ⠹ 52 done (127/s) | 411ms
#> ⠸ 53 done (127/s) | 418ms
#> ⠼ 54 done (127/s) | 425ms
#> ⠴ 55 done (127/s) | 433ms
#> ⠦ 56 done (127/s) | 440ms
#> ⠧ 57 done (128/s) | 448ms
#> ⠇ 58 done (128/s) | 455ms
#> ⠏ 59 done (128/s) | 462ms
#> ⠋ 60 done (128/s) | 470ms
#> ⠙ 61 done (128/s) | 477ms
#> ⠹ 62 done (128/s) | 485ms
#> ⠸ 63 done (128/s) | 492ms
#> ⠼ 64 done (128/s) | 499ms
#> ⠴ 65 done (128/s) | 507ms
#> ⠦ 66 done (129/s) | 514ms
#> ⠧ 67 done (129/s) | 521ms
#> ⠇ 68 done (129/s) | 529ms
#> # 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.28ms 7.47ms      134.     265KB     2.02
cli_progress_done()