BlockMap
and Developer`PartitionMap
do pretty the same thing in this case and they are roughly of same speed.
data = RandomReal[{-1, 1}, {1000000}];
F = Sin[#1 - #2] &;
a = BlockMap[F @@ # &, data, 2]; // RepeatedTiming // First
b = Developer`PartitionMap[F @@ # &, data, 2]; // RepeatedTiming // First
a === b
0.94367
0.927
True
This is however not as fast as your original version:
a1 = F @@@ Partition[data, 2]; // RepeatedTiming // First
a === a1
0.58
True
It may be also worthwhile to replace F
by a function G
with vector argument, because this circumvents Apply
.
G = Sin[First[#] - Last[#]] &;
a2 = BlockMap[G, data, 2]; // RepeatedTiming // First
b2 = Developer`PartitionMap[G, data, 2]; // RepeatedTiming // First
a === a2 === b === b2
0.24
0.23
True
But if F
is Listable
or composed of functions with the attribute Listable
(like in our case), the following allows us to exploit that:
a3 = F[data[[1 ;; ;; 2]], data[[2 ;; ;; 2]]]; // RepeatedTiming // First
a4 = F @@ Transpose[Partition[data, 2]]; // RepeatedTiming // First
a === a3 === a4
0.0029
0.0065
True
In this case, data
is a packed array and the components of F
are very well vectorized functions. So this will not as efficient in other, less specific cases.
Moreover, a4
might become more performant than a3
when the length of the sublists increases.
F @@@ Partition[{a,b,c,d,e,f},2]
? which kind of features are you looking for in a different solution? $\endgroup$Developer`PartitionMap
$\endgroup$Partition[data, 2, 2, {1, -1}, 0, F]
$\endgroup$Partition
rather than withPartition
. Make it shorter rather than make it longer. $\endgroup$