Skip to content

Instantly share code, notes, and snippets.

@rblaze
Created March 28, 2012 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rblaze/2225366 to your computer and use it in GitHub Desktop.
Save rblaze/2225366 to your computer and use it in GitHub Desktop.
С созданием таблицы как new
4,068,203,620 bytes allocated in the heap
464,278,756 bytes copied during GC
196,619,868 bytes maximum residency (11 sample(s))
8,628,244 bytes maximum slop
323 MB total memory in use (0 MB lost due to fragmentation)
Generation 0: 5565 collections, 0 parallel, 34.72s, 34.86s elapsed
Generation 1: 11 collections, 0 parallel, 1.16s, 1.27s elapsed
INIT time 0.00s ( 0.00s elapsed)
MUT time 10.96s ( 11.06s elapsed)
GC time 35.88s ( 36.12s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 46.84s ( 47.19s elapsed)
%GC time 76.6% (76.6% elapsed)
Alloc rate 371,028,334 bytes per MUT second
Productivity 23.4% of total user, 23.2% of total elapsed
С созданием как newSized 0xffffff
3,835,164,148 bytes allocated in the heap
179,941,588 bytes copied during GC
302,386,284 bytes maximum residency (2 sample(s))
3,025,160 bytes maximum slop
379 MB total memory in use (0 MB lost due to fragmentation)
Generation 0: 4930 collections, 0 parallel, 19.73s, 19.86s elapsed
Generation 1: 2 collections, 0 parallel, 0.31s, 0.31s elapsed
INIT time 0.00s ( 0.00s elapsed)
MUT time 7.84s ( 7.97s elapsed)
GC time 20.05s ( 20.18s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 27.88s ( 28.15s elapsed)
%GC time 71.9% (71.7% elapsed)
Alloc rate 489,401,016 bytes per MUT second
Productivity 28.1% of total user, 27.8% of total elapsed
module Main where
import Codec.Digest.SHA
import Control.Monad
import Data.Bits
import Data.ByteString as BS(ByteString, pack, unpack)
import qualified Data.HashTable.IO as H
import Data.Int
import Data.Maybe
import Data.Word
import Data.Vector.Storable as V(Vector, last)
import Data.Vector.Storable.ByteString
mkWord :: ByteString -> Word64
mkWord str = (V.last vec) .&. 0xFFFFFFFFFFF --0x3FFFFFFFFFFFF
where
vec :: Vector Word64
vec = byteStringToVector str
mkByteString :: Integer -> ByteString
mkByteString i = pack (mksplit i)
where
mksplit :: Integer -> [Word8]
mksplit 0 = []
mksplit n = fromIntegral r : mksplit q
where
(q, r) = quotRem n 256
hashWord :: Word64 -> Int32
hashWord w = if testBit w 40 then v else negate v
where
v :: Int32
v = fromIntegral (w .&. 0x7FFFFFFF)
type HashTable k v = H.CuckooHashTable k v
mkht :: IO (HashTable Word64 Integer)
mkht = do
ht <- H.newSized 0xffffff
return ht
main::IO()
main = do
ht <- mkht
forM_ [1..] $ \i -> do
let str = mkByteString i
let hv = mkWord (hash SHA256 str)
found <- liftM isJust (H.lookup ht hv)
when found $ do
iold <- liftM fromJust (H.lookup ht hv)
error (show (unpack $ mkByteString i) ++ " " ++ show (unpack $ mkByteString iold))
H.insert ht hv i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment