c# - Syntax Highlighting performance issue -
i have richtextbox , once user loads file, program proceeds scan entire file in order change color of words. here code:
static regex ckeywords = new regex(@"\b(?=[a-gilr-w])(?: s(?:hort|i(?:gned|zeof)|t(?:atic|ruct)|witch) | c(?:ase|har|on(?:st|tinue)) | e(?:lse|num|xtern) | i(?:f|nt) | f(?:loat|or) | d(?:o|efault|ouble) | un(?:ion|signed) | re(?:gister|turn) | vo(?:id|latile) | while | break | long | typedef | auto | goto )\b", regexoptions.compiled | regexoptions.ignorepatternwhitespace); ... programtextbox.enabled = false; int selectstart = this.programtextbox.selectionstart; programtextbox.suspendlayout(); matchcollection matches = ckeywords.matches(programtextbox.text); foreach (match match in matches) { if (match.index == 0) programtextbox.select(match.index, match.length/* - 1*/); else programtextbox.select(match.index + 1, match.length - 1); programtextbox.selectioncolor = color.blue; } programtextbox.select(selectstart, 0); programtextbox.selectioncolor = color.black; programtextbox.enabled = true; programtextbox.resumelayout();
problem: code takes 5 , half seconds scan , change colors of keywords in file has 200,000 characters.
i've been told before shouldn't use regex kind of stuff, after doing several tests figured out the: matchcollection matches = ckeywords.matches(programtextbox.text);
only takes 0.1s , removing the
programtextbox.selectioncolor = color.blue;
reduces total execution time of code 5.5s 0.3s
how? why? , importantly: can do?
have tried this?
this blocks painting , seems block properly. had small test file put through it, seemed work pretty well.
Comments
Post a Comment