File: cp.hs

package info (click to toggle)
haskell-bytestring-mmap 0.2.2-13
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 116 kB
  • sloc: haskell: 230; ansic: 11; sh: 7; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 795 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
-- A non-copying cp based on mmap.

import System.IO.Posix.MMap
import qualified Data.ByteString as S

import Text.Printf
import Control.Exception
import System.CPUTime
import System.Cmd
import System.Directory

import System.Environment

time :: IO t -> IO t
time a = do
    start <- getCPUTime
    v <- a
    v `seq` return ()
    end   <- getCPUTime
    let diff = (fromIntegral (end - start)) / (10^12)
    printf "Computation time: %0.3f sec\n" (diff :: Double)
    return v

main = do
    [f] <- getArgs

    putStrLn "mmap copy"
    time $ S.writeFile "file-1" =<< unsafeMMapFile   f
    putChar '\n'

    putStrLn "lazy copy"
    time $ S.writeFile "file-2" =<< S.readFile f
    putChar '\n'

    system $ "diff " ++ "file-1 " ++ "file-2"
    removeFile "file-1"
    removeFile "file-2"