# Performance / Memory differences between constructors of BigInts

**URL:** https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863
**Category:** Performance
**Tags:** performance, numbers, type, memory-allocation
**Created:** [August 16, 2023, 2:13pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863 "2023-08-16T14:13:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maxasauruswall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxasauruswall/32/52263_2.png) [@maxasauruswall](https://discourse.julialang.org/u/maxasauruswall)
#### Post date: [August 16, 2023, 2:13pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/1 "2023-08-16T14:13:15Z")

</div>

Hi all,

I’m playing around with arbitrary precision integers in a jupyter notebook (Macbook air, Julia v1.10.0-beta1). I noticed a difference between two constructors of BigInts:

```julia
@btime BigInt(1234567890)
# 45.294 ns (2 allocations: 40 bytes)

@btime big"1234567890"
# 1.333 ns (0 allocations: 0 bytes)

```

I’m guessing this has something to do with the way Julia wraps the GMP package, or C code in general? Or maybe it’s something even more general, having to do with string literals versus constructors?

Anyway, just curious why these two would be different. Any pointers to the manual/resources where I could learn more would be greatly appreciated!

Thanks for the help.  
Cheers,  
Max

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [August 16, 2023, 2:26pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/2 "2023-08-16T14:26:29Z")

</div>

It’s possibly slightly surprising, but `big"1234567890"` isn’t actually a constructor. It just returns the same `BigInt` each time. (while `BigInt(1234567890)` does actually allocate a new `BigInt` each time.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 16, 2023, 2:28pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/3 "2023-08-16T14:28:57Z")

</div>

To expand on what @Oscar_Smith has shown consider:

```julia
julia> f1() = BigInt(1234567890)
f1 (generic function with 1 method)

julia> f2() = big"1234567890"
f2 (generic function with 1 method)

julia> f1() === f1()
false

julia> f2() === f2()
true

```

The reason is that `big"..."` is evaluated only once at compile time while `BigInt(...)` is evaluated at execution time. This happens because `BigInt` is mutable, so the compiler cannot optimize-out the `BigInt` call in the second case (if it did the `f1() === f1()` would produce `true`, and it should not).

You have the same situation with regex - `Regex(...)` vs `r"..."`.

---

<div class="post-metadata">

### Author: ![maxasauruswall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxasauruswall/32/52263_2.png) [@maxasauruswall](https://discourse.julialang.org/u/maxasauruswall)
#### Post date: [August 16, 2023, 3:43pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/4 "2023-08-16T15:43:10Z")

</div>

Ah… okay, I think I understand. And @btime only measures allocations in runtime, not compile time?

Thanks @bkamins and @Oscar_Smith

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [August 16, 2023, 3:54pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/5 "2023-08-16T15:54:51Z")

</div>

Yes - `@btime` measures execution time only. Even with `@time` you would not catch these allocations as they happen before code is run:

```julia
julia> @time big"123456789"
  0.000001 seconds
123456789

```

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 16, 2023, 4:07pm UTC](https://discourse.julialang.org/t/performance-memory-differences-between-constructors-of-bigints/102863/6 "2023-08-16T16:07:11Z")

</div>

But this is also quite risky to use:

```julia
julia> foo() = big"0"
foo (generic function with 2 methods)

julia> x = foo()
0

julia> Base.GMP.MPZ.add!(x, big(1))
1

julia> foo() # look out!
1

```
