Debian Codegraph @

docs/optimizations.rst

raw
1 :doc:`/index`
2
3 .. _optimizations_label:
4
5 Optimizations
6 =============
7
8 If you are dealing with large nested objects and ignore_order=True, chances are DeepDiff takes a while to calculate the diff. Here are some tips that may help you with optimizations and progress report.
9
10
11 Optimized Libraries
12 -------------------
13
14 If you dump DeepDiff or Delta objects as json, you can improve the performance by installing orjson.
15 DeepDiff will automatically use orjson instead of Python's built-in json library to do json serialization.
16
17 pip install "deepdiff[optimize]"
18
19
20 Max Passes
21 ----------
22
23 :ref:`max_passes_label` comes with the default of 10000000.
24 If you don't need to exactly pinpoint the difference and you can get away with getting a less granular report, you can reduce the number of passes. It is recommended to get a diff of your objects with the defaults max_passes and take a look at the stats by running :ref:`get_stats_label` before deciding to reduce this number. In many cases reducing this number does not yield faster results.
25
26 A new pass is started each time 2 iterables are compared in a way that every single item that is different from the first one is compared to every single item that is different in the second iterable.
27
28 .. _max_diffs_label:
29
30 Max Diffs
31 ---------
32
33 max_diffs: Integer, default = None
34 max_diffs defined the maximum number of diffs to run on objects to pin point what exactly is different. This is only used when ignore_order=True. Every time 2 individual items are compared a diff is counted. The default value of None means there is no limit in the number of diffs that will take place. Any positive integer can make DeepDiff stop doing the calculations upon reaching that max_diffs count.
35
36 You can run diffs and then :ref:`get_stats_label` to see how many diffs and passes have happened.
37
38 >>> from deepdiff import DeepDiff
39 >>> diff=DeepDiff(1, 2)
40 >>> diff
41 {'values_changed': {'root': {'new_value': 2, 'old_value': 1}}}
42 >>> diff.get_stats()
43 {'PASSES COUNT': 0, 'DIFF COUNT': 1, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False}
44 >>> diff=DeepDiff([[1,2]], [[2,3,1]])
45 >>> diff.get_stats()
46 {'PASSES COUNT': 0, 'DIFF COUNT': 8, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False}
47 >>> diff=DeepDiff([[1,2]], [[2,3,1]], ignore_order=True)
48 >>> diff.get_stats()
49 {'PASSES COUNT': 3, 'DIFF COUNT': 6, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False}
50
51 .. note::
52 Compare :ref:`max_diffs_label` with :ref:`max_passes_label`
53
54
55 .. _cache_size_label:
56
57 Cache Size
58 ----------
59
60 cache_size : int >= 0, default=0
61 Cache size to be used to improve the performance. A cache size of zero means it is disabled.
62 Using the cache_size can dramatically improve the diff performance especially for the nested objects at the cost of more memory usage. However if cache hits rate is very low, having a cache actually reduces the performance.
63
64 Cache Examples
65 --------------
66
67 For example lets take a look at the performance of the benchmark_deeply_nested_a in the `DeepDiff-Benchmark repo <https://github.com/seperman/deepdiff-benchmark/blob/master/benchmark.py>`_ .
68
69 No Cache
70 ^^^^^^^^
71
72 With the no cache option we have the following stats:
73
74 {'PASSES COUNT': 11234, 'DIFF COUNT': 107060, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 10}
75
76 Yes it has taken 10 seconds to do the diff!
77
78 .. figure:: _static/benchmark_deeply_nested_a__3.8__ignore_order=True__cache_size=0__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
79 :alt: cache_size=0
80
81 cache_size=0
82
83 Cache Size 500
84 ^^^^^^^^^^^^^^
85
86 With a cache size of 500, we are doing the same diff in 2.5 seconds! And the memory usage has not changed. It is still hovering around 100Mb.
87
88 {'PASSES COUNT': 3960, 'DIFF COUNT': 19469, 'DISTANCE CACHE HIT COUNT': 11847, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 2}
89
90 As you can see the number of passes and diff counts have gone down and instead the distance cache hit count has gone up.
91
92 .. figure:: _static/benchmark_deeply_nested_a__3.8__ignore_order=True__cache_size=500__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
93 :alt: cache_size=500
94
95 cache_size=500
96
97
98 Cache Size 500 and Cache Tuning Sample Size 500
99 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
100
101 With a cache size of 500, we set the :ref:`cache_tuning_sample_size_label` to be 500 too. And we have a slight improvement. we are doing the same diff in 2 seconds now. And the memory usage has not changed. It is still hovering around 100Mb.
102
103 {'PASSES COUNT': 3960, 'DIFF COUNT': 19469, 'DISTANCE CACHE HIT COUNT': 11847, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 2}
104
105 As you can see in this case none of the stats have changed compared to the previous stats.
106
107 .. figure:: _static/benchmark_deeply_nested_a__3.8__ignore_order=True__cache_size=500__cache_tuning_sample_size=500__cutoff_intersection_for_pairs=1.png
108 :alt: cache_size=500 cache_tuning_sample_size=500
109
110 cache_size=500 cache_tuning_sample_size=500
111
112
113 Cache Size of 5000
114 ^^^^^^^^^^^^^^^^^^
115
116 Let's pay a little attention to our stats. Particularly to 'DISTANCE CACHE HIT COUNT': 11847 and the fact that the memory usage has not changed so far. What if we bump the cache_size to 5000 and disable cache_tuning_sample_size?
117
118 {'PASSES COUNT': 1486, 'DIFF COUNT': 6637, 'DISTANCE CACHE HIT COUNT': 3440, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 0}
119
120 We get the result calculated below 1 second! And the memory usage is only slightly above 100Mb.
121
122 .. figure:: _static/benchmark_deeply_nested_a__3.8__ignore_order=True__cache_size=5000__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
123 :alt: cache_size=5000
124
125 cache_size=5000
126
127
128
129 .. _cache_tuning_sample_size_label:
130
131 Cache Tuning Sample Size
132 ------------------------
133
134 cache_tuning_sample_size : int >= 0, default = 0
135 cache_tuning_sample_size is an experimental feature. It works hands in hands with the :ref:`cache_size_label`. When cache_tuning_sample_size is set to anything above zero, it will sample the cache usage with the passed sample size and decide whether to use the cache or not. And will turn it back on occasionally during the diffing process. This option can be useful if you are not sure if you need any cache or not. However you will gain much better performance with keeping this parameter zero and running your diff with different cache sizes and benchmarking to find the optimal cache size.
136
137 .. note::
138 A good start with cache_tuning_sample_size is to set it to the size of your cache.
139
140
141 .. _diffing_numbers_optimizations_label:
142
143 Optimizations for Diffing Numbers
144 ---------------------------------
145
146 If you are diffing lists of python numbers, you could get performance improvement just by installing numpy. DeepDiff will use Numpy to improve the performance behind the scene.
147
148 For example lets take a look at the performance of the benchmark_array_no_numpy vs. benchmark_numpy_array in the `DeepDiff-Benchmark repo <https://github.com/seperman/deepdiff-benchmark/blob/master/benchmark.py>`_.
149
150 In this specific test, we have 2 lists of numbers that have nothing in common: `mat1 <https://github.com/seperman/deepdiff-benchmark/blob/master/data/mat1.txt>`_ and `mat2 <https://github.com/seperman/deepdiff-benchmark/blob/master/data/mat2.txt>`_ .
151
152 No Cache and No Numpy
153 ^^^^^^^^^^^^^^^^^^^^^
154
155 With the no cache option and no Numpy installed we have the following stats:
156
157 {'PASSES COUNT': 1, 'DIFF COUNT': 439944, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 30}
158
159 Yes it has taken 30 seconds to do the diff!
160
161 .. figure:: _static/benchmark_array_no_numpy__3.8__ignore_order=True__cache_size=0__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
162 :alt: cache_size=0 and no Numpy
163
164 cache_size=0 and no Numpy
165
166 Cache Size 10000 and No Numpy
167 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168
169 What if we increase the cache size to 10000?
170
171 {'PASSES COUNT': 1, 'DIFF COUNT': 439944, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 35}
172
173 Not only it didn't help, it increased the diff time by 15%!!
174
175 Worse, if you look at the stats you see that the cache hit count is zero. This has happened since the 2 lists of items have nothing in common and hence caching the results does not improve the performance.
176
177
178 .. figure:: _static/benchmark_array_no_numpy__3.8__ignore_order=True__cache_size=10000__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
179 :alt: cache_size=10000 and no Numpy
180
181 cache_size=10000 and no Numpy
182
183 No Cache and Numpy
184 ^^^^^^^^^^^^^^^^^^
185
186 Let's install Numpy now. Set the cache_size=0 and run the diff again.
187
188 Yay, the same diff is done in 5 seconds!
189
190 {'PASSES COUNT': 1, 'DIFF COUNT': 1348, 'DISTANCE CACHE HIT COUNT': 0, 'MAX PASS LIMIT REACHED': False, 'MAX DIFF LIMIT REACHED': False, 'DURATION SEC': 5}
191
192 As you can see the memory usage has gone up from around 500Mb to around 630Mb.
193
194 .. figure:: _static/benchmark_numpy_array__3.8__ignore_order=True__cache_size=0__cache_tuning_sample_size=0__cutoff_intersection_for_pairs=1.png
195 :alt: Numpy but no cache
196
197 Numpy but no cache
198
199
200 Pypy
201 ----
202
203 If you are diffing big blobs of data that do not mainly include numbers, you may gain some performance improvement by running DeepDiff on Pypy3 instead of cPython.
204
205 For example lets take a look at the performance of the benchmark_big_jsons in the `DeepDiff-Benchmark repo <https://github.com/seperman/deepdiff-benchmark/blob/master/benchmark.py>`_.
206
207 First we will run it on cPython 3.8:
208
209 It takes around 17.5 seconds and 40Mb of memory:
210
211 .. figure:: _static/benchmark_big_jsons__3.8__ignore_order=True__cache_size=0__cache_tuning_sample_size=0__max_diffs=300000__max_passes=40000__cutoff_intersection_for_pairs=1.png
212 :alt: Nested blob of text diffed in Python3.8
213
214 Nested blob of text diffed in Python3.8
215
216 And then we run it in Pypy3.6-7.3.0. It takes 12 seconds now but around 110Mb of memory.
217
218 .. figure:: _static/benchmark_big_jsons__pypy3.6__ignore_order=True__cache_size=0__cache_tuning_sample_size=0__max_diffs=300000__max_passes=40000__cutoff_intersection_for_pairs=1.png
219 :alt: Nested blob of text diffed in Pypy3.6-7.3.0
220
221 Nested blob of text diffed in Pypy3.6-7.3.0
222
223 .. note::
224 Note that if you are diffing numbers, and have Numpy installed as recommended, cPython will have a better performance than Pypy. But if you are diffing blobs of mixed strings and some numbers, Pypy will have a better CPU performance and worse memory usage.
225
226
227 Cutoff Intersection For Pairs
228 -----------------------------
229
230 :ref:`cutoff_intersection_for_pairs_label` which is only used when ignore_order=True can have a huge affect on the granularity of the results and the performance. A value of zero essentially stops DeepDiff from doing passes while a value of 1 forced DeepDiff to do passes on iterables even when they are very different. Running passes is an expensive operation.
231
232 As an example of how much this parameter can affect the results in deeply nested objects, please take a look at :ref:`distance_and_diff_granularity_label`.
233
234 .. _cache_purge_level:
235
236 Cache Purge Level
237 -----------------
238
239 cache_purge_level: int, 0, 1, or 2. default=1
240 cache_purge_level defines what objects in DeepDiff should be deleted to free the memory once the diff object is calculated. If this value is set to zero, most of the functionality of the diff object is removed and the most memory is released. A value of 1 preserves all the functionalities of the diff object. A value of 2 also preserves the cache and hashes that were calculated during the diff calculations. In most cases the user does not need to have those objects remained in the diff unless for investigation purposes.
241
242
243 .. _zip_ordered_iterables_label:
244
245 Zip Ordered Iterables
246 ---------------------
247
248 zip_ordered_iterables: Boolean, default = False
249 When comparing ordered iterables such as lists, DeepDiff tries to find the smallest difference between the two iterables to report. That means that items in the two lists are not paired individually in the order of appearance in the iterables. Sometimes, that is not the desired behavior. Set this flag to True to make DeepDiff pair and compare the items in the iterables in the order they appear.
250
251
252 >>> from pprint import pprint
253 >>> from deepdiff import DeepDiff
254 >>> t1 = ["a", "b", "d", "e"]
255 >>> t2 = ["a", "b", "c", "d", "e"]
256 >>> DeepDiff(t1, t2)
257 {'iterable_item_added': {'root[2]': 'c'}}
258
259 When this flag is set to True and ignore_order=False, diffing will be faster.
260
261 >>> diff=DeepDiff(t1, t2, zip_ordered_iterables=True)
262 >>> pprint(diff)
263 {'iterable_item_added': {'root[4]': 'e'},
264 'values_changed': {'root[2]': {'new_value': 'c', 'old_value': 'd'},
265 'root[3]': {'new_value': 'd', 'old_value': 'e'}}}
266
267
268 .. _threshold_to_diff_deeper_label:
269
270 Threshold To Diff Deeper
271 ------------------------
272
273 threshold_to_diff_deeper: float, default = 0.33
274 threshold_to_diff_deeper is a number between 0 and 1. When comparing dictionaries that have a small intersection of keys, we will report the dictionary as a new_value instead of reporting individual keys changed. If you set it to zero, you get the same results as DeepDiff 7.0.1 and earlier, which means this feature is disabled. The new default is 0.33 which means if less that one third of keys between dictionaries intersect, report it as a new object.
275
276
277 >>> from deepdiff import DeepDiff
278 >>> t1 = {"veggie": "carrots"}
279 >>> t2 = {"meat": "carrots"}
280 >>>
281 >>> DeepDiff(t1, t2, threshold_to_diff_deeper=0)
282 {'dictionary_item_added': ["root['meat']"], 'dictionary_item_removed': ["root['veggie']"]}
283 >>> DeepDiff(t1, t2, threshold_to_diff_deeper=0.33)
284 {'values_changed': {'root': {'new_value': {'meat': 'carrots'}, 'old_value': {'veggie': 'carrots'}}}}
285
286
287 Back to :doc:`/index`