-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathressourcemanagement.jl
101 lines (79 loc) · 1.91 KB
/
ressourcemanagement.jl
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
abstract type Message
end
struct DZip{S,T}
X::S
Y::T
end
struct Start
end
struct Close{T}
state::T
end
struct Postprocess{T,S} <: Message
state::T
signal::S
end
Base.iterate(M::Message) = getfield(M, 1), 1
Base.iterate(M::Message, Any) = getfield(M, 2), nothing
Base.iterate(M::Message, ::Nothing) = nothing
struct DRange{T}
start::T
stop::T
end
dyniterate(r::DRange, ::Start) = (r.start, r.start)
dyniterate(r::DRange, s) = (s == r.stop) ? nothing : (s+1, s+1)
dyniterate(r::DRange, E::Close) = nothing
Base.iterate(Z::DZip) = dyniterate(Z, Start())
Base.iterate(Z::DZip, state) = dyniterate(Z, state)
dyniterate(Z::DZip, ::Start) = dyniterate(Z::DZip, (Start(), Start()))
dyniterate(Z::DZip, (start, signal)::Postprocess{Start}) = dyniterate(Z::DZip, Postprocess((Start(), Start()), signal))
function dyniterate(Z::DZip, (p, q))
ϕ = dyniterate(Z.X, p)
ϕ === nothing && return nothing
x, p = ϕ
ϕ = dyniterate(Z.Y, q)
ϕ === nothing && return nothing
y, q = ϕ
(x, y), (p, q)
end
function dyniterate(Z::DZip, ((p,q), signal)::Postprocess)
ϕ = dyniterate(Z.X, p)
ϕ === nothing && return dyniterate(Z.Y, signal(q))
x, p = ϕ
ϕ = dyniterate(Z.Y, q)
ϕ === nothing && return dyniterate(Z.X, signal(p))
y, q = ϕ
(x, y), Postprocess((p, q), signal)
end
using BenchmarkTools
function f(n)
Z = zip(1:n, 1:n)
i = 0
for (x, y) in Z
i += x + 2y % 100
end
i
end
function g(n)
Z = DZip(DRange(1,n), DRange(1,n))
i = 0
for (x, y) in Z
i += x + 2y % 100
end
i
end
function h(n)
Z = DZip(DRange(1,n), DRange(1,n))
i = 0
ϕ = dyniterate(Z, Postprocess(Start(), x->Close(x)))
while !(ϕ === nothing)
(x, y), state = ϕ
i += x + 2y % 100
ϕ = dyniterate(Z, state)
ϕ === nothing && return i
end
i
end
@btime f(10000)
@btime g(10000)
@btime h(10000)