Skip to content

Efficient unit conversion in array #518

@DylanMMarques

Description

@DylanMMarques

Hello, thanks for this package. I just started using it and it is helping me a lot in my work.

I looked into the documentation looking for a function that allow me to convert an Array described in Hz into the same Array but in described in MHz.
The way that I found would be:

using Unitful
a = rand(10) * 1u"Hz"
@time b = uconvert.(u"MHz", a)
#0.000018 seconds (4 allocations: 8.008 KiB)

However, this creates a new array and therefore allocates memory which I would like to avoid. I was wondering if there is a uconvert!(unit, a) available, that allows in place conversion of the array. This should be easy to do and may be already implemented. Something like:

a = rand(10) * 1u"Hz"
uconvert_Hz_to_MHz!(a) =  b = ustrip(a); b  .*= 1E-6;
@time b = uconvert_Hz_to_MHz!(a) 
# 0.000007 seconds (1 allocation: 32 bytes)

Is there already a function implemented that does this? If not, would it be possible to implement?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions