From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,64b29dfa2220a59f X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!newsfeed2.dallas1.level3.net!news.level3.com!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Reserve_Capacity for Unbounded_String? Date: Tue, 24 Jul 2007 22:25:12 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <1185134043.892012.217560@n2g2000hse.googlegroups.com> <1185203238.701948.307410@m37g2000prh.googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls6.std.com 1185330312 18648 192.74.137.71 (25 Jul 2007 02:25:12 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Wed, 25 Jul 2007 02:25:12 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:zzjyW3DoABbZGURka5cf9yl7LJQ= Xref: g2news2.google.com comp.lang.ada:1148 Date: 2007-07-24T22:25:12-04:00 List-Id: "Randy Brukardt" writes: > "Robert A Duff" wrote in message > news:wccr6mx5pz0.fsf@shell01.TheWorld.com... >> "Randy Brukardt" writes: > ... >> > For Janus/Ada, Unbounded_String is just a controlled wrapper around > "access >> > String"; in particular, the length is the length of the item >> > allocated. >> >> Are you saying that: >> >> for I in 1..N loop >> Append (Some_Unbounded_String, 'x'); >> >> causes a free and a "new" and a copy every time around the loop? > > Yup; I just checked the code to make sure. Moreover, I have yet to see an > real example where the performance difference would matter that much (Free > and new aren't that expensive relative to the alternatives). It's the copy I'm wondering about, not the new and free so much. To append N characters to an unb string requires quadratic copying (copying a char N**2 / 2 times). If you double the allocation every time, it's amortized linear. Yes, you waste some space that way. > If performance is a real issue, you'd have to write your own code to do the > actual operations you want; for instance, in your example > To_Unbounded_String (Some_Unbounded_String, (1..N => 'x')); would be a lot > faster ... Yes, but that's just an example. The real example is where you're reading characters one-by-one from somewhere, and you don't know how many there will be, and you want to append some of them to a string. >...on any compiler (far fewer calls to a library; even if there is no > allocation, there still will be overhead checks for bounds and the like). > The examples where an alternative implementation of Unbounded_Strings would > make a significant difference such that the application was fast enough with > it and not fast enough without it have to be extremely rare. Fair enough. Real-world measurements beat theoretical reasoning. ;-) - Bob