Boost logo

Boost Users :

From: Mauricio Gomes (mg_at_[hidden])
Date: 2005-02-11 15:33:04


For me this kind of thing belongs to compiler optimization.
In Java javac does that by using StringBuffer instead of String for
these concatenations.

Does anybody knows if the most used C++ compilers (gcc, borland, ms,
intel, etc) do such optimization ?

Thanks,
Mauricio Gomes
Pensar Digital
phone: 55-11-4121-6287
mobile: 55-11-8319-9610
http://pensardigital.com

On Feb 11, 2005, at 4:52 PM, Martin Dluhoš wrote:

> Hi,
> just an idea. What about:
>
> s1 << fastcat() + a + b + c;
>
> where:
>
> class fastcat
> {
> vector<std::string *> list;
> size_t len = 0;
>
> friend concat operator+ (const concat &, const std::string &s)
> {
> list.push_back(&s);
> len += s.size();
> }
>
> operator std::string()
> {
> std::string s;
>
> s.reserve (len);
>
> for (int I = 0; I <list.size(); i++) s+=list[i];
>
> return s;
> }
>
> I didn't test it.
>
> Martin
>
> -----Original Message-----
> From: boost-users-bounces_at_[hidden]
> [mailto:boost-users-bounces_at_[hidden]] On Behalf Of t. scott
> urban
> Sent: Friday, February 11, 2005 6:06 PM
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] Concatenation of many std::string
>
> On Fri, 2005-02-11 at 11:22 +0100, Chateauneu Remi wrote:
>>> On Thu, 2005-02-10 at 18:48 +0100, Chateauneu Remi wrote:
>>>> std::string a = my_special_object() + b + c + d + e ;
>>>>
>>> std::string s1;
>>> s1 << concat () + a + b + c;
>>> std::string s2 = concat () + a + b + c;
>>>
>>> If you want to see it, let me know.
>> Yes, thanks ! If you wish, I can send mine (330 lines, 11 k-bytes).
>> Could we take the best of both, and maybe put it in the string algo
>> lib ?
>
> Attached. The way I did it is pretty short - 50 lines or so of
> implementation, but like I said it relies on lifetimes of temporaries
> and
> order of operations which I think are guaranteed, but not certain.
> Also, a quick stress test doesn't show any improvement in speed over
> the naive
> method, but that would depend greatly on the lifetime of the
> destination
> string, size and number of strings you're concatenating, the behavior
> of your
> implementation (e.g. mine penalizes multi-threaded std::string usage
> heavily),
> etc.
>
> Perhaps you have a way to solve it in a more effective manner.
>
> In case I haven't put enough caveats on this, this is just toy code, I
> haven't
> and wouldn't put this in production code without more testing,
> profiling, etc.
>
>
> Regards
>
>
> --
> t. scott urban <scottu_at_[hidden]>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net