ada*_*max 7 performance parsing haskell
我正在寻找一种有效的方法来从文本文件中读取数字而无需安装其他软件包.Data.ByteString.Lazy.Char8.readInt似乎是整数的诀窍.我已经读过ByteString现在有一个readDouble方法,但是当我编写import Data.ByteString.Lex.Lazy.Double (readDouble)编译器时抱怨:
Main.hs:4:7:
Could not find module `Data.ByteString.Lex.Lazy.Double':
locations searched:
Data/ByteString/Lex/Lazy/Double.hs
Data/ByteString/Lex/Lazy/Double.lhs
我的bytestring包版本是0.9.1.5.
那么,我做错了什么?或者可能有更好的解决方案?谢谢.
更新:好的,似乎readDouble是在默认情况下未安装的包bytestring-lexer中.还有其他想法吗?
另一种解决方案:安装bytestring-lexing包,并使用readDouble,我为你优化了.
cabal install bytestring-lexing
Run Code Online (Sandbox Code Playgroud)
该包为浮点文字提供了优化的解析函数:
readDouble :: ByteString -> Maybe (Double, ByteString)
Run Code Online (Sandbox Code Playgroud)
我唯一一次在关键路径上遇到解析双精度数时,我使用了这个:
{-# LANGUAGE ForeignFunctionInterface #-}
import qualified Data.ByteString.Char8 as B
import Foreign.C.Types
import Foreign.C.String
import System.IO.Unsafe
foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO CDouble
unsafeReadDouble = unsafePerformIO . flip B.useAsCString c_atof
Run Code Online (Sandbox Code Playgroud)
readDouble不过,当时还没有任何看起来像in bytestring 的东西。如果现在成为标准,这可能是一个更好的解决方案。