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

Faster complement and conjugate methods for Composition using naive method #25691

Open
mathzeta opened this issue Jun 28, 2018 · 2 comments
Open

Comments

@mathzeta
Copy link

For compositions of n greater than about 55, it seems that the naive method of constructing the complement composition using the difference of sets is faster. This obviously also can affect the conjugate() method.

In the attached branch, I added a "complement_naive" method. Few timings:

sage: C = Compositions(20)
sage: A = [C.random_element() for i in range(300)]
sage: %timeit B = [c.complement() for c in A]
100 loops, best of 3: 5.29 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
100 loops, best of 3: 8.48 ms per loop

sage: C = Compositions(50)
sage: A = [C.random_element() for i in range(300)]
sage: %timeit B = [c.complement() for c in A]
100 loops, best of 3: 10.1 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
100 loops, best of 3: 10.3 ms per loop

sage: C = Compositions(55)
sage: A = [C.random_element() for i in range(300)]
sage: %timeit B = [c.complement() for c in A]
100 loops, best of 3: 10.9 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
100 loops, best of 3: 10.5 ms per loop

sage: C = Compositions(100)
sage: A = [C.random_element() for i in range(200)]
sage: %timeit B = [c.complement() for c in A]
100 loops, best of 3: 12.6 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
100 loops, best of 3: 8.44 ms per loop

sage: C = Compositions(200)
sage: A = [C.random_element() for i in range(300)]
sage: %timeit B = [c.complement() for c in A]
10 loops, best of 3: 39.8 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
100 loops, best of 3: 17.2 ms per loop

sage: C = Compositions(1000)
sage: A = [C.random_element() for i in range(300)]
sage: %timeit B = [c.complement() for c in A]
1 loop, best of 3: 396 ms per loop
sage: %timeit B = [c.complement_naive() for c in A]
10 loops, best of 3: 59.9 ms per loop

This happens probably because a random composition of n have "many" small parts

Component: combinatorics

Keywords: days94

Author: Tomer Bauer

Branch/Commit: u/mathzeta2/25691_composition_complement_naive @ 5a53744

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

@mathzeta mathzeta added this to the sage-8.3 milestone Jun 28, 2018
@mathzeta
Copy link
Author

Commit: 5a53744

@mathzeta
Copy link
Author

@mathzeta mathzeta changed the title Faster Composition.complement() using naive method Faster complement and conjugate methods for Composition using naive method Jun 28, 2018
@mkoeppe mkoeppe removed this from the sage-8.3 milestone Dec 29, 2022
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

2 participants