R
Roedy Green
I wrote some months back that I got my main app, the HTMLMACRO running
10% faster just by optimising the size of new StringBuilder.
This set me wondering how it might be fine tuned further, and perhaps
turned into a commercial tool to fine tune it for any app.
Let's pretend that for every new StringBuilder invocation I collected
statistics of every string built and how long it was.
Knowing that, how big should you make the initialisation parameter?
Right now I pretty well always set it to the longest string. But in a
few cases I made it shorter.
Setting it to the longest string conserves CPU time in an infinitely
RAM rich environment. However outside the government, you probably
also want to conserve RAM by making letting a few of the StringBuildes
grow in return for most StringBuilders being smaller.
You need some measure of tradeoff of doublings vs wasted bytes in a
given invocation. I suppose you might find that number by experiment.
A mindless algorithm might work like this:
Simulate the doublings and wasted bytes of every for every possible
StringBuilder size between min and max length and pick the op for each
instance. This would not be done while the app was running, so even a
crude (o n^2) algorithm like that would likely be acceptable.
10% faster just by optimising the size of new StringBuilder.
This set me wondering how it might be fine tuned further, and perhaps
turned into a commercial tool to fine tune it for any app.
Let's pretend that for every new StringBuilder invocation I collected
statistics of every string built and how long it was.
Knowing that, how big should you make the initialisation parameter?
Right now I pretty well always set it to the longest string. But in a
few cases I made it shorter.
Setting it to the longest string conserves CPU time in an infinitely
RAM rich environment. However outside the government, you probably
also want to conserve RAM by making letting a few of the StringBuildes
grow in return for most StringBuilders being smaller.
You need some measure of tradeoff of doublings vs wasted bytes in a
given invocation. I suppose you might find that number by experiment.
A mindless algorithm might work like this:
Simulate the doublings and wasted bytes of every for every possible
StringBuilder size between min and max length and pick the op for each
instance. This would not be done while the app was running, so even a
crude (o n^2) algorithm like that would likely be acceptable.