Skip to content
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

very minor speedup in edge_labels #21345

Closed
fchapoton opened this issue Aug 26, 2016 · 14 comments
Closed

very minor speedup in edge_labels #21345

fchapoton opened this issue Aug 26, 2016 · 14 comments

Comments

@fchapoton
Copy link
Contributor

by creating the list directly

Component: graph theory

Author: Frédéric Chapoton

Branch/Commit: c62e295

Reviewer: David Coudert

Issue created by migration from https://trac.sagemath.org/ticket/21345

@fchapoton fchapoton added this to the sage-7.4 milestone Aug 26, 2016
@fchapoton
Copy link
Contributor Author

Commit: 16c8698

@fchapoton
Copy link
Contributor Author

New commits:

16c8698minor speedup in edge_labels

@fchapoton
Copy link
Contributor Author

Branch: u/chapoton/21345

@dcoudert
Copy link
Contributor

comment:2

We can easily do better.

Previous code:

sage: G = graphs.PetersenGraph()
sage: %timeit G.edge_labels()
The slowest run took 5.12 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.9 µs per loop

sage: G = graphs.Grid2dGraph(50,50)
sage: %timeit G.edge_labels()
100 loops, best of 3: 6.98 ms per loop

Your code (almost the same):

sage: G = graphs.PetersenGraph()
sage: %timeit G.edge_labels()
The slowest run took 4.17 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.8 µs per loop

sage: G = graphs.Grid2dGraph(50,50)
sage: %timeit G.edge_labels()
100 loops, best of 3: 6.8 ms per loop

using instead return list(l for _,_,l in self.edge_iterator()) seems more efficient.

sage: G = graphs.PetersenGraph()
sage: %timeit G.edge_labels()
100000 loops, best of 3: 10.1 µs per loop

sage: G = graphs.Grid2dGraph(50,50)
sage: %timeit G.edge_labels()
100 loops, best of 3: 2.81 ms per loop

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2016

Changed commit from 16c8698 to c3a83e0

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

c9e7169Merge branch 'u/chapoton/21345' in 7.4.b2
c3a83e0trac 21345 even better

@fchapoton
Copy link
Contributor Author

comment:4

done, thanks

@dcoudert
Copy link
Contributor

comment:5

Hello. It is much better to use edge_iterator. We don't need to build the list of edges first and then iterate over that list.

sage: G = graphs.Grid2dGraph(50,50)
sage: %timeit [l for _, _, l in G.edges()]
100 loops, best of 3: 7.59 ms per loop
sage: %timeit [l for _, _, l in G.edge_iterator()]
100 loops, best of 3: 3.34 ms per loop

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2016

Changed commit from c3a83e0 to c62e295

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 26, 2016

Branch pushed to git repo; I updated commit sha1. New commits:

c62e295trac 21345 and better yet

@fchapoton
Copy link
Contributor Author

comment:7

done, thanks again !

@dcoudert
Copy link
Contributor

comment:8

We can certainly do better accessing directly the backends, but we already have a significant speed up.
Good to go.

@dcoudert
Copy link
Contributor

Reviewer: David Coudert

@vbraun
Copy link
Member

vbraun commented Aug 29, 2016

Changed branch from u/chapoton/21345 to c62e295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants