回答編集履歴

1

combinationsとかありえない

2018/09/26 00:12

投稿

quickquip
quickquip

スコア11046

test CHANGED
@@ -19,3 +19,39 @@
19
19
  result = [sentence[i] for i in sorted(random.sample(range(len(sentence)), k=3))]
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ ----
26
+
27
+
28
+
29
+ ```
30
+
31
+ In [1]: import itertools, random
32
+
33
+
34
+
35
+ In [2]: sentence = sorted(list(map(str, range(1000))))
36
+
37
+
38
+
39
+ In [3]: %timeit [sentence[i] for i in sorted(random.sample(range(len(sentence)), k=3))]
40
+
41
+ 6.7 µs ± 99.3 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
42
+
43
+
44
+
45
+ In [4]: %timeit [k for i, k in sorted(random.sample(list(enumerate(sentence)), k=3))]
46
+
47
+ 49.4 µs ± 604 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
48
+
49
+
50
+
51
+ In [5]: %time random.choice(list(itertools.combinations(sentence, 3)))
52
+
53
+ CPU times: user 23.3 s, sys: 15 s, total: 38.2 s
54
+
55
+ Wall time: 41.6 s
56
+
57
+ ```