Created
August 26, 2013 08:22
-
-
Save rblaze/6339176 to your computer and use it in GitHub Desktop.
Чтение текстового файла
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls -l test.txt | |
-rw-rw-r-- 1 blaze blaze 69979380 Aug 26 12:08 test.txt | |
$ wc -l test.txt | |
8994700 test.txt | |
Strict Data.Bytestring + decodeUtf8 -- 5 секунд | |
Strict Data.Text.IO -- 7 секунд | |
Lazy Data.Bytestring + decodeUtf8 -- 10 секунд | |
Lazy Data.Text.IO -- 12 секунд |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
module Main where | |
-- Lazy добавлять и удалять по желанию | |
import qualified Data.Text.Lazy as T | |
import qualified Data.Text.Lazy.Read as T | |
import qualified Data.Text.Lazy.IO as T | |
import qualified Data.Text.Lazy.Encoding as T | |
import qualified Data.ByteString.Lazy as BS | |
import qualified Data.Map as M | |
import Data.List | |
addval :: M.Map Int [Int] -> T.Text -> M.Map Int [Int] | |
addval !g t | |
| M.member k g = M.adjust (v :) k g | |
| otherwise = M.insert k [v] g | |
where | |
[Right (k, _), Right (v, _)] = map T.decimal $ T.words t | |
main :: IO () | |
main = do | |
t <- T.readFile "test.txt" | |
-- bs <- BS.readFile "test.txt" | |
-- let t = T.decodeUtf8 bs | |
let g = foldl' addval M.empty $ T.lines t | |
_ <- g `seq` return g | |
--print g | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment