回答編集履歴
2
追記
test
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
コードの速さが気になるのであれば、numpy.arrayへのキャストなくすべきでしょう。
|
20
20
|
|
21
|
-
|
21
|
+
また検索があるので、listやnp.arrayではなくsetを使うとだいぶ早くなります。
|
22
22
|
|
23
23
|
```python
|
24
24
|
|
@@ -34,7 +34,7 @@
|
|
34
34
|
|
35
35
|
for n in range(1, 101):
|
36
36
|
|
37
|
-
a = [grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))]
|
37
|
+
a = set([grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))])
|
38
38
|
|
39
39
|
grundy_ceil.append(next(i for i in count() if i not in a))
|
40
40
|
|
@@ -50,6 +50,12 @@
|
|
50
50
|
|
51
51
|
追記:完全に趣味ですが。[ここ](https://stackoverflow.com/questions/7088625/what-is-the-most-efficient-way-to-check-if-a-value-exists-in-a-numpy-array)を参考に。
|
52
52
|
|
53
|
+
yag1kazさんのlistからの取り出し方も取り入れました。
|
54
|
+
|
55
|
+
sliceをリスト内包表記で書くと遅くなるのは盲点でした…
|
56
|
+
|
57
|
+
|
58
|
+
|
53
59
|
```python
|
54
60
|
|
55
61
|
from timeit import timeit
|
@@ -150,24 +156,80 @@
|
|
150
156
|
|
151
157
|
|
152
158
|
|
159
|
+
def f4():
|
160
|
+
|
161
|
+
grundy_ceil = [0]
|
162
|
+
|
163
|
+
for n in range(1, 101):
|
164
|
+
|
165
|
+
a = set(grundy_ceil[:-math.ceil(n/2+1):-1])
|
166
|
+
|
167
|
+
j = next(i for i in count() if i not in a)
|
168
|
+
|
169
|
+
grundy_ceil.append(j)
|
170
|
+
|
171
|
+
df = pd.DataFrame(grundy_ceil).T
|
172
|
+
|
173
|
+
return df
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
a = f0()
|
178
|
+
|
179
|
+
b = f1()
|
180
|
+
|
181
|
+
c = f2()
|
182
|
+
|
183
|
+
d = f3()
|
184
|
+
|
185
|
+
e = f4()
|
186
|
+
|
187
|
+
print('f1', np.allclose(a[0].values, b[0].values))
|
188
|
+
|
189
|
+
print('f2', np.allclose(a[0].values, c[0].values))
|
190
|
+
|
191
|
+
print('f3', np.allclose(a[0].values, d[0].values))
|
192
|
+
|
193
|
+
print('f4', np.allclose(a[0].values, e[0].values))
|
194
|
+
|
195
|
+
'''
|
196
|
+
|
197
|
+
一応同じ結果になることを確認
|
198
|
+
|
199
|
+
f1 True
|
200
|
+
|
201
|
+
f2 True
|
202
|
+
|
203
|
+
f3 True
|
204
|
+
|
205
|
+
f4 True
|
206
|
+
|
207
|
+
'''
|
208
|
+
|
153
209
|
n = 1000
|
154
210
|
|
155
|
-
print('Original {}'.format(timeit(f0, number=n)))
|
211
|
+
print('Original {}'.format(timeit(f0, number=n)))
|
156
|
-
|
212
|
+
|
157
|
-
print('list {}'.format(timeit(f1, number=n)))
|
213
|
+
print('list {}'.format(timeit(f1, number=n)))
|
158
|
-
|
214
|
+
|
159
|
-
print('np.array {}'.format(timeit(f2, number=n)))
|
215
|
+
print('np.array {}'.format(timeit(f2, number=n)))
|
160
|
-
|
216
|
+
|
161
|
-
print('set {}'.format(timeit(f3, number=n)))
|
217
|
+
print('set {}'.format(timeit(f3, number=n)))
|
218
|
+
|
162
|
-
|
219
|
+
print('slice+set {}'.format(timeit(f4, number=n)))
|
220
|
+
|
163
|
-
|
221
|
+
'''
|
164
|
-
|
222
|
+
|
165
|
-
|
223
|
+
Original 9.65767323700129
|
166
|
-
|
224
|
+
|
167
|
-
|
225
|
+
list 1.1692692330107093
|
168
|
-
|
226
|
+
|
169
|
-
|
227
|
+
np.array 7.38920716199209
|
170
|
-
|
228
|
+
|
171
|
-
|
229
|
+
set 0.9272018460032996
|
230
|
+
|
231
|
+
slice+set 0.6420557640085462
|
232
|
+
|
233
|
+
'''
|
172
234
|
|
173
235
|
```
|
1
追記
test
CHANGED
@@ -43,3 +43,131 @@
|
|
43
43
|
df = pd.DataFrame(grundy_ceil).T
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
---
|
50
|
+
|
51
|
+
追記:完全に趣味ですが。[ここ](https://stackoverflow.com/questions/7088625/what-is-the-most-efficient-way-to-check-if-a-value-exists-in-a-numpy-array)を参考に。
|
52
|
+
|
53
|
+
```python
|
54
|
+
|
55
|
+
from timeit import timeit
|
56
|
+
|
57
|
+
from itertools import count
|
58
|
+
|
59
|
+
import math
|
60
|
+
|
61
|
+
import pandas as pd
|
62
|
+
|
63
|
+
import numpy as np
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
pd.options.display.max_columns = None
|
68
|
+
|
69
|
+
pd.options.display.notebook_repr_html = True
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def f0():
|
74
|
+
|
75
|
+
grundy_ceil = np.array([0])
|
76
|
+
|
77
|
+
#grundy_ceil_1p = np.array([0])
|
78
|
+
|
79
|
+
for n in range(1, 101):
|
80
|
+
|
81
|
+
compare = np.array([])
|
82
|
+
|
83
|
+
compare = np.append(compare, [grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))])
|
84
|
+
|
85
|
+
for j in range(compare.size + 1):
|
86
|
+
|
87
|
+
if not j in compare:
|
88
|
+
|
89
|
+
grundy_ceil = np.append(grundy_ceil, j)
|
90
|
+
|
91
|
+
break
|
92
|
+
|
93
|
+
df = pd.DataFrame(grundy_ceil).T
|
94
|
+
|
95
|
+
return df
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
def f1():
|
100
|
+
|
101
|
+
grundy_ceil = [0]
|
102
|
+
|
103
|
+
for n in range(1, 101):
|
104
|
+
|
105
|
+
a = [grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))]
|
106
|
+
|
107
|
+
j = next(i for i in count() if i not in a)
|
108
|
+
|
109
|
+
grundy_ceil.append(j)
|
110
|
+
|
111
|
+
df = pd.DataFrame(grundy_ceil).T
|
112
|
+
|
113
|
+
return df
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def f2():
|
118
|
+
|
119
|
+
grundy_ceil = [0]
|
120
|
+
|
121
|
+
for n in range(1, 101):
|
122
|
+
|
123
|
+
a = np.array([grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))])
|
124
|
+
|
125
|
+
j = next(i for i in count() if i not in a)
|
126
|
+
|
127
|
+
grundy_ceil.append(j)
|
128
|
+
|
129
|
+
df = pd.DataFrame(grundy_ceil).T
|
130
|
+
|
131
|
+
return df
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def f3():
|
136
|
+
|
137
|
+
grundy_ceil = [0]
|
138
|
+
|
139
|
+
for n in range(1, 101):
|
140
|
+
|
141
|
+
a = set([grundy_ceil[n - i] for i in range(1, math.ceil(n /2 + 1))])
|
142
|
+
|
143
|
+
j = next(i for i in count() if i not in a)
|
144
|
+
|
145
|
+
grundy_ceil.append(j)
|
146
|
+
|
147
|
+
df = pd.DataFrame(grundy_ceil).T
|
148
|
+
|
149
|
+
return df
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
n = 1000
|
154
|
+
|
155
|
+
print('Original {}'.format(timeit(f0, number=n)))
|
156
|
+
|
157
|
+
print('list {}'.format(timeit(f1, number=n)))
|
158
|
+
|
159
|
+
print('np.array {}'.format(timeit(f2, number=n)))
|
160
|
+
|
161
|
+
print('set {}'.format(timeit(f3, number=n)))
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
# Original 9.262939481996
|
166
|
+
|
167
|
+
# list 1.147810668015154
|
168
|
+
|
169
|
+
# np.array 7.4433728019939736
|
170
|
+
|
171
|
+
# set 0.8586325150099583
|
172
|
+
|
173
|
+
```
|