tag:blogger.com,1999:blog-4054614406137433263.post1445915399118775903..comments2024-03-25T18:56:05.062-04:00Comments on Metadata Consulting [dot] ca: C# - Fastest way to find a number/integer in a stringmetadataconsulting (profile)http://www.blogger.com/profile/02252886574672860301noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-4054614406137433263.post-33775612905428689722020-06-30T08:42:56.645-04:002020-06-30T08:42:56.645-04:00.ToCharArray() will allocate an array of character....ToCharArray() will allocate an array of characters that's as big as the initial string. Your method will be fast(now) but it will create garbage that later needs to be GC-ed (slow later).<br />With this pattern:<br />sw.Start();<br />Console.WriteLine("aaaaaa".GetFirstIntFast());<br />sw.Stop();<br />you are also measuring Console.WriteLine.Code Samuraihttps://www.blogger.com/profile/07641319833503077523noreply@blogger.com