DateTime.Parse, slower you than might think (or I might think at least)...

So I took the time to write some code today to process a flat file.  The flat file didn't have too many rows, only about 1800, so I figured the routines would run farely fast.  They did, at about 65 milliseconds on average parsing a CSV file with 8 or so fields into a strongly typed data structure, and then placing said structures into an array list.

I did some neat things with the structures, like making them sortable on any field in any direction, etc...  But that wasn't the part that was hanging me up.  I thought the darn code should be running a bit faster.  Since the code is a basic string.Split, a couple of int, bool parses, and a datetime parse along with the object construction I figured the problem had to be in the parse routines.  Now, I know that integer parses can't be that darn slow, so I left those in.  I did, however, take out the datetime.Parse.  to my surprise the speed came down to 23 milliseconds or less.  I basically got a 200% performance increase by removing a single datetime.Parse line.  I realize that parsing dates must be a fairly hard task, but I would think if the exact structure of the date is know, the parser could be very fast and very determinate.

Well, just figured I'd point out to beware of any DateTime fields since they tend to be slow in the parsing area.  Maybe someone from the BCL team can comment on how to make better use of it.  I did try taking some examples from the DateTime best practices guide and pass in a custom ParseExact format.  It did get a little bit faster, and actually dropped me down to an average run time of 41 ms, so I guess I have to say it got a LOT faster than it was previously.  I'm still thinking a custom formatter class run over top of the string might yield an even faster result because of the complex code used by the DateTime class in order to switch to specialized methods based on my custom parsing format.  After all, DateTime is really a swiss army knife when all I really need is a sharp stick to poke someone in the eye with.

Published Sunday, January 25, 2004 1:31 AM by Justin Rogers

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)