It might be easy to overlook that the new “+=” operator can be used to append strings to an existing string in addition to incrementing a numerical value. However, using the “+=” method provides a significant performance benefit, as I will illustrate with this posting.
The legacy method for appending a string to an existing string has been to code something like this:
$Text = $Text + "some new text...";
The new way to do this with Tinderbox 9.1 and later is as follows:
$Text += "some new text...";
However, while the two approaches shown above achieve the same result, the new += version seems to be much faster. This makes sense, because the new approach does not require Tinderbox to completely replace the contents of $Text (or any other attribute or variable) with the same text plus whatever is being appended. Instead, it merely needs to append the new text.
To test this out, I used the Logging tools that I provided as Toolbox v1b to compare the two approaches to appending new text. More information about the logging tools is available in the post Logging for fun and profit.
The logging tools employ a single function to perform all logging operations. When called, this function will append the provided text as a new log record to a specified log book note. For backwards compatibility with older versions of Tinderbox, the provided Toolbox v1b document uses the “$Text=$Text+string” approach, but in my own Tinderbox documents, I’m using the “+=” approach.
I have just finished a rather complex custom explode project where I use Action code to process a somewhat messy set of imported files into over 300 Tinderbox Prototypes that will later be used in further import operations. Since I use the logging tools for debugging, I was running this custom explode code with several log messages being recorded for each imported file. The net effect was that about 2,000 log records were being recorded into two different logbooks for one complete run of the custom explode. Realizing that this provided a real-world opportunity to test the relative performance of the two approaches to appending strings, I ran this code twice, first with the legacy approach, and subsequently with the “+=” approach. Each test case was started with fresh log books.
In the first case, the execution took 31 minutes. In the second test case, it took less than 5 minutes. This was with just one line of code being changed. Furthermore, since timestamps were being included with the log records, I could see direct evidence that things slowed down the larger the logbooks got. In the first case with the legacy approach, merely watching the progress it was clear that things slowed down toward the end of the execution.
So, if you do a lot of appending of strings, and have found performance suffering, you might want to try modifying your code to use the new “+=” operator.