Package Guide
UnitfulData is designed to expand the package Unitful to the most common Information and Information rate units. It can also easily identify the size of objects and files in the correct unit for development and profiling purposes. Quick start
Quick start
You can install UnitfulData using the Julia package manager. From Julia REPL, type ] to enter the Pkg REPL mode and run
pkg> add UnitfulData
To load the package in your environment, you can run in the Julia REPL:
julia> using UnitfulData
Adding More Units and Custom Prefixes
The current package exports the base @unit
macro from the Unitful.jl
package to add more units. To create a new unit and extend it using a custom prefix – different from the Unitful's default power-of-ten prefix or just using a subset of it – you can use the macro [@unit_custom_prefix
]@(ref).
julia> 10u_test==1Au_test
true
julia> 4u_test==1Bu_test
true
julia> 27u_test==1Cu_test
true
If the macro @unit_custom_prefix
is provided without a prefix, it will use the default prefix_data
constant, which includes the positive power-of-tens up to Yotta
(10^24) and the power-of-twos up to 80 Yi
(2^80).
To add new units to a precompiled package, you will need to add, at the end of your package and after all the new units and dimensions, an initialization function to register the new units with Unitful
const localunits = copy(Unitful.basefactors)
const localpromotion = copy(Unitful.promotion) # only if you have used @dimension
function __init__()
merge!(Unitful.basefactors, localunits)
merge!(Unitful.promotion, localpromotion) # only if you've used @dimension
Unitful.register(UnitfulData) #register newly created units
end
Memory and Allocation
You can check the memory used by an object using data_summary
.
julia> julia> data_summary(a; unit=MByte) # total size of the object in MBytes (base 10)
8.000055999999999 MByte
julia> data_summary(aa; unit=MiByte) # total size of the object in MiBytes (base 2)
7.629447937011719 MiByte
julia> data_summary(aa; unit=bit, exclude=Array) # size of the object itself in bits
128 bit
Similarly, to check the memory allocated during the execution of an expression, you can use `@data_allocated
julia> @data_allocated tests(1,b) bit # memory allocated in bits
256 bit
julia> @data_allocated tests(1,c) nat # memory allocated in natural units
177.445678223346 nat
If no second term is provided to the macro, the result will be shown in Bytes