# Is there an in-place alternative to +=, \*=, etc.?

**URL:** https://discourse.julialang.org/t/is-there-an-in-place-alternative-to-etc/70520
**Category:** Performance
**Tags:** question
**Created:** [October 27, 2021, 10:34pm UTC](https://discourse.julialang.org/t/is-there-an-in-place-alternative-to-etc/70520 "2021-10-27T22:34:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![failedentertainment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/failedentertainment/32/30255_2.png) [@failedentertainment](https://discourse.julialang.org/u/failedentertainment)
#### Post date: [October 27, 2021, 10:34pm UTC](https://discourse.julialang.org/t/is-there-an-in-place-alternative-to-etc/70520/1 "2021-10-27T22:34:21Z")

</div>

Using += with arbitrary precision integers seems to end up using much more memory (and takes much longer) than the equivalent in-place operator (for my test, I used addeq! from Nemo and add! from Base.GMP.MPZ).

Result:

```julia
julia> @time test_mpz(BigInt(5)^200000)
  0.045873 seconds (41 allocations: 354.750 KiB)

julia> @time test_nemo(fmpz(5)^200000)
  0.047201 seconds (38 allocations: 354.664 KiB)

julia> @time test_default(BigInt(5)^200000)
  0.491928 seconds (20.04 k allocations: 554.338 MiB, 0.75% gc time)

```

Code:

```julia
julia> function test_mpz(n)
           output = zero(n)
           for i in 1:10000
               MPZ.add!(output,n)
           end
       end
test_mpz (generic function with 1 method)

julia> function test_nemo(n)
           output = zero(n)
           for i in 1:10000
               addeq!(output, n)
           end
       end
test_nemo (generic function with 1 method)

julia> function test_default(n)
           output = zero(n)
           for i in 1:10000
               output += n
           end
       end
test_default (generic function with 1 method)

```

This seems to be related to mutability of BigInts; is there no built in +=, \*=, etc. operators in Julia that work in-place like mul! in Nemo or Base.GMP.MPZ? (I guess one could argue that the latter is “built in,” but I wanted to know if there were any alternatives). Sorry if this question is basic or has been answered. It seems like in-place ought to be the default behavior for BigInts at least, given the orders of magnitude worse performance in space and time.

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [November 5, 2021, 3:47pm UTC](https://discourse.julialang.org/t/is-there-an-in-place-alternative-to-etc/70520/2 "2021-11-05T15:47:09Z")

</div>

Maybe something like [https://github.com/simonbyrne/InplaceOps.jl](https://github.com/simonbyrne/InplaceOps.jl) could help?
