[Haskell-beginners] Optimise mininal date conversion function?

Patrick LeBoutillier patrick.leboutillier at gmail.com
Tue Jan 11 15:21:56 CET 2011


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?

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


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
-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada



More information about the Beginners mailing list