# "Cat" very slow for relativelly small tasks

**URL:** <https://discourse.julialang.org/t/cat-very-slow-for-relativelly-small-tasks/76067>\
**Category:** Performance\
**Created:** [February 9, 2022, 10:38am UTC](https://discourse.julialang.org/t/cat-very-slow-for-relativelly-small-tasks/76067 "2022-02-09T10:38:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 9, 2022, 10:38am UTC](https://discourse.julialang.org/t/cat-very-slow-for-relativelly-small-tasks/76067/1 "2022-02-09T10:38:40Z")

</div>

This make my 16GB system hang for a while and eventually crash with a StackOverflowError, even if `top` doesn’t show all this memory usage (the matrices themselves should be ~ 400 MB)

```julia
mainM = rand(28,28,60000)
toAdd = [rand(28,28) for i in 1:12800]
totM = cat(mainM, toAdd...,dims=3)

```

curios who is the culprit and which alternatives I could use…

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [February 9, 2022, 10:50am UTC](https://discourse.julialang.org/t/cat-very-slow-for-relativelly-small-tasks/76067/2 "2022-02-09T10:50:44Z")

</div>

found a workaround:

```julia
mainM = rand(28,28,60000)
toAdd = [rand(28,28) for i in 1:12800] 
B = reduce(hcat, toAdd)
C = reshape(B, 28, 28, :)
totM = cat(mainM, C, dims=3)

```

It seems `cat` didn’t like the many concatenations deriving from using the splat operator…

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 9, 2022, 10:55am UTC](https://discourse.julialang.org/t/cat-very-slow-for-relativelly-small-tasks/76067/3 "2022-02-09T10:55:23Z")

</div>

If you needed `vcat` or `hcat`, then the common suggestion is to use `reduce` instead of splatting. But I don’t think `reduce` works performantly with the general `cat`…  
So I’d suggest the following solution, somewhat cleaner than `reshape`:

```julia
julia> using SplitApplyCombine
julia> cat(mainM, combinedims(toAdd), dims=3)

```

Uses an external package, but `SplitApplyCombine` is useful for many other tasks in working with array- or table-like data as well.
