http://www.mredkj.com/VBQuicktakes/WordWrap.html
Printable View
It isn't the best example, but the theories behind it are probably the fastest way to do it.
Some of the bad points that I spotted:
- The textbox (and value of intMaxLineLen) should be passed as a parameters/properties, instead of being coded into the routine. [not speed related - but stops re-usablilty]
- Instead of just "frmMain.txtHidden" the code should be "frmMain.txtHidden.Text" (or the programmer should be shot! ;) )
- The hWnd should be stored in a variable at the start of the routine, to reduce the time to retrieve it.
- As it is only called from one place, the FixBuf function should be moved inline.
- It may be possible to replace txtHidden with a string variable (I havent checked the code enough, but this would make it much faster), otherwise a With block would help.
Ah thanks for the tips. I wrote that like 6+ years ago because I had to wordwrap gigabytes of text.
Jacob Roman said, he has found speed problem with With block : http://www.vbforums.com/showpost.php...67&postcount=9Quote:
Originally Posted by si_the_geek
There have been many tests on the speed of With blocks, and from what I have seen the general rules are:
If you want examples/explanations of these, search for posts on this site by Merri that discuss it, or search the web for VBSpeed and VBFibre.
- Using it for single item (eg: "With MyObject") does not yield an improvement, and for some things slows it down.
- Using it for an array (eg: "With MyObject(MyIndex)") is about the same speed.
- And using it for a child object (eg: "With MyParentObject.MyObject"), which is used in this case, is faster - as the "chain of reference" does not need to be evaluated each time.
Then the last level optimization: get rid of processing strings. Basically this means you fake an integer array with no items to point to the string data in memory. This happens by replacing the array header data in memory while the process is going on. However this gets very complex and figuring the fastest way might take a good while: you'd have to solve how to have enough space in the array/string where the end result is being processed into.
If interested to know how to let an integer array access a string data, take a look into this thread: InStrCount. The code snippet uses the technique to work very fast. The code looks for given string data inside another, counting strings within strings. In binary compare mode only though.
From the mighty optimisation thread (here) I found this thread which dicusses it:
http://www.vbforums.com/showthread.php?t=335783