[Haskell-beginners] Optimise mininal date conversion function?
Patrick LeBoutillier
patrick.leboutillier at gmail.com
Tue Jan 11 20:17:34 CET 2011
On Tue, Jan 11, 2011 at 9:44 AM, Daniel Fischer
<daniel.is.fischer at googlemail.com> wrote:
> 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
Thanks, that's exactly what I needed...
Patrick
>
>>
>> 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
>
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
More information about the Beginners
mailing list