teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

3

追記

2019/02/03 08:29

投稿

quickquip
quickquip

スコア11314

answer CHANGED
@@ -74,4 +74,14 @@
74
74
 
75
75
  In : %timeit slice_deque(x, 700000,10)
76
76
  257 µs ± 19.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
77
+ ```
78
+
79
+ 追記
80
+ **長い**dequeから、**長い**部分要素を取り出すならisliceの方が有利です。(popを繰り返す操作がネックになりますから)
81
+ ```plain
82
+ In : %timeit y = list(islice(x, 700000, 100000))
83
+ 2.21 ms ± 24.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
84
+
85
+ In : %timeit slice_deque(x, 700000, 100000)
86
+ 9.72 ms ± 48.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
77
87
  ```

2

スライスに関して

2019/02/03 08:29

投稿

quickquip
quickquip

スコア11314

answer CHANGED
@@ -42,4 +42,36 @@
42
42
 
43
43
  In : %timeit y = list(x)[-100:]
44
44
  5.93 µs ± 636 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
45
+ ```
46
+
47
+ ----
48
+ 追記
49
+
50
+ [https://docs.python.org/ja/3/library/collections.html#deque-recipes](https://docs.python.org/ja/3/library/collections.html#deque-recipes)
51
+ > To implement deque slicing, use a similar approach applying rotate() to bring a target element to the left side of the deque. Remove old entries with popleft(), add new entries with extend(), and then reverse the rotation. With minor variations on that approach, it is easy to implement Forth style stack manipulations such as dup, drop, swap, over, pick, rot, and roll.
52
+
53
+ ```python
54
+ def slice_deque(d, i, n):
55
+ d.rotate(-i)
56
+ t = [d.popleft() for _i in range(n)]
57
+ d.extend(t)
58
+ d.rotate(i+n)
59
+ return t
60
+ ```
61
+
62
+
63
+ ```plain
64
+ In : x = deque(range(1000000), 1000000)
65
+
66
+ In : %timeit y = list(islice(x, 100, 10))
67
+ 808 ns ± 12.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
68
+
69
+ In : %timeit slice_deque(x, 100,10)
70
+ 1.99 µs ± 52.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
71
+
72
+ In %timeit y = list(islice(x, 700000, 10))
73
+ 2.31 ms ± 44.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
74
+
75
+ In : %timeit slice_deque(x, 700000,10)
76
+ 257 µs ± 19.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
45
77
  ```

1

追求

2019/02/03 07:22

投稿

quickquip
quickquip

スコア11314

answer CHANGED
@@ -1,5 +1,6 @@
1
1
  一般的にこれが最良という方法はありません。
2
2
  dequeはどういうアルゴリズムで動いているかを中までわかった上でないと**速くならない**し、実際にどのぐらいのデータをどのように扱うかを前提にしないと使うべきなのか判断できない、かなり低水準なライブラリなのです。
3
+ (だからこそスライス実装を提供していないように思えます)
3
4
 
4
5
  **長い**dequeから、**少数の**末尾を取り出す**だいたいは**速い方法は
5
6
  ```python