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 91418515.        0B       0 
#> 2 fun()                  120.02ns    141ns  4701238.        0B     470.
#> 3 .Call(ccli_tick_reset) 110.01ns  120.1ns  7968842.        0B       0 
#> 4 interactive()            8.96ns   11.1ns 65233106.        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.2ns 18120799.        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    130ns  7006982.        0B        0
#> 2 ta[[1]]       130ns    150ns  6324205.        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()         12.3ms   12.5ms      80.2    21.6KB     374.
#> 2 fp()         14.1ms   14.2ms      66.3    82.3KB     276.
(ben_taf$median[2] - ben_taf$median[1]) / 1e5
#> [1] 16.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)     137ms    144ms      6.32        0B     52.2
#> 2 fp(1e+06)     150ms    152ms      6.55    1.87KB     52.4
(ben_taf2$median[2] - ben_taf2$median[1]) / 1e6
#> [1] 7.56ns
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.5s     1.5s     0.665        0B     55.2
#> 2 fp(1e+07)     1.56s    1.56s     0.641    1.87KB     51.9
(ben_taf3$median[2] - ben_taf3$median[1]) / 1e7
#> [1] 5.67ns
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)     13.9s    13.9s    0.0721        0B     38.1
#> 2 fp(1e+08)     14.9s    14.9s    0.0672    1.87KB     33.4
(ben_taf4$median[2] - ben_taf4$median[1]) / 1e8
#> [1] 10ns

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()         68.5ms   82.9ms      9.75     781KB    17.6 
#> 2 f01()        96.4ms  106.2ms      9.36     781KB    16.8 
#> 3 fp()        110.6ms  124.6ms      7.69     783KB     9.61
(ben_tam$median[3] - ben_tam$median[1]) / 1e5
#> [1] 417ns
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)  912.27ms 912.27ms     1.10     7.63MB     7.67
#> 2 f01(1e+06)    1.73s    1.73s     0.578    7.63MB     4.62
#> 3 fp(1e+06)      1.9s     1.9s     0.527    7.63MB     4.22
(ben_tam2$median[3] - ben_tam2$median[1]) / 1e6
#> [1] 984ns
(ben_tam2$median[3] - ben_tam2$median[2]) / 1e6
#> [1] 166ns

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())
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
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()         66.2ms   69.5ms     13.9     1.39MB     7.96
#> 2 f01()        94.6ms  106.5ms      8.33   781.3KB     8.33
#> 3 fp()        120.6ms  129.3ms      7.73  783.23KB     5.80
(ben_pur$median[3] - ben_pur$median[1]) / 1e5
#> [1] 598ns
(ben_pur$median[3] - ben_pur$median[2]) / 1e5
#> [1] 228ns
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)  850.87ms 850.87ms     1.18     7.63MB     4.70
#> 2 f01(1e+06)    1.24s    1.24s     0.806    7.63MB     3.23
#> 3 fp(1e+06)     2.84s    2.84s     0.352    7.63MB     1.76
(ben_pur2$median[3] - ben_pur2$median[1]) / 1e6
#> [1] 1.99µs
(ben_pur2$median[3] - ben_pur2$median[2]) / 1e6
#> [1] 1.6µs

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()        13.62ms  13.76ms    68.0      39.3KB     1.94
#> 2 fp()          4.03s    4.03s     0.248   100.4KB     1.74
(ben_tk$median[2] - ben_tk$median[1]) / 1e5
#> [1] 40.1µ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()        12.29ms  13.43ms    57.1      18.7KB     3.94
#> 2 ff()        21.87ms  22.14ms    41.2      27.6KB     1.96
#> 3 fp()          2.25s    2.25s     0.445    25.1KB     1.78
(ben_api$median[3] - ben_api$median[1]) / 1e5
#> [1] 22.3µs
(ben_api$median[2] - ben_api$median[1]) / 1e5
#> [1] 87.1ns
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)   142.8ms    152ms    6.59          0B     3.30
#> 2 ff(1e+06)   230.3ms  238.7ms    4.16      1.88KB     2.78
#> 3 fp(1e+06)     23.1s    23.1s    0.0433    1.88KB     1.47
(ben_api2$median[3] - ben_api2$median[1]) / 1e6
#> [1] 22.9µs
(ben_api2$median[2] - ben_api2$median[1]) / 1e6
#> [1] 86.7ns

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.02ms 622.02ms     1.61     2.08KB        0
#> 2 test_modulo()        1.25s    1.25s     0.800    2.24KB        0
#> 3 test_cli()           1.25s    1.25s     0.802   23.88KB        0
#> 4 test_cli_unroll() 623.57ms 623.57ms     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: 39m
#>    0% | ETA: 35m
#>    0% | ETA: 32m
#>    0% | ETA: 30m
#>    0% | ETA: 28m
#>    0% | ETA: 27m
#>    0% | ETA: 26m
#>    0% | ETA: 24m
#>    0% | ETA: 24m
#>    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: 18m
#>    0% | ETA: 18m
#>    0% | ETA: 18m
#>    0% | ETA: 18m
#>    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: 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: 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
#>    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.93ms 6.04ms      163.     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 (489/s) | 3ms
#> ⠹ 2 done (70/s) | 29ms
#> ⠸ 3 done (85/s) | 36ms
#> ⠼ 4 done (94/s) | 43ms
#> ⠴ 5 done (101/s) | 50ms
#> ⠦ 6 done (106/s) | 57ms
#> ⠧ 7 done (110/s) | 64ms
#> ⠇ 8 done (113/s) | 71ms
#> ⠏ 9 done (116/s) | 78ms
#> ⠋ 10 done (118/s) | 85ms
#> ⠙ 11 done (120/s) | 92ms
#> ⠹ 12 done (122/s) | 99ms
#> ⠸ 13 done (123/s) | 106ms
#> ⠼ 14 done (124/s) | 113ms
#> ⠴ 15 done (125/s) | 120ms
#> ⠦ 16 done (126/s) | 127ms
#> ⠧ 17 done (126/s) | 135ms
#> ⠇ 18 done (127/s) | 143ms
#> ⠏ 19 done (127/s) | 150ms
#> ⠋ 20 done (128/s) | 157ms
#> ⠙ 21 done (129/s) | 164ms
#> ⠹ 22 done (129/s) | 171ms
#> ⠸ 23 done (130/s) | 178ms
#> ⠼ 24 done (130/s) | 185ms
#> ⠴ 25 done (131/s) | 192ms
#> ⠦ 26 done (131/s) | 199ms
#> ⠧ 27 done (132/s) | 206ms
#> ⠇ 28 done (132/s) | 213ms
#> ⠏ 29 done (132/s) | 220ms
#> ⠋ 30 done (133/s) | 227ms
#> ⠙ 31 done (133/s) | 234ms
#> ⠹ 32 done (133/s) | 241ms
#> ⠸ 33 done (134/s) | 248ms
#> ⠼ 34 done (134/s) | 255ms
#> ⠴ 35 done (134/s) | 262ms
#> ⠦ 36 done (134/s) | 269ms
#> ⠧ 37 done (134/s) | 276ms
#> ⠇ 38 done (135/s) | 283ms
#> ⠏ 39 done (135/s) | 290ms
#> ⠋ 40 done (135/s) | 297ms
#> ⠙ 41 done (135/s) | 304ms
#> ⠹ 42 done (135/s) | 311ms
#> ⠸ 43 done (136/s) | 318ms
#> ⠼ 44 done (136/s) | 325ms
#> ⠴ 45 done (136/s) | 332ms
#> ⠦ 46 done (136/s) | 339ms
#> ⠧ 47 done (136/s) | 346ms
#> ⠇ 48 done (136/s) | 353ms
#> ⠏ 49 done (136/s) | 360ms
#> ⠋ 50 done (136/s) | 367ms
#> ⠙ 51 done (137/s) | 374ms
#> ⠹ 52 done (137/s) | 381ms
#> ⠸ 53 done (137/s) | 388ms
#> ⠼ 54 done (137/s) | 395ms
#> ⠴ 55 done (137/s) | 402ms
#> ⠦ 56 done (137/s) | 409ms
#> ⠧ 57 done (137/s) | 416ms
#> ⠇ 58 done (137/s) | 423ms
#> ⠏ 59 done (137/s) | 430ms
#> ⠋ 60 done (137/s) | 437ms
#> ⠙ 61 done (138/s) | 444ms
#> ⠹ 62 done (138/s) | 451ms
#> ⠸ 63 done (138/s) | 458ms
#> ⠼ 64 done (138/s) | 465ms
#> ⠴ 65 done (138/s) | 472ms
#> ⠦ 66 done (138/s) | 479ms
#> ⠧ 67 done (138/s) | 486ms
#> ⠇ 68 done (138/s) | 493ms
#> ⠏ 69 done (138/s) | 500ms
#> ⠋ 70 done (138/s) | 507ms
#> ⠙ 71 done (138/s) | 514ms
#> ⠹ 72 done (138/s) | 521ms
#> ⠸ 73 done (138/s) | 528ms
#> # 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.93ms 6.99ms      142.     265KB        0
cli_progress_done()