Skip to content

numr_replace() or numr_substitute()? #10

@joeroe

Description

@joeroe

Which to use as the default for formatting numerals? numr_replace() uses string replacement; numr_substitute() decomposes a numeric into digits and directly subsets the numerals matrix.

I was hoping numr_substitute() would be faster, but it isn't (yet):

library("numerals")

x <- floor(runif(1000) * 1e6)

bench::mark(
  numr_substitute(x, "fa")
)
#> Warning: Some expressions had a GC in every iteration; so filtering is disabled.
#> # A tibble: 1 x 6
#>   expression                    min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>               <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 numr_substitute(x, "fa")   18.9ms     21ms      45.2        NA     68.7

bench::mark(
  numr_replace(x, "fa")
)
#> # A tibble: 1 x 6
#>   expression                 min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>            <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 numr_replace(x, "fa")   8.51ms   8.54ms      112.        NA        0

But I do suspect numr_substitute() is more robust to e.g. character encoding and locale issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions