Another potential optimization would be to use a timer instead of firing off on every keypress. A period of 1 second would feel "instant" enough I'd think, and it could reduce the CPU usage significantly.
On each keydown I'd have something like if tmrWordCount.mode<>1 then tmrWordCount.mode=1 end And then tmrWordCount's action event would call the word count procedures. This way, you limit the call to a minimum of getting called every second. Note that when you change a timer's mode, it's internal counter starts over, so you need that "if - then" block, otherwise it wouldn't update until the user hadn't been typing for 1 second. That would probably reduce CPU usage even more, though it might make the live word count feel sluggish. Hope that helps, ~ Tomis --- Marcel <[EMAIL PROTECTED]> wrote: > Hello everybody! > > Maybe somebody finds this trick useful: > (... and maybe someone finds a trap, I haven't seen?) > > I make heavy use of styled EditFields in my project. After playing > around, I found out that the call of the method... > > aNumberOfParagraphs = anEditField.StyledText.ParagraphCount() > > ... is very time consuming. About a second (!) for 30 paragraphs on > a 1,25 GHz G4. > > Because I have to test the number of paragraphs after each keystroke, > you can imagine that this timeframe isn't acceptable at all. > When I count the EndOfLines in my text with ... > > aNumberOfParagraphs = CountFields(anEditField.Text, EndOfLine) > > ... I get (almost) the right value. I just recognized that "empty" > lines in the text don't generate a paragraph in a styled EditField. > So I removed them before counting ... > > aNumberOfParagraphs = CountFields(ReplaceAll(aField.Text, > (EndOfLine + EndOfLine), EndOfLine), EndOfLine) > > This was almost working, I just saw that multiple empty lines in a > row were not eliminated right. Using a grep sequence finally solved > it... > > aRegEx.SearchPattern = "^[\f\n\r]+" > aRegEx.ReplacementPattern = "" > aTextToCount = aField.Text > do > aRegExMatch=aRegEx.Search(aTextToCount) > if (aRegExMatch <> Nil) then > aTextToCount = aRegEx.Replace(aTextToCount) > end > loop until (aRegExMatch = Nil) > aParaLastIndex = CountFields(aTextToCount, EndOfLine) > > In my tests the above CountFields() call always returns the same > values as the ParagraphCount() call. It needs always a processing > time of about 1 tick (1/60 of a second), with up to 70 paragraphs and > 16,000 chars tested. > > Maybe you want to replace the ParagraphCount() calls in your code and > try it. I would be very interested in feedback. Can I trust this > "work-around"? Does someone has any objections arising, I do not see? > > Thanks and best wishes, > Marcel > > _______________________________________________ > Unsubscribe or switch delivery mode: > <http://www.realsoftware.com/support/listmanager/> > > Search the archives of this list here: > <http://support.realsoftware.com/listarchives/lists.html> > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives of this list here: <http://support.realsoftware.com/listarchives/lists.html>
