You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder if there is a fast ByteString Builder for numbers from Data.Fixed?
I've written one myself, but don't think it's quite as fast as it could be:
--| Encode a 'Fixed' resolution number using ASCII digits.fixedDec::HasResolutiona=>Fixeda->BB.Builder
fixedDec fa@(MkFixed a) | a <0=P.primFixed P.char8 '-'<> fixedDec (asTypeOf (MkFixed (negate a)) fa)
fixedDec fa@(MkFixed a) = integerDec i <>P.primFixed P.char8 '.'<> stimesMonoid zeros (P.primFixed P.char8 '0') <> integerDec d
where
(i, d) = a `quotRem` resolution fa
zeros ::Integer=if d ==0then0elseceiling (logBase10 (fromIntegral (resolution fa) /fromIntegral d /10) ::Double)
Calculating logBase 10 gets expensive, so it doesn't seem to perform well with lots of Fixed numbers.
Ideally if there was an efficient builder of fixed, it could be included in bytestring. Anyone have ideas of how to make it more efficient?
The text was updated successfully, but these errors were encountered:
I was able to get some pretty good results (sub 1ms for 10,000 calls based on benchmarks) with this version:
--| Encode a 'Fixed' resolution number using ASCII digits.fixedDec::HasResolutiona=>Fixeda->Builder
fixedDec fa@(MkFixed a) | a <0=P.primFixed P.char8 '-'<> fixedDec (asTypeOf (MkFixed (negate a)) fa)
fixedDec fa@(MkFixed a) = integerDec i <>P.primFixed P.char8 '.'<>P.primMapListFixed P.char8 (List.genericReplicate zeros '0') <> integerDec d
where!(i, d) = a `quotRem` resolution fa
zeros ::Integer=if d ==0then0elseceiling (logBase10 (fromIntegral (resolution fa) /fromIntegral d /10) ::Double)
You can about half (like 500μs) that by approximating Fixed to "Word64" instead of Integer:
--| Encode a 'Fixed' resolution number using ASCII digits, approximated to Word64.fixedDecWord64::HasResolutiona=>Fixeda->Builder
fixedDecWord64 fa@(MkFixed a) | a <0=P.primFixed P.char8 '-'<> fixedDecWord64 (asTypeOf (MkFixed (negate a)) fa)
fixedDecWord64 fa@(MkFixed a) = word64Dec i <>P.primFixed P.char8 '.'<>P.primMapListFixed P.char8 (List.replicate zeros '0') <> word64Dec d
where!(i, d) =fromIntegral a `quotRem`fromIntegral (resolution fa)
zeros ::Int=if d ==0then0elseceiling (logBase10 (fromIntegral (resolution fa) /fromIntegral d /10) ::Double)
I wonder if there is a fast ByteString Builder for numbers from Data.Fixed?
I've written one myself, but don't think it's quite as fast as it could be:
Calculating
logBase 10
gets expensive, so it doesn't seem to perform well with lots of Fixed numbers.Ideally if there was an efficient builder of fixed, it could be included in bytestring. Anyone have ideas of how to make it more efficient?
The text was updated successfully, but these errors were encountered: