# Substantial increase in time in copying an instantiated broadcasted object vs a non-instantiated one

**URL:** <https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673>\
**Category:** Performance\
**Tags:** broadcast, linearalgebra\
**Created:** [January 11, 2024, 11:09am UTC](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673 "2024-01-11T11:09:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 11, 2024, 11:09am UTC](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673/1 "2024-01-11T11:09:02Z")

</div>

I’m trying to understand the difference in performance here:

```julia
julia> using LinearAlgebra

julia> n=40; U = UpperTriangular(rand(n,n)); C = similar(U); B = Broadcast.broadcasted(*, 2.0, U);

julia> @btime copyto!($C, $B);
  307.138 ns (0 allocations: 0 bytes)

julia> B2 = Broadcast.instantiate(B);

julia> @btime copyto!($C, $B2);
  483.149 ns (0 allocations: 0 bytes)

```

Why is it substantially more expensive to copy the instantiated object? This is a small difference that doesn’t scale with matrix size, but I’m trying to understand why this exists at all. I don’t observe a similar difference if `Array`s are involved.

This is on the current master.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 11, 2024, 11:48am UTC](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673/2 "2024-01-11T11:48:32Z")

</div>

Cannot quite reproduce the drastic difference on 1.10:

```julia-repl
julia> using LinearAlgebra, BenchmarkTools

julia> n=40; U = UpperTriangular(rand(n,n)); C = similar(U); B = Broadcast.broadcasted(*, 2.0, U);

julia> @btime copyto!($C, $B);
  303.206 ns (0 allocations: 0 bytes)

julia> B2 = Broadcast.instantiate(B);

julia> @btime copyto!($C, $B2);
  336.258 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 11, 2024, 11:49am UTC](https://discourse.julialang.org/t/substantial-increase-in-time-in-copying-an-instantiated-broadcasted-object-vs-a-non-instantiated-one/108673/3 "2024-01-11T11:49:58Z")

</div>

Yes, the difference is more pronounced on the current master than on v1.10
