PC Review


Reply
Thread Tools Rate Thread

cdate is slow... any alternatives?

 
 
=?Utf-8?B?RXJpYyBGbGVldA==?=
Guest
Posts: n/a
 
      25th Feb 2004
In implementing the IComparer method of a listview, I found sorts by date are incredibly slow. I used a second dummy project to find out what the issue was. Code as follows simply converts two strings to two dates, and if they are not dates, uses 1/1/1900. If str1, and str2 are both valid dates, this loop runs in milliseconds. If one is not a date, the loop funds in 8 seconds. If both are not dates, then the loop runs in 16 seconds!!
Can anyone suggest a way to speed this up? I understand that type conversion can be intensive, but this is ridiculous in my opinion..

[code snippet
On Error Resume Next
For intCounter = 1 To 100
dte1 = #1/1/1900
dte2 = #1/1/1900
dte1 = CDate(str1
dte2 = CDate(str2
[Date].Compare(dte1, dte2
Nex
[end code snippet]
 
Reply With Quote
 
 
 
 
Chris Morse
Guest
Posts: n/a
 
      25th Feb 2004
On Wed, 25 Feb 2004 07:31:08 -0800, "Eric Fleet"
<(E-Mail Removed)> wrote:

>In implementing the IComparer method of a listview, I found sorts by date are incredibly slow. I used a second dummy project to find out what the issue was. Code as follows simply converts two strings to two dates, and if they are not dates, uses 1/1/1900. If str1, and str2 are both valid dates, this loop runs in milliseconds. If one is not a date, the loop funds in 8 seconds. If both are not dates, then the loop runs in 16 seconds!!!
>Can anyone suggest a way to speed this up? I understand that type conversion can be intensive, but this is ridiculous in my opinion...
>
>[code snippet]
> On Error Resume Next
> For intCounter = 1 To 1000
> dte1 = #1/1/1900#
> dte2 = #1/1/1900#
> dte1 = CDate(str1)
> dte2 = CDate(str2)
> [Date].Compare(dte1, dte2)
> Next
>[end code snippet]


Have you tried using the DateTime class?
Try using DateTime.Parse() to parse the date..

What I ended up doing was using the date format "YYYY-MM-DD HH:MM:SS",
which is how SQL server likes to format dates. The nice thing about
it is that it will sort correctly if you use a regular string
comparison.

// CHRIS

 
Reply With Quote
 
=?Utf-8?B?RXJpYyBGbGVldA==?=
Guest
Posts: n/a
 
      25th Feb 2004
I played around a bit, and found this 'solution'. I search the string for a "/" character before trying to cdate it. It gets rid of nearly all of the non-dates before I use cdate to convert. In my mind, the fact that this works says that microsoft's function is less than optimal. I would think it would kick out anything that is obviously not a date very quickly. For those curious, here is the new code snippet

For intCounter = 1 To 100000
On Error Resume Nex
dte1 = #1/1/1900
dte2 = #1/1/1900

If InStr(str1, "/", CompareMethod.Text) > 1 The
dte1 = CDate(str1
End I

If InStr(str2, "/", CompareMethod.Text) > 1 The
dte2 = CDate(str2
End I

[Date].Compare(dte1, dte2
Nex

Note that I increased the loop by a factor of 1,000, and it now takes 1.1 seconds regardless of whether str1 is not a date, str2 is not a date, or both. Without the 'pre-check', this would have about 4 hours and 15 minutes :

Eric
 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      25th Feb 2004
* "=?Utf-8?B?RXJpYyBGbGVldA==?=" <(E-Mail Removed)> scripsit:
> In implementing the IComparer method of a listview, I found sorts by
> date are incredibly slow.


Did you try 'DateTime.Parse'/'DateTime.ParseExcact'?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet/>
 
Reply With Quote
 
=?Utf-8?B?RXJpYyBGbGVldA==?=
Guest
Posts: n/a
 
      25th Feb 2004
Thanks to you and Chris for your help. I was not aware of these functions... wasn't in the "See Also" for cdate. This is considerably faster than cdate, most likely due to its more specialized nature. It also avoids my weird code :

Thanks
Eric
 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      25th Feb 2004
* "=?Utf-8?B?RXJpYyBGbGVldA==?=" <(E-Mail Removed)> scripsit:
> Thanks to you and Chris for your help. I was not aware of these
> functions... wasn't in the "See Also" for cdate.


Always have a look at the datatype's methods!

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet/>
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of CDate Nigel Microsoft Access External Data 2 12th Aug 2009 04:23 PM
cdate cj2 Microsoft VB .NET 6 25th Feb 2009 04:06 PM
Alternatives to Vistas cryptic and ridiculously slow disk defrag ngalbrai Windows Vista 4 5th Jan 2008 11:28 AM
'Not In' Query Slow - What are my alternatives? User 2084 Microsoft Access Queries 2 25th Apr 2005 08:54 AM
Re: cdate Teemu Keiski Microsoft ASP .NET 0 3rd Jul 2004 04:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:14 PM.