-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
plot_value_caching.py
78 lines (59 loc) · 2.31 KB
/
plot_value_caching.py
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
"""
Usage of ValueCachingWrapper in Neuraxle.
=============================================================
This demonstrates how you can use value caching in a Neuraxle pipeline.
..
Copyright 2019, Neuraxio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import shutil
import time
import numpy as np
from neuraxle.pipeline import Pipeline
from neuraxle.steps.caching import JoblibValueCachingWrapper
from neuraxle.steps.loop import ForEach
from neuraxle.steps.misc import Sleep
from neuraxle.steps.numpy import MultiplyByN
def main():
value_caching_folder = 'value_caching'
if not os.path.exists(value_caching_folder):
os.makedirs(value_caching_folder)
data_inputs = list(range(100))
sleep_time = 0.001
a = time.time()
for i in range(5):
p = Pipeline([
JoblibValueCachingWrapper(
ForEach(Pipeline([Sleep(sleep_time=sleep_time), MultiplyByN(2)])),
cache_folder=value_caching_folder
)
])
outputs_value_caching = p.transform(data_inputs)
b = time.time()
time_value_caching_pipeline = b - a
print('Pipeline with ValueCachingWrapper')
print('execution time: {} seconds'.format(time_value_caching_pipeline))
a = time.time()
for i in range(5):
p = Pipeline([
ForEach(Pipeline([Sleep(sleep_time=sleep_time), MultiplyByN(2)])),
])
outputs_vanilla = p.transform(data_inputs)
b = time.time()
time_vanilla_pipeline = b - a
print('Pipeline without value caching')
print('execution time: {} seconds'.format(time_vanilla_pipeline))
shutil.rmtree(value_caching_folder)
assert np.array_equal(outputs_value_caching, outputs_vanilla)
assert time_value_caching_pipeline < time_vanilla_pipeline
if __name__ == '__main__':
main()