On Fri, Aug 6, 2021 at 4:16 PM Kwankyu Lee <[email protected]> wrote:
>
>
>
> On Saturday, August 7, 2021 at 6:13:19 AM UTC+9 [email protected] wrote:
>>
>> Possible benefits may include:
>>
>> - potentially much faster: for similar reasons to numpy, a mutable sage
>> vector can be much faster than a generic Python list.
>>
To clarify, by "similar reasons to numpy", I meant that you open up
the possibility of using Cython, vectorized operations, JITs like
numba, etc. Some of these provide order of magnitude speedups and
aren't an option with generic Python lists. Here's a little example:
n = 10^6
def build():
v = vector(RDF, n)
for i in range(n):
v[i] = i/(i+1)
return v
def build2():
l = list()
for i in range(n):
l.append(i/(i+1.0))
return l
a = build()
b = build2()
%time a.sum()
1.49ms
%time sum(b)
188ms
Now imagine you are implementing an algorithm that modifies a bunch of
entries of a, then computes a sum. You can replace "sum" by any other
method on vectors, e.g., a dot product. If there were no mutable
vectors in Sage, then you would make whole classes of algorithms
possibly 100x slower to implement. Numpy (and their ilk, e.g., numba
jit, cython support for numpy, etc.) make these algorithms very fast
to implement...
>
> sage: def build():
> ....: v = vector(QQ, 1000)
> ....: for i in range(1000):
> ....: v[i] = i/(i+1)
> ....: return v
> ....:
> ....:
> sage: def build2():
> ....: l = list()
> ....: for i in range(1000):
> ....: l.append(i/(i+1))
> ....: return vector(QQ, l)
> ....:
> ....:
> sage:
> sage: timeit('build()')
> 125 loops, best of 3: 2.93 ms per loop
> sage: timeit('build2()')
> 125 loops, best of 3: 2.74 ms per loop
> sage: timeit('build()')
> 125 loops, best of 3: 2.9 ms per loop
> sage: timeit('build2()')
> 125 loops, best of 3: 2.73 ms per loop
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sage-devel/15652d60-1f36-43cf-bb64-94622d1438acn%40googlegroups.com.
--
William (http://wstein.org)
--
You received this message because you are subscribed to the Google Groups
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sage-devel/CACLE5GB%3D0pCoumwQz64L686JwcZ32kouB531EDF%3DUkP0Sjqv1w%40mail.gmail.com.