回答編集履歴

3

2024/09/21 07:05

投稿

melian
melian

スコア20574

test CHANGED
@@ -11,3 +11,35 @@
11
11
 
12
12
  # [0, 0, 0, 0, 1, 2, 3, 0, 0, 1, 2, 0, 0]
13
13
  ```
14
+
15
+ ### 追記
16
+
17
+ ```julia
18
+ $ julia
19
+ help?> zip
20
+ search: zip
21
+
22
+ zip(iters...)
23
+
24
+ Run multiple iterators at the same time, until any of them is exhausted.
25
+ The value type of the zip iterator is a tuple of values of its subiterators.
26
+ ```
27
+
28
+ `zip()`(イテレータ)を利用する方が3倍程度速く、メモリ使用量も少ないです。
29
+ ```julia
30
+ using DataFrames
31
+ import ShiftedArrays: lag
32
+ using BenchmarkTools
33
+
34
+ test = DataFrame(a=[1,2,3,2,2,2,2,4,5,5,5,6,7])
35
+
36
+ @btime accumulate((acc, i) -> i ? acc+1 : 0,
37
+ test.a .== lag(test.a, default=test.a[1]-1))
38
+ @btime accumulate((acc, (i, j)) -> i==j ? acc+1 : 0,
39
+ zip(test.a, lag(test.a, default=0));
40
+ init=test.a[1]-1)
41
+
42
+ # 3.859 μs (22 allocations: 1.59 KiB)
43
+ # 1.304 μs (8 allocations: 416 bytes)
44
+ ```
45
+

2

2024/09/21 06:30

投稿

melian
melian

スコア20574

test CHANGED
@@ -5,8 +5,8 @@
5
5
 
6
6
  test = DataFrame(a=[1,2,3,2,2,2,2,4,5,5,5,6,7])
7
7
 
8
- newList = accumulate((acc, (i, j)) -> i==j ? acc+1 : 0, zip(test.a, lag(test.a, default=0));
8
+ newList = accumulate((acc, i) -> i ? acc+1 : 0,
9
- init=test.a[1]-1)
9
+ test.a .== lag(test.a, default=test.a[1]-1));
10
10
  println(newList)
11
11
 
12
12
  # [0, 0, 0, 0, 1, 2, 3, 0, 0, 1, 2, 0, 0]

1

2024/09/21 04:17

投稿

melian
melian

スコア20574

test CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  test = DataFrame(a=[1,2,3,2,2,2,2,4,5,5,5,6,7])
7
7
 
8
- newList = accumulate((acc, i) -> i[1]==i[2] ? acc+1 : 0, zip(test.a, lag(test.a, default=0));
8
+ newList = accumulate((acc, (i, j)) -> i==j ? acc+1 : 0, zip(test.a, lag(test.a, default=0));
9
9
  init=test.a[1]-1)
10
10
  println(newList)
11
11