So, I have the following Regex which I use for syntax highlighting:
static Regex cKeyWords = new Regex("(\t|\r\n|\\s|\\(|\\)|^)(auto|break|c(ase|har|onst|ontinue)|d(efaut|ouble)|e(lse|num|xtern)|f(loat|or)|goto|i(f|nt)" +
"|long|re(gister|turn)|s(hort|igned|izeof|tatic|truct|witch)|typedef|u(nion|nsigned)|v(oid|olatile)|while)(?=\t|\r\n|\\s|\\(|\\)|{|}|$)", RegexOptions.Compiled);
It does what I want, but when it comes to large files with about 200,000 characters, it takes a little more than 6 seconds.
If there a way to improve performance?
EDIT: After taking a good look at all the comments/answers/tips, I now have this:
static Regex cKeyWords = new Regex(@"\b(?:
s(?:hort|i(?:gned|zeof)|t(?:atic|ruct)|witch) | c(?:ase|har|o(?:nst|ntinue)) |
e(?:lse|num|xtern) | i(?:f|nt) | f(?:loat|or) | d(?:efault|ouble) | un(?:ion|signed) |
re(?:gister|turn) | vo(?:id|latile) | while | break | long | typedef | auto | goto
)\b",
RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
This one can handle the 200,000 characters long text in about 5.5 seconds. It's better. However, I will keep doing some tests to see if I can further reduce time.
()instead of(?:)but that's a tiny concern). You should rather look into why you're trying to highlight 200kb in one run. No text editor does that. Code editors usually maintain a scope at the start of a line and highlight incrementally. See AvalonEdit's docs.(?>...)) to avoid unnecessary backtracking.default(efaut)