# Diff! (in-place or preallocated output version of diff)

**URL:** https://discourse.julialang.org/t/diff-in-place-or-preallocated-output-version-of-diff/41949
**Category:** Performance
**Tags:** question
**Created:** [June 23, 2020, 6:12pm UTC](https://discourse.julialang.org/t/diff-in-place-or-preallocated-output-version-of-diff/41949 "2020-06-23T18:12:33Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [June 23, 2020, 6:12pm UTC](https://discourse.julialang.org/t/diff-in-place-or-preallocated-output-version-of-diff/41949/1 "2020-06-23T18:12:33Z")

</div>

The Julia manual nicely makes the case for pre-allocating outputs:  
[https://docs.julialang.org/en/latest/manual/performance-tips/#Pre-allocating-outputs-1](https://docs.julialang.org/en/latest/manual/performance-tips/#Pre-allocating-outputs-1)  
Yet the function `diff` that is used extensively in scientific computing seems not to have a corresponding `diff!`. I searched and could not find one so I wrote one myself (with some extra features like periodic boundary conditions):  
[https://github.com/JeffFessler/MIRT.jl/blob/master/src/regularize/diffl.jl](https://github.com/JeffFessler/MIRT.jl/blob/master/src/regularize/diffl.jl)  
Would some version of `diff!` be appropriate for the standard library, or is it better left to external packages? (And did I reinvent the wheel?)

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [June 23, 2020, 6:22pm UTC](https://discourse.julialang.org/t/diff-in-place-or-preallocated-output-version-of-diff/41949/2 "2020-06-23T18:22:16Z")

</div>

You might find this package of interest

> [@\[ANN\]: StaticKernels.jl - Fast Kernel Operations on Arrays](https://discourse.julialang.org/t/ann-statickernels-jl-fast-kernel-operations-on-arrays/37658):
>
> [StaticKernels.jl](https://github.com/stev47/StaticKernels.jl) is a new package that aims to enable easy and fast execution of kernel operations on arrays, e.g. finite differences, convolutions, image filters and morphological operations, etc. custom kernel functions in arbitrary dimensions custom boundary handling allocation-free execution small size (currently ~300 loc) and dependency free Introduction You can think of a Kernel as a function that takes a neighbourhood Window view on the underlying data as an argument. The kernel funct…

---

<div class="post-metadata">

### Author: ![JeffFessler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jefffessler/32/6650_2.png) [@JeffFessler](https://discourse.julialang.org/u/JeffFessler)
#### Post date: [June 24, 2020, 3:16am UTC](https://discourse.julialang.org/t/diff-in-place-or-preallocated-output-version-of-diff/41949/3 "2020-06-24T03:16:31Z")

</div>

Thanks so much for the pointer to that package!  
I updated my version to use `@inbounds` and then added a comparison and found that `StaticKernels` and my “diff specific” code had essentially identical compute times. That is a testament to the efficiency of the more general approach in `StaticKernels`  
[https://github.com/JeffFessler/MIRT.jl/blob/master/time/diffl.jl](https://github.com/JeffFessler/MIRT.jl/blob/master/time/diffl.jl)

My advice to anyone who comes here looking for `diff!` is to use `StaticKernels` but be sure to use `map!` not `map`.  
Here’s an equivalent of `diff!(out, in, dims=2)` for a 2D input `in`:

```julia
@inline kf(w) = @inbounds w[0,0] - w[0,-1] # for dims=2
k = Kernel{(0:0,-1:0)}(kf)
map!(k, out, extend(in, StaticKernels.ExtensionReplicate()))

```
