Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/list_clone_timings.py
4056 views
1
# -*- coding: utf-8 -*-
2
"""
3
Performance Test for Clone Protocol
4
5
see :class:`sage.structure.list_clone.ClonableArray`
6
7
EXAMPLES::
8
9
sage: from sage.structure.list_clone_timings import *
10
sage: cmd =["",
11
... "e.__copy__()",
12
... "copy(e)",
13
... "e.clone()",
14
... "e.__class__(e.parent(), e._get_list())",
15
... "e.__class__(e.parent(), e[:])",
16
... "e.check()",
17
... "",
18
... "add1_internal(e)",
19
... "add1_immutable(e)",
20
... "add1_mutable(e)",
21
... "add1_with(e)",
22
... "",
23
... "cy_add1_internal(e)",
24
... "cy_add1_immutable(e)",
25
... "cy_add1_mutable(e)",
26
... "cy_add1_with(e)"]
27
28
Various timings using a Cython class::
29
30
sage: size = 5
31
sage: e = IncreasingArrays()(range(size))
32
sage: # random
33
... for p in cmd: print "{0:36} : ".format(p),; timeit(p)
34
:
35
e.__copy__() : 625 loops, best of 3: 446 ns per loop
36
copy(e) : 625 loops, best of 3: 1.94 µs per loop
37
e.clone() : 625 loops, best of 3: 736 ns per loop
38
e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 1.34 µs per loop
39
e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 1.35 µs per loop
40
e.check() : 625 loops, best of 3: 342 ns per loop
41
:
42
add1_internal(e) : 625 loops, best of 3: 3.53 µs per loop
43
add1_immutable(e) : 625 loops, best of 3: 3.72 µs per loop
44
add1_mutable(e) : 625 loops, best of 3: 3.42 µs per loop
45
add1_with(e) : 625 loops, best of 3: 4.05 µs per loop
46
:
47
cy_add1_internal(e) : 625 loops, best of 3: 752 ns per loop
48
cy_add1_immutable(e) : 625 loops, best of 3: 1.28 µs per loop
49
cy_add1_mutable(e) : 625 loops, best of 3: 861 ns per loop
50
cy_add1_with(e) : 625 loops, best of 3: 1.51 µs per loop
51
52
Various timings using a Python class::
53
54
sage: e = IncreasingArraysPy()(range(size))
55
sage: # random
56
... for p in cmd: print "{0:36} : ".format(p),; timeit(p)
57
:
58
e.__copy__() : 625 loops, best of 3: 869 ns per loop
59
copy(e) : 625 loops, best of 3: 2.13 µs per loop
60
e.clone() : 625 loops, best of 3: 1.86 µs per loop
61
e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 7.52 µs per loop
62
e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 7.27 µs per loop
63
e.check() : 625 loops, best of 3: 4.02 µs per loop
64
:
65
add1_internal(e) : 625 loops, best of 3: 9.34 µs per loop
66
add1_immutable(e) : 625 loops, best of 3: 9.91 µs per loop
67
add1_mutable(e) : 625 loops, best of 3: 12.6 µs per loop
68
add1_with(e) : 625 loops, best of 3: 15.9 µs per loop
69
:
70
cy_add1_internal(e) : 625 loops, best of 3: 7.13 µs per loop
71
cy_add1_immutable(e) : 625 loops, best of 3: 6.95 µs per loop
72
cy_add1_mutable(e) : 625 loops, best of 3: 14.1 µs per loop
73
cy_add1_with(e) : 625 loops, best of 3: 17.5 µs per loop
74
"""
75
#*****************************************************************************
76
# Copyright (C) 2009-2010 Florent Hivert <[email protected]>
77
#
78
# Distributed under the terms of the GNU General Public License (GPL)
79
# http://www.gnu.org/licenses/
80
#*****************************************************************************
81
82
83
from sage.structure.list_clone import ClonableArray, IncreasingArrays
84
from sage.structure.list_clone_timings_cy import *
85
86
class IncreasingArraysPy(IncreasingArrays):
87
88
class Element(ClonableArray):
89
"""
90
A small class for testing :class:`ClonableArray`: Increasing Lists
91
92
TESTS::
93
94
sage: from sage.structure.list_clone_timings import IncreasingArraysPy
95
sage: TestSuite(IncreasingArraysPy()([1,2,3])).run()
96
"""
97
98
def check(self):
99
"""
100
Check that ``self`` is increasing.
101
102
EXAMPLES::
103
104
sage: from sage.structure.list_clone_timings import IncreasingArraysPy
105
sage: IncreasingArraysPy()([1,2,3]) # indirect doctest
106
[1, 2, 3]
107
sage: IncreasingArraysPy()([3,2,1]) # indirect doctest
108
Traceback (most recent call last):
109
...
110
AssertionError: Lists is not increasing
111
"""
112
for i in range(len(self)-1):
113
assert self[i] < self[i+1], "Lists is not increasing"
114
115
116
#####################################################################
117
###### Timings functions ######
118
#####################################################################
119
from list_clone import *
120
from list_clone_timings_cy import *
121
122
def add1_internal(bla):
123
"""
124
TESTS::
125
126
sage: from sage.structure.list_clone_timings import *
127
sage: add1_internal(IncreasingArrays()([1,4,5]))
128
[2, 5, 6]
129
"""
130
blo = bla.__copy__()
131
lst = blo._get_list()
132
for i in range(len(blo)): lst[i] += 1
133
blo.set_immutable()
134
blo.check()
135
return blo
136
137
def add1_immutable(bla):
138
"""
139
TESTS::
140
141
sage: from sage.structure.list_clone_timings import *
142
sage: add1_immutable(IncreasingArrays()([1,4,5]))
143
[2, 5, 6]
144
"""
145
lbla = bla[:]
146
for i in range(len(lbla)): lbla[i] += 1
147
return bla.__class__(bla.parent(), lbla)
148
149
def add1_mutable(bla):
150
"""
151
TESTS::
152
153
sage: from sage.structure.list_clone_timings import *
154
sage: add1_mutable(IncreasingArrays()([1,4,5]))
155
[2, 5, 6]
156
"""
157
blo = bla.__copy__()
158
for i in range(len(blo)): blo[i] += 1
159
blo.set_immutable()
160
blo.check()
161
return blo
162
163
def add1_with(bla):
164
"""
165
TESTS::
166
167
sage: from sage.structure.list_clone_timings import *
168
sage: add1_with(IncreasingArrays()([1,4,5]))
169
[2, 5, 6]
170
"""
171
with bla.clone() as blo:
172
for i in range(len(blo)): blo[i] += 1
173
return blo
174
175