Jim Maas
Jim Maas

Reputation: 1729

Julia working with large arrays, is there any memory/speed advantage to using Int8 vs Int64?

Julia 1.5.3 on Ubuntu. I'm writing an iterative model, form of microsimulation and converting from R. The array sizes can get quite large as the population grows yet are all comprised of Integers, and small ones at that. Is there any advantage to using Int8 in this case in terms of either memory or speed, as opposed to what appears to me to be the default of Int64?

Is there a way to tell Julia, some global/universal setting that can be changed to use Int8 as default rather than Int64 unless otherwise instructed?

Have come across my first problem. I have a function that works with Int8 values, but when I call it as such:

Pop = InitialPop(StaNum, LayNum, MaxSts, MaxPar, MaxDay, MaxTrt)

I get this error:

ERROR: MethodError: no method matching InitialPop(::Int8, ::Int8, ....

This function simply creates an initial starting "population" or organisms so my question is:

  1. Is it better to just use default Int64 methods and convert the whole array to Int8 after it is produced or
  2. What do I need to do to make it all work in Int8 and overcome the above error?

Elucidation of glaring flaws in my logic most welcome! Thx. J

Upvotes: 4

Views: 488

Answers (1)

Przemyslaw Szufel
Przemyslaw Szufel

Reputation: 42214

Yes, the operations on Int8s will be much faster and will consume less memory - Int8 takes 1 byte of RAM while Int64 takes 8 bytes of RAM (see the benchmark below). However, for the meta-parameters of the model (such as population size) it is OK to stick with Int64s. They are used only once and perhaps not a central part of your computation.

Consider the following two vectors:

a = rand(Int8(0):Int8(127), 1000)
b = rand(0:127, 1000)

Let us see how much time and memory is needed to substract 5 from each element of those vectors:

julia> using BenchmarkTools

julia> @btime $a.-Int8(5);
  118.053 ns (1 allocation: 1.06 KiB)

julia> @btime $b.-Int64(5);
  581.959 ns (1 allocation: 7.94 KiB)

You can see that the speedup is 4x and the memory consumption is 8x less.

Upvotes: 4

Related Questions