AVA
AVA

Reputation: 2568

What is the best suited type for big numbers?

I would like to create a unique transaction ids, IBANs randomly in julia.
I arrived range of transaction ids in [0:10^58] per millennium as IBAN domain is [A-Z]{2}[0-9]{32} or [A-Z]{34} (ie. max 10^51) and assume max 10 txns per day per account.

Whether should I select transaction id type as String or Bigint and their pros and cons in txn processing system and decision support systems?

Upvotes: 1

Views: 139

Answers (2)

StefanKarpinski
StefanKarpinski

Reputation: 33290

@Mathieu_B's suggestion of using UUIDs makes sense but if you prefer something strictly numerical, you can use UInt128s easily as well:

julia> rand(UInt128)
0xa62ff810bcd06e644f5df0a64611cc0b

The number of bits in a UUID and a UInt128 are the same, so this should be at least as unique as UUIDs generated by uuid4() (i.e. randomly). The main benefit of UUIDs is that there are other methods implemented, should you need to use them:

  • uuid1: based on date-time and MAC address
  • uuid4: random/pseudo-random
  • uuid5: hash-based

Upvotes: 4

Mathieu B
Mathieu B

Reputation: 471

If what you are looking for is an identifier, systems like UUIDs may be more appropriate. In Julia, the UUIDs module from the standard library can help:

julia> using UUIDs: uuid4

julia> import Random

julia> Random.seed!(43);

julia> uuid4()
UUID("7d6398a5-f5bb-4164-8552-1668b964e46f")

julia> uuid4()
UUID("93938d68-980f-40b1-b1f9-789c5230116c")

Comparing with UUIDs is faster than with String, and about the same as BigInt. Here is a quick benchmark using BenchmarkTools.jl, the setup part is not taken into account for runtime estimation:

julia> using BenchmarkTools: @btime

julia> @btime s1 == s2 setup=(s1=string(uuid4()); s2=string(uuid4()))
  5.685 ns (0 allocations: 0 bytes)

julia> @btime s1 == s2 setup=(s1=uuid4(); s2=uuid4())
  1.640 ns (0 allocations: 0 bytes)

julia> @btime s1 == s2 setup=(s1=BigInt(rand(Int)); s2=BigInt(rand(Int)))
  2.976 ns (0 allocations: 0 bytes)

Upvotes: 6

Related Questions