-
-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use as.numeric(seq_len(n))
in favor of seq(1, n)
#1427
Comments
I wasn't able to get to this today 😢 |
The above fails if n = 0. |
Yeah, my |
What is the point of a special function |
I also wonder: how bad is it if we omit the test |
Because the natural sequence is the default argument for many input functions, I suspect that this case is pretty frequent and worth optimizing for, but I haven't done any measurements. |
Your benchmark seems to show that |
🤦 🤦 You're right, of course. But the test isn't representative either: bench::mark(
sum(seq(1, 10000000, 1)),
sum(as.numeric(rlang::seq2(1, 10000000))),
sum(as.numeric(seq_len(10000000)))
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:t> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 sum(seq(1, 1e+07, 1)) 77.6ms 93.34ms 10.6 191MB 16.0
#> 2 sum(as.numeric(rlang::seq2(1, 1… 902ns 1.02µs 800320. 0B 0
#> 3 sum(as.numeric(seq_len(1e+07))) 164ns 246.04ns 2861548. 0B 0 Created on 2024-07-08 with reprex v2.1.0 The second and third example probably uses Gauss's formula. Let's try bench::mark(
var(seq(1, 100000, 1)),
var(as.numeric(rlang::seq2(1, 100000))),
var(as.numeric(seq_len(100000)))
)
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch> <bch:> <dbl> <bch:byt> <dbl>
#> 1 var(seq(1, 1e+05, 1)) 818µs 960µs 883. 1.92MB 32.0
#> 2 var(as.numeric(rlang::seq2(1, 1e+05… 434µs 496µs 2002. 781.3KB 26.9
#> 3 var(as.numeric(seq_len(1e+05))) 432µs 495µs 2016. 781.3KB 29.4 Created on 2024-07-08 with reprex v2.1.0 This is more realistic but also indicates that |
A thought: avoid seq() in any case. |
I tried |
seq(1, 0)
edge caseas.numeric(seq_len(n))
in favor of seq(1, n)
What happens, and what did you expect instead?
#1330 (comment)
We also need the
by
argument to ensure that the output is a numeric.Perhaps add a function
seq_numeric()
that has a special case for zero-length output?To reproduce
seq(by = 1)
seems to be the fastest, look at the units. Maybe it's worth arguing with R core to supportseq(start, start - by, by)
without error and to return an empty vector in this case?Created on 2024-07-05 with reprex v2.1.0
System information
No response
The text was updated successfully, but these errors were encountered: