Comments (10)
A thought: avoid seq() in any case.
from rigraph.
I tried make_ring(1E5)
. Optimization saves ca. 50%, 3.72ms v.s. 6.42ms.
from rigraph.
I wasn't able to get to this today 😢
from rigraph.
seq(1, vcount(graph))
does not work for empty vectors.
Alternatives are: seq_along, seq_len.
library(bench)
n <- 1
graph <- make_ring(n)
bench:: mark(
"seq" = seq(1, vcount(graph)),
"along" = seq_along(V(graph)),
"len" = seq_len(vcount(graph))
)
The above fails if n = 0.
vcount()
is faster then V(g)
.
from rigraph.
Yeah, my seq_numeric()
would special-case for the n == 0
edge case.
from rigraph.
What is the point of a special function seq_numeric()
?
Isn't it simpler to replace seq(1, x)
with seq_len(x)
?
from rigraph.
I also wonder: how bad is it if we omit the test index_is_natural_sequence()
? How likely is it that the test does indeed pass?
from rigraph.
seq(1, x)
is much faster, as the benchmark shows.
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.
from rigraph.
Your benchmark seems to show that seq_len
(246 ns) is much faster than seq
(791 µs), exactly the opposite of what you say above @krlmlr. What am I missing?
from rigraph.
🤦 🤦
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 var()
:
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 as.numeric(seq_len())
is faster. I stand corrected.
from rigraph.
Related Issues (20)
- revdeps HOT 13
- V(g) not the same as 1:vcount HOT 5
- `cluster_label_prop()` crashes R when using fixed labels HOT 6
- Export `.from` etc. with behavior similar to `dplyr::across()` HOT 1
- use lifecyle for layout.fruchterman.reingold()
- Remove/deprecate the functions using constructor_spec()? HOT 1
- deprecate neimode argument in bfs() for good
- `feedback_arc_set()` needs tests
- Add `igraph_feedback_vertex_set()`
- A weights vector in `local_scan`
- TODOs in `rinterface_extra.c`
- TODOs in tests HOT 17
- TODO in `src/igraph_vector.hpp` HOT 2
- TODOs in `socnet.R` HOT 7
- TODO in tkplot
- ensure max.print is used consistently in all of print.R
- Update `is_separator()` docs
- can indexing be made faster HOT 1
- can we remove the "## TODO: speed this up" in make.R?
- Add `igraph_find_cycle()`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rigraph.