Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

# -*- coding: utf-8 -*- 

""" 

Performance Test for Clone Protocol 

 

see :class:`sage.structure.list_clone.ClonableArray` 

 

EXAMPLES:: 

 

sage: from sage.structure.list_clone_timings import * 

sage: cmd =["", 

....: "e.__copy__()", 

....: "copy(e)", 

....: "e.clone()", 

....: "e.__class__(e.parent(), e._get_list())", 

....: "e.__class__(e.parent(), e[:])", 

....: "e.check()", 

....: "", 

....: "add1_internal(e)", 

....: "add1_immutable(e)", 

....: "add1_mutable(e)", 

....: "add1_with(e)", 

....: "", 

....: "cy_add1_internal(e)", 

....: "cy_add1_immutable(e)", 

....: "cy_add1_mutable(e)", 

....: "cy_add1_with(e)"] 

 

Various timings using a Cython class:: 

 

sage: size = 5 

sage: e = IncreasingArrays()(range(size)) 

sage: # random 

....: for p in cmd: 

....: print("{0:36} : ".format(p), end=""); timeit(p) 

: 

e.__copy__() : 625 loops, best of 3: 446 ns per loop 

copy(e) : 625 loops, best of 3: 1.94 µs per loop 

e.clone() : 625 loops, best of 3: 736 ns per loop 

e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 1.34 µs per loop 

e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 1.35 µs per loop 

e.check() : 625 loops, best of 3: 342 ns per loop 

: 

add1_internal(e) : 625 loops, best of 3: 3.53 µs per loop 

add1_immutable(e) : 625 loops, best of 3: 3.72 µs per loop 

add1_mutable(e) : 625 loops, best of 3: 3.42 µs per loop 

add1_with(e) : 625 loops, best of 3: 4.05 µs per loop 

: 

cy_add1_internal(e) : 625 loops, best of 3: 752 ns per loop 

cy_add1_immutable(e) : 625 loops, best of 3: 1.28 µs per loop 

cy_add1_mutable(e) : 625 loops, best of 3: 861 ns per loop 

cy_add1_with(e) : 625 loops, best of 3: 1.51 µs per loop 

 

Various timings using a Python class:: 

 

sage: e = IncreasingArraysPy()(range(size)) 

sage: # random 

....: for p in cmd: print("{0:36} : ".format(p), end=""); timeit(p) 

: 

e.__copy__() : 625 loops, best of 3: 869 ns per loop 

copy(e) : 625 loops, best of 3: 2.13 µs per loop 

e.clone() : 625 loops, best of 3: 1.86 µs per loop 

e.__class__(e.parent(), e._get_list()) : 625 loops, best of 3: 7.52 µs per loop 

e.__class__(e.parent(), e[:]) : 625 loops, best of 3: 7.27 µs per loop 

e.check() : 625 loops, best of 3: 4.02 µs per loop 

: 

add1_internal(e) : 625 loops, best of 3: 9.34 µs per loop 

add1_immutable(e) : 625 loops, best of 3: 9.91 µs per loop 

add1_mutable(e) : 625 loops, best of 3: 12.6 µs per loop 

add1_with(e) : 625 loops, best of 3: 15.9 µs per loop 

: 

cy_add1_internal(e) : 625 loops, best of 3: 7.13 µs per loop 

cy_add1_immutable(e) : 625 loops, best of 3: 6.95 µs per loop 

cy_add1_mutable(e) : 625 loops, best of 3: 14.1 µs per loop 

cy_add1_with(e) : 625 loops, best of 3: 17.5 µs per loop 

""" 

#***************************************************************************** 

# Copyright (C) 2009-2010 Florent Hivert <Florent.Hivert@univ-rouen.fr> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

 

 

from sage.structure.list_clone import ClonableArray 

from sage.structure.list_clone_demo import IncreasingArrays 

from sage.structure.list_clone_timings_cy import * 

 

class IncreasingArraysPy(IncreasingArrays): 

 

class Element(ClonableArray): 

""" 

A small class for testing :class:`ClonableArray`: Increasing Lists 

 

TESTS:: 

 

sage: from sage.structure.list_clone_timings import IncreasingArraysPy 

sage: TestSuite(IncreasingArraysPy()([1,2,3])).run() 

""" 

 

def check(self): 

""" 

Check that ``self`` is increasing. 

 

EXAMPLES:: 

 

sage: from sage.structure.list_clone_timings import IncreasingArraysPy 

sage: IncreasingArraysPy()([1,2,3]) # indirect doctest 

[1, 2, 3] 

sage: IncreasingArraysPy()([3,2,1]) # indirect doctest 

Traceback (most recent call last): 

... 

ValueError: Lists is not increasing 

""" 

for i in range(len(self)-1): 

if self[i] >= self[i+1]: 

raise ValueError("Lists is not increasing") 

 

 

##################################################################### 

###### Timings functions ###### 

##################################################################### 

def add1_internal(bla): 

""" 

TESTS:: 

 

sage: from sage.structure.list_clone_timings import * 

sage: add1_internal(IncreasingArrays()([1,4,5])) 

[2, 5, 6] 

""" 

blo = bla.__copy__() 

lst = blo._get_list() 

for i in range(len(blo)): lst[i] += 1 

blo.set_immutable() 

blo.check() 

return blo 

 

def add1_immutable(bla): 

""" 

TESTS:: 

 

sage: from sage.structure.list_clone_timings import * 

sage: add1_immutable(IncreasingArrays()([1,4,5])) 

[2, 5, 6] 

""" 

lbla = bla[:] 

for i in range(len(lbla)): lbla[i] += 1 

return bla.__class__(bla.parent(), lbla) 

 

def add1_mutable(bla): 

""" 

TESTS:: 

 

sage: from sage.structure.list_clone_timings import * 

sage: add1_mutable(IncreasingArrays()([1,4,5])) 

[2, 5, 6] 

""" 

blo = bla.__copy__() 

for i in range(len(blo)): blo[i] += 1 

blo.set_immutable() 

blo.check() 

return blo 

 

def add1_with(bla): 

""" 

TESTS:: 

 

sage: from sage.structure.list_clone_timings import * 

sage: add1_with(IncreasingArrays()([1,4,5])) 

[2, 5, 6] 

""" 

with bla.clone() as blo: 

for i in range(len(blo)): blo[i] += 1 

return blo