我在“真实世界 Haskell”(第7章,第189页)中读到了IO缓冲,并尝试测试不同的缓冲大小如何影响性能. import System.IOimport Data.Time.Clockimport Data.Char(toUpper)main :: IO ()main = do hInp - openFile "bigFile.t
import System.IO import Data.Time.Clock import Data.Char(toUpper) main :: IO () main = do hInp <- openFile "bigFile.txt" ReadMode let bufferSize = truncate $2**10 hSetBuffering hInp (BlockBuffering (Just bufferSize)) bufferMode <- hGetBuffering hInp putStrLn $"Current buffering mode: " ++ (show bufferMode) startTime <- getCurrentTime inp <- hGetContents hInp writeFile "processed.txt" (map toUpper inp) hClose hInp finishTime <- getCurrentTime print $diffUTCTime finishTime startTime return ()
然后我创建了一个“bigFile.txt”
-rw-rw-r-- 1 user user 96M янв. 26 09:49 bigFile.txt
并使用不同的缓冲区大小对此文件运行我的程序:
Current buffering mode: BlockBuffering (Just 32) 9.744967s Current buffering mode: BlockBuffering (Just 1024) 9.667924s Current buffering mode: BlockBuffering (Just 1048576) 9.494807s Current buffering mode: BlockBuffering (Just 1073741824) 9.792453s
但程序运行时间几乎相同.这是正常的,还是我做错了什么?
在现代操作系统上,由于1)内核执行预读,并且2)如果您最近已经读取了文件,则文件可能已经在页面缓存中,缓冲区大小可能对线性读取文件几乎没有影响. .这是一个测量缓冲对写入的影响的程序.典型的结果是:
$./mkbigfile 32 -- 12.864733s $./mkbigfile 64 -- 9.668272s $./mkbigfile 128 -- 6.993664s $./mkbigfile 512 -- 4.130989s $./mkbigfile 1024 -- 3.536652s $./mkbigfile 16384 -- 3.055403s $./mkbigfile 1000000 -- 3.004879s
资源:
{-# LANGUAGE OverloadedStrings #-} import qualified Data.ByteString as BS import Data.ByteString (ByteString) import Control.Monad import System.IO import System.Environment import Data.Time.Clock main = do (arg:_) <- getArgs let size = read arg let bs = "abcdefghijklmnopqrstuvwxyz" n = 96000000 `div` (length bs) h <- openFile "bigFile.txt" WriteMode hSetBuffering h (BlockBuffering (Just size)) startTime <- getCurrentTime replicateM_ n $hPutStrLn h bs hClose h finishTime <- getCurrentTime print $diffUTCTime finishTime startTime return ()