Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/structure/list_clone_timings.py
8814 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
84
from sage.structure.list_clone_demo import IncreasingArrays
85
from sage.structure.list_clone_timings_cy import *
86
87
class IncreasingArraysPy(IncreasingArrays):
88
89
class Element(ClonableArray):
90
"""
91
A small class for testing :class:`ClonableArray`: Increasing Lists
92
93
TESTS::
94
95
sage: from sage.structure.list_clone_timings import IncreasingArraysPy
96
sage: TestSuite(IncreasingArraysPy()([1,2,3])).run()
97
"""
98
99
def check(self):
100
"""
101
Check that ``self`` is increasing.
102
103
EXAMPLES::
104
105
sage: from sage.structure.list_clone_timings import IncreasingArraysPy
106
sage: IncreasingArraysPy()([1,2,3]) # indirect doctest
107
[1, 2, 3]
108
sage: IncreasingArraysPy()([3,2,1]) # indirect doctest
109
Traceback (most recent call last):
110
...
111
ValueError: Lists is not increasing
112
"""
113
for i in range(len(self)-1):
114
if self[i] >= self[i+1]:
115
raise ValueError, "Lists is not increasing"
116
117
118
#####################################################################
119
###### Timings functions ######
120
#####################################################################
121
def add1_internal(bla):
122
"""
123
TESTS::
124
125
sage: from sage.structure.list_clone_timings import *
126
sage: add1_internal(IncreasingArrays()([1,4,5]))
127
[2, 5, 6]
128
"""
129
blo = bla.__copy__()
130
lst = blo._get_list()
131
for i in range(len(blo)): lst[i] += 1
132
blo.set_immutable()
133
blo.check()
134
return blo
135
136
def add1_immutable(bla):
137
"""
138
TESTS::
139
140
sage: from sage.structure.list_clone_timings import *
141
sage: add1_immutable(IncreasingArrays()([1,4,5]))
142
[2, 5, 6]
143
"""
144
lbla = bla[:]
145
for i in range(len(lbla)): lbla[i] += 1
146
return bla.__class__(bla.parent(), lbla)
147
148
def add1_mutable(bla):
149
"""
150
TESTS::
151
152
sage: from sage.structure.list_clone_timings import *
153
sage: add1_mutable(IncreasingArrays()([1,4,5]))
154
[2, 5, 6]
155
"""
156
blo = bla.__copy__()
157
for i in range(len(blo)): blo[i] += 1
158
blo.set_immutable()
159
blo.check()
160
return blo
161
162
def add1_with(bla):
163
"""
164
TESTS::
165
166
sage: from sage.structure.list_clone_timings import *
167
sage: add1_with(IncreasingArrays()([1,4,5]))
168
[2, 5, 6]
169
"""
170
with bla.clone() as blo:
171
for i in range(len(blo)): blo[i] += 1
172
return blo
173
174