c# - How does String.Contains work? -
possible duplicate:
what algorithm .net use searching pattern in string?
i have loop in program gets line file. there check whether line contains string
if(line.contains("string")) { //do other stuff }
there on 2 million rows in file if can quicken speed 1/10th millisecond save me on 3 minutes on each run.
so... line 1000 chars long, quicker short or long string, or not make difference?
line.contains("abcdefghijklmnopqrstuvwxyz");
or
line.contains("abcdefg")
thank in advance.
string.contains() follows torturous route through system.globalization.compareinfo clr , nls support sub-system thoroughly got lost. contains highly optimized code impressive perf. way faster through pinvoking standard crt function wcsstr, available in msvcrt.dll
[dllimport("msvcrt.dll", charset = charset.unicode)] private static extern intptr wcsstr(string tosearch, string tofind);
it returns intptr.zero if string wasn't found. made measurements, using string.indexof() instead of contains() test various string comparison options. times in nanoseconds, searching string of 7 characters in string of 60 characters. string not present worst case measured. lowest time out of sample of 20 used:
stringcomparison.ordinal (same contains) : 245 nanoseconds stringcomparison.ordinalignorecase : 327 stringcomparison.invariantculture : 251 stringcomparison.invariantcultureignorecase : 327 stringcomparison.currentculture : 275 stringcomparison.currentcultureignorecase : 340 wcsstr : 213
very impressive numbers , on par you'd expect these functions require. wcsstr() function same kind of ordinal comparison string.compare(). 13% faster, statistically insignificant improvement given real perf not approach these measurements due effects of cpu cache locality. can conclude you're going fast can expect. whether slight improvement wcsstr worth you.
Comments
Post a Comment