On Fri, 02 Sep 2011 06:17:53 -0400, Timon Gehr <timon.g...@gmx.ch> wrote:
On 09/02/2011 03:29 AM, Don wrote:
If you're concerned about speed, the writeTo method is much quicker,
since it doesn't require any heap activity at all.
allocating a new string on the heap always requires heap activity. I was
benchmarking to!string with toString and with what would probably be the
solution for writeTo, and toString was quicker.
Simple appending is slow. There are better ways to do it. For example,
use Appender.
writeTo should be faster than toString for most cases, on principal that
it can generally avoid *any* heap allocations. Ideally, to!string should
use a stack-allocated buffer and idup it to get the final string. Of
course, toString for simple cases, like printing one integer, can be
optimized in a toString method better than writeTo.
writeTo can support a variety of other use cases where it is much
quicker of course, and I consider it a very worthy addition. I just
think that the support for data types with a toString method should not
just disappear.
There are very few (if any) use cases for toString that don't involve
showing the result, for which allocating a string is typically wasted
cycles/space. toString is not a serializable form of an object, I don't
see why it should be encouraged.
-Steve