
On Tuesday 11 January 2011 15:21:56, Patrick LeBoutillier wrote:
Hi,
I have this crude function that converts a date in YYYY/MM/DD format to an Int, but it's slow. Does any one have a clue on how to optimize it?
Try Data.ByteString[.Lazy].Char8.readInt
date2int :: B.ByteString -> Int date2int b = y*12*31 + (m-1)*31 + (d-1) where y = read . B.unpack . B.take 4 $ b m = read . B.unpack . B.take 2 . B.drop 5 $ b d = read . B.unpack . B.drop 8 $ b
date2int b0 = fromMaybe 0 $ do (y,b1) <- readInt b0 (m,b2) <- readInt $ unsafeTail b1 (d,_) <- readInt $ unsafeTail b2 return $ y*12*31 + (m-1)*31 + (d-1) (perhaps use bang-patterns, that may speed it up a bit [or not]).
Here is the original C++ function that was ported:
static unsigned date2int (const char *date){ return atoi(date)*12*31+(atoi(date+5)-1)*31+atoi(date+8)-1; }
Thanks,
Patrick