Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I came up with.</p> <p>I used the function offered by JB and added two tricks which I learned from the source code of bytestring-lexing (thanks, sclv!). The first one is this function:</p> <pre><code>strict = SB.concat . LB.toChunks</code></pre> <p>It transforms a lazy bytestring into non-lazy one efficiently.</p> <p>The second trick is function <code>Data.ByteString.Internal.inlinePerformIO</code> which is a more efficient variant of unsafePerformIO.</p> <p>Here's complete code that allows a pretty fast number reading:</p> <pre><code> {-# LANGUAGE ForeignFunctionInterface #-} import qualified Data.ByteString.Lazy.Char8 as LB import qualified Data.ByteString as SB import Data.ByteString.Internal (inlinePerformIO) import Foreign.C.String (CString) import Foreign.C (CDouble) import Data.Maybe (fromJust) foreign import ccall unsafe "stdlib.h atof" c_atof :: CString -> IO Double unsafeReadDouble = inlinePerformIO . flip SB.useAsCString c_atof {-# INLINE unsafeReadDouble #-} readDouble = unsafeReadDouble . SB.concat . LB.toChunks readInt = fst . fromJust . LB.readInt </code></pre> <p>And a sample program that calculates the sum of all numbers in the input: <code><pre> main = LB.getContents >>= (print . sum . map readDouble . LB.lines) </code></pre> It processes an 11Mb file (1M numbers) in about 0.5 seconds</p> <p>I also <a href="http://www.haskell.org/haskellwiki/SPOJ#Enormous_Input%3a_Solutions_to_INTEST" rel="nofollow">found</a> <a href="http://www.mail-archive.com/haskell-cafe@haskell.org/msg63973.html" rel="nofollow">several</a> <a href="http://groups.google.com/group/fa.haskell/browse_thread/thread/4133fa71ce97eb0e/fef34d1c3943bbe0#fef34d1c3943bbe0" rel="nofollow">links</a>, where a much more efficient version of <code>readInt</code> is discussed. Presumably one can build a <code>readDouble</code> based on similar ideas. But I think I'll stick with my current version for now.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload