# A weird operation to optimize

**URL:** <https://discourse.julialang.org/t/a-weird-operation-to-optimize/128658>\
**Category:** General Usage\
**Created:** [May 3, 2025, 1:34pm UTC](https://discourse.julialang.org/t/a-weird-operation-to-optimize/128658 "2025-05-03T13:34:01Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [May 3, 2025, 4:04pm UTC](https://discourse.julialang.org/t/a-weird-operation-to-optimize/128658/14 "2025-05-03T16:04:07Z")

</div>

This is another version, in an attempt to make the code neater:

```julia
function weird_cumsum3!(out,in,I,J)
    Jrng = 1:J[end]
    @views out[Jrng] .= in[I[Jrng]]
    @views cumsum!(out[Jrng], out[Jrng])
    @views reverse(out) .= out[reverse(J)]
    return nothing
end

```

It suffers from some tiny allocations. Not sure where they stem from. But is still quite fast. Perhaps for large lengths, `cumsum!` might have optimizations.

---

_[View the full topic](https://discourse.julialang.org/t/a-weird-operation-to-optimize/128658)._
