# Fastest way to apply function to rows without slices

**URL:** <https://discourse.julialang.org/t/fastest-way-to-apply-function-to-rows-without-slices/69257>\
**Category:** General Usage\
**Created:** [October 5, 2021, 9:07pm UTC](https://discourse.julialang.org/t/fastest-way-to-apply-function-to-rows-without-slices/69257 "2021-10-05T21:07:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abc](https://avatars.discourse-cdn.com/v4/letter/a/2bfe46/32.png) [@abc](https://discourse.julialang.org/u/abc)\
**Post date:** [October 5, 2021, 9:07pm UTC](https://discourse.julialang.org/t/fastest-way-to-apply-function-to-rows-without-slices/69257/1 "2021-10-05T21:07:12Z")

</div>

Hi!

I have a function which takes two 2-element Array{Float64,1} as input. An example (mine is much more complicated, but can’t be shared sadly). It can look something like this.

```julia
function Example(x,y)
    return norm(x.-y)
end

```

Imagine now that I have two Nx2-element Array{Float64,2} `xv` and `yv`, where N is a large number. Of course, with a for loop and slices, we can apply `Èxample` to each row, by

```julia
for i in N
     Example(xv[i,:],yv[i,:])
end

```

I want to do this fast and I want to avoid slices since I want to use ForwardDiff later on. I have tried pmap and mapslices with no luck. Any tips?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [October 5, 2021, 9:47pm UTC](https://discourse.julialang.org/t/fastest-way-to-apply-function-to-rows-without-slices/69257/2 "2021-10-05T21:47:33Z")

</div>

It may help to provide an example of what you need to differentiate. Instead of elementwise operations on N x 2 arrays, you may want to consider reshaping into a 2 X N array to take advantage of Julia’s column-major memory layout, or better yet, a vector of `StaticVector`s. Your original example can be written as

```julia
map(Example, eachrow(xv), eachrow(yv))

```
