I was checking boost spirit karma generator performance when I was somewhat surprised by performance degradation when using policy for real numbers.
Live on Coliru
The code was taken from boost spirit and a couple of test functions were added. Coliru example replaces the timer used. Note that Coliru aborts long running progs so it may not end all tests.
As one can see policy use degrades performance 2-3 (x10 on coliru) times. Is it expected behavior?
My figures:
sprintf: 0.367
iostreams: 0.818
format: 1.036
karma: 0.087
(string): 0.152
karma (string) with policy: 0.396
karma (rule): 0.12
karma (direct): 0.083
karma (direct) string: 0.089
karma (direct) string with policy: 0.278
Built with x64 VC14
It's not a regression if you compare apples and pears. In this case, twice.
First apple/pear pair
Here
fixedis apples, andscientificis pears.Not only is the resulting output clearly different, but also arriving it the result requires different steps.
Importantly
scientificinvolves taking thelog10of the input values so as to establish the magnitude of the number in base-10-digits before the decimal point:By default, real_policies call a "cheap" verdict:
So you can watch the difference evaporate if you choose a format that would switch to scientific anyways:
123456.123456instead of12345.12345...:As you can see, the custom policy is (predictable) much faster
As Interactive Link:
Second apples/pears pair
This arises where you pinned the precision to 15 digits.
By using a separate head-to-head benchmark of two policies that actually does the precision in addition: http://paste.ubuntu.com/13087371/ you can see that this is what more than loses the benefit of fixing the format to
scientificseen above:Or in graph: Interactive Link