# Batch distance operations

**URL:** <https://community.konduit.ai/t/batch-distance-operations/781>\
**Category:** ND4J\
**Created:** [August 11, 2020, 6:17am UTC](https://community.konduit.ai/t/batch-distance-operations/781 "2020-08-11T06:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![madhs53](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@madhs53](https://community.konduit.ai/u/madhs53)\
**Post date:** [August 11, 2020, 6:17am UTC](https://community.konduit.ai/t/batch-distance-operations/781/1 "2020-08-11T06:17:06Z")

</div>

Hi all,

As part of calculating the silhouette coefficient on a set of clusters, we run code to calculate the distance between INDArray vectors:

`pointA.getArray().distance2(pointB.getArray())`

We currently run this for every point vs every other point in the same and other clusters.  
With large amounts of points in each cluster, this can take quite a long time to compute (we are running with a CUDA backend).

We were wondering if the overhead was between CPU and GPU. Would it be possible to batch these distance calculations and run them all at once? I had a look at Transforms.allEuclideanDistances, but the JavaDoc doesn’t have an explanation on the expected input or output.

Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![madhs53](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@madhs53](https://community.konduit.ai/u/madhs53)\
**Post date:** [August 12, 2020, 4:03am UTC](https://community.konduit.ai/t/batch-distance-operations/781/2 "2020-08-12T04:03:01Z")

</div>

Hi all,

I tried using `Transforms.allEuclideanDistances(arrayA, arrayB, 1);`

But it took 20 seconds to perform a batch calculation this way (1 vs 1000+ vectors), as opposed to 2 seconds to perform the same calculations individually in a for loop. It seems strange that doing them individually is so much faster.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [August 13, 2020, 8:52am UTC](https://community.konduit.ai/t/batch-distance-operations/781/3 "2020-08-13T08:52:41Z")

</div>

What are your dimensions you’re doing the batch operations on?  
Some reproduction code would be helpful.  
A quick JMH benchmark indicates that the for loop is slower.
