#13352: Running time improvement of the bitset_len method
---------------------------------+----------------------------
Reporter: dcoudert | Owner: jason
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-5.12
Component: misc | Resolution:
Keywords: | Merged in:
Authors: David Coudert | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: | Stopgaps:
---------------------------------+----------------------------
Changes (by dcoudert):
* cc: ncohen (added)
Old description:
> This patch improves the running time of the {{{bitset_len}}} method by
> using fast methods for counting bits in 32 and 64 bits integers
> (popcount). It
> - adds file {{{bitcount.pxi}}} to {{{sage/misc/}}}. This file contains
> the functions for counting bits in 32 or 64 bits integers
> - adds corresponding tests to file {{{misc_c.pyx}}}
> - modifies the {{{bitset_len}}} function accordingly
>
> Before:
> {{{
> sage: x = FrozenBitset('10'*1003002); len(x)
> 1003002
> sage: %timeit len(x)
> 125 loops, best of 3: 5.27 ms per loop
> sage: x = FrozenBitset('10'*10705); len(x)
> 10705
> sage: %timeit len(x)
> 625 loops, best of 3: 56.3 µs per loop
> }}}
>
> After:
> {{{
> sage: x = FrozenBitset('10'*1003002); len(x)
> 1003002
> sage: %timeit len(x)
> 625 loops, best of 3: 865 µs per loop
> sage: x = FrozenBitset('10'*10705); len(x)
> 10705
> sage: %timeit len(x)
> 625 loops, best of 3: 9.39 µs per loop
> }}}
>
> The ``popcount_32`` method is the same than the function used in patch
> #12371.
>
> The alternative would be to use functions {{{__builtin_popcount()}}} and
> {{{__builtin_popcountll()}}}. They are extremely fast but they required
> to add flag ``-mpopcnt`` or ``-msse4.2`` to the gcc compiler. Can we do
> that? how?
>
> APPLY:
>
> * [attachment:trac_13352_bitset_len.patch]
New description:
This patch improves the running time of the {{{bitset_len}}} method by
using {{{__builtin_popcount()}}}.
Before:
{{{
sage: B = Bitset('10'*10000)
sage: len(B)
10000
sage: %timeit len(B)
1000 loops, best of 3: 245 us per loop
}}}
After:
{{{
sage: B = Bitset('10'*10000)
sage: len(B)
10000
sage: %timeit len(B)
100000 loops, best of 3: 2.52 us per loop
}}}
APPLY:
* [attachment:trac_13352_bitset_len_v2.patch]
--
Comment:
Hello,
Since I have now understood how to use {{{__builtin_popcountl}}} (not
obvious when you don't know), I have uploaded a new version of the patch.
before:
{{{
sage: B = Bitset('00'*10000+'1')
sage: len(B)
1
sage: %timeit len(B)
1000000 loops, best of 3: 268 ns per loop
sage: B = Bitset('10'*10000)
sage: len(B)
10000
sage: %timeit len(B)
1000 loops, best of 3: 245 us per loop
sage: B = Bitset('10'*1000000)
sage: len(B)
1000000
sage: %timeit len(B)
10 loops, best of 3: 24.1 ms per loop
}}}
after:
{{{
sage: B = Bitset('00'*10000+'1')
sage: len(B)
1
sage: %timeit len(B)
1000000 loops, best of 3: 275 ns per loop
sage: B = Bitset('10'*10000)
sage: len(B)
10000
sage: %timeit len(B)
100000 loops, best of 3: 2.52 us per loop
sage: B = Bitset('10'*1000000)
sage: len(B)
1000000
sage: %timeit len(B)
1000 loops, best of 3: 236 us per loop
}}}
I'm wondering if I should add a wrapper in case the
{{{__builtin_popcountl}}} method is not available. This is not difficult,
but I don't know which environnement variables should be tested.
Thanks.
--
Ticket URL: <http://trac.sagemath.org/ticket/13352#comment:8>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.