Hey,
I have an application that loads data from an XML API, parses that response using XmlToMap
and then iterates over the parsed data to transform it into maps and generally a better structure for future processing.
Part of that transformation is to parse the values I get. They can be booleans, text, numbers and timestamps (also just numbers tho).
Here is what I did:
def parse_value("true"), do: true
def parse_value("True"), do: true
def parse_value("TRUE"), do: true
def parse_value("false"), do: false
def parse_value("False"), do: false
def parse_value("FALSE"), do: false
def parse_value(value) do
case Cldr.Number.Parser.parse(value) do
{:ok, number} -> number
_ -> value
end
end
Yes, not the prettiest but it did the job. Unfortunately, the Cldr.Number.Parser.parse
slows everything down which leads to an execution time of the entire transformation of 5 seconds (my dev system) and 16 seconds (Raspberry Pi). This is pretty bad, because my plan was to fetch the data every 5 seconds and store it in an influxDB database.
Is there a way to improve the performance of Cldr.Number.Parser.parse
or can I use something else? I chose Cldr.Number.Parser.parse
because I can just throw stuff at it and it returns it, no matter if its an integer, or float, or whatever.
Comparison:
iex(23)> :timer.tc(MyApp, :parse_value, ["true"])
{3, true}
iex(21)> :timer.tc(MyApp, :parse_value, ["1"])
{8520, 1}
This function goes over my data map and returns a huge list of tuples.
iex(20)> :timer.tc(MyApp, :generate_influx_tuples, [data])
{584, [...]}