Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c++ Subject: Re: rational numbers Date: Mon, 27 Sep 2021 21:27:09 +1300 Lines: 47 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net JN6VSWeajFHptA8/JPKubwyhAAFLlKobraqJFI6Zf2W+AixzNH Cancel-Lock: sha1:LQLY0rusQ8A4+hK0kwoircdxHis= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 In-Reply-To: Content-Language: en-US Xref: csiph.com comp.lang.c++:81618 On 27/09/2021 21:19, Paavo Helde wrote: > 27.09.2021 08:36 Juha Nieminen kirjutas: >> Paavo Helde wrote: >>> Adding a stream adaptor for a class having only a to_string() is trivial: >>> >>> std::ostream& operator<<(std::ostream& os, const A& a) { >>> os << a.to_string(); >>> return os; >>> } >> >> While you are at it, why not just output the contents of that A object >> directly, rather than making it construct a string? > > > Because of speed. I just showed elsethread that serializing a large > object into an in-memory string can be up to 10x faster than writing it > into a std::ostream piece-by-piece. > > Also, because of better modularity and easier usage. A string is > basically just a raw memory buffer which is easy to transport and use. > Streams are more complicated. > > Say, I want to write my large data structure into a file in AWS cloud. > AmazonStreamingWebServiceRequest::SetBody() takes a pointer to an input > stream and reads data from it later when I call S3Object::PutObject(). > > Say, for my large data structure I have proper streaming support which > writes the data into an std::ostream. So now what? How do I connect this > output stream to an input stream used by the AWS library so that they > would "flow together"? Sure it can be done, but seems not so easy. > Threads or coroutines come to mind. > > The easiest way is to dump the data into a temporary file, then let the > AWS library to read it. We do not need a file on disk, so this ought to > be an in-memory file. And guess what is the fastest way to create an > in-memory file? Answer: serializing the data into a raw memory buffer > such as std::string. IOW the dreaded to_string() method. You can string it into an in memory straeam buffer. I can't see how adding to a string can be any faster and you have to convert each field to a string representation which is what streams do for you. -- Ian.