Reputation: 1729
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:
Elucidation of glaring flaws in my logic most welcome! Thx. J
Upvotes: 4
Views: 488
Reputation: 42214
Yes, the operations on Int8
s 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 Int64
s. 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