What is the difference between reduce
vs. fold
with respect to their technical implementation?
I understand that they differ by their signature as fold
accepts additional parameter (i.e. initial value) which gets added to each partition output.
fold
?Thanks in advance.
There is no practical difference when it comes to performance whatsoever:
RDD.fold
action is using fold
on the partition Iterators
which is implemented using foldLeft
.RDD.reduce
is using reduceLeft
on the partition Iterators
.Both methods keep mutable accumulator and process partitions sequentially using simple loops with foldLeft
implemented like this:
foreach (x => result = op(result, x))
and reduceLeft
like this:
for (x <- self) {
if (first) {
...
}
else acc = op(acc, x)
}
Practical difference between these methods in Spark is only related to their behavior on empty collections and ability to use mutable buffer (arguably it is related to performance). You'll find some discussion in Why is the fold action necessary in Spark?
Moreover there is no difference in the overall processing model:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With