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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c32f6f0b23106020 X-Google-Attributes: gid103376,public From: dvdeug@x8b4e53cd.dhcp.okstate.edu (David Starner) Subject: Re: Large strings in ADA Date: 2000/04/16 Message-ID: <8ddcko$a2g1@news.cis.okstate.edu>#1/1 X-Deja-AN: 611998670 References: <38FA3003.A38D7B51@xpress.se> Organization: Oklahoma State University User-Agent: slrn/0.9.6.2 (Linux) Reply-To: dstarner98@aasaa.ofe.org Newsgroups: comp.lang.ada Date: 2000-04-16T00:00:00+00:00 List-Id: On Sun, 16 Apr 2000 23:26:27 +0200, Johan Groth wrote: >Hello, >I'm trying to write a program that converts a couple of types to strings >and are concatenated into one large string that is at the moment a >unbounded_string but when the string gets about 50KB big it takes longer >and longer to append to it. >The code looks like below. I need to concatenate about 2.5MB of data. >What is the fasted way to do that in ADA? Just for comparison a similar >program in C takes about one second. Then it's not similar. C doesn't have Unbounded_Strings and 'Image attribute, so I doubt the two programs are all that similar. At the worst, why didn't/don't you transliterate that C into Ada? It won't be nice, pretty or friendly, but it will probably be as fast as the C version (with checks turned off.) This below isn't similar to any C program I can concieve of - C just won't let you write at this level. >Can anyone help me? > >TIA, >Johan > >procedure Main is > type String32 is > record > Info : String(1..32) := (others => ' '); > Len : Natural range 0 .. 32 := 0; > end record; > > Str : String32; > No : Basic_Integer; > Flt : Basic_Float; > Msg : Unbounded_String := Null_Unbounded_String; > Last : Natural; > > procedure Append_To_Msg (Str : in String; > Max_Len : in Natural; > Message : in out Asu.Unbounded_String; > Last : in out Natural) is > begin > Append(Message, " "); > Append(Message, Natural'Image(Max_Len)); > Append(Message, " "); > Append(Message, Str); > Last := Asu.Length (Message); It probably really hurts to count the length of the string over and over. In fact, this is where I would bet a lot of time is going. Why don't you try gprof (for GNAT) or a profiler for whatever Ada compiler you're using and see where the times being spent? > end Add_To_Message; > >begin > Str.info(1..6) := "hejsan"; > Str.Len := 6; > No := 10; > Flt := 5.5; > for I in 1 .. 1600 loop > Append_To_Msg(Str.Info, Basic_Natural(Str.Len), Msg, Last); Why don't you pass Basic_Natural (Str.Len)'Image in? If you do so, most compilers will do the conversion only once, instead of 1600 times. Also, you don't mention what compiler's you're comparing, running on what systems, with what switchs, which can make a big difference. -- David Starner - dstarner98@aasaa.ofe.org Only a nerd would worry about wrong parentheses with square brackets. But that's what mathematicians are. -- Dr. Burchard, math professor at OSU