Alvin
Thu Apr 22 10:21:33 CDT 2004
I don't think i found the exact thread but what i found i used which just
had the hard check and exception.
I also have a monster laptop
No extra checking: 00:00:30.4537904
(Total=65554500000)
With preliminary checking: 00:00:00.2603744
(Total=65554500000)
Your conclusions are correct.
>> Tryparse actually throws an exception on an invalid format. I'm not sure
>> that a live exception is faster than a regex compilation and execution.
i really don't know where i got that from. it's obviously wrong.
Another important point is that running this test under visual studio skews
each result by about 5% - 15% when the debugger is not attached in both
release and debug mode. Attaching the debugger provided results which took
over 10 minutes. I've concluded that timing applications by using visual
studio is unreliable at best since the skew is not a constant value for each
result.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1aef72976181fb9c98a6f8@msnews.microsoft.com...
> <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
>> > This approach is significantly faster than using regular expressions
>> > (in the tests I did a while ago, at least).
>>
>> can you point me to those test?
>
> Sure. Have a look on groups.google.com for "Checking if a string can be
> converted to Int32" (in this group).
>
>> Tryparse actually throws an exception on an invalid format. I'm not sure
>> that a live exception is faster than a regex compilation and execution.
>
> Um, I don't think TryParse *does* throw an exception - the whole point
> is that it doesn't, surely?
>
>> It would make sense that if the data was significantly faulty, or perhaps
>> generally unknown that the use of a regex would be a suitable
>> optimization
>> to avoid the run-time exceptions.
>
> No it wouldn't - not compared with a simple hard coded test and then
> using TryParse.
>
> Here are the results I got from the benchmark in the thread mentioned
> earlier:
>
> Benchmarking type StringToInt
> JustException: 00:01:15.7989936
> HardCodedCheck: 00:00:00.7010080
> DoubleTryParse: 00:00:40.8387232
> Regex: 00:00:43.0418912
> IsNumeric: 00:01:06.9062064
>
> (It does the obvious thing.)
>
> Yes, the hard-coded test is over 50x faster than the next-best, which
> is Double.TryParse.
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too