1. Append or concatenate to each element in sequence
- Posted by Terry Constant <EUforum at terryconstant.com> Aug 20, 2004
- 498 views
Is there a better/faster way to concatenate or append to each element in a sequence than by using a loop, something like illustrated below?
sequence s1 global function seqConcat(sequence srcSeq, object toAppend) sequence dstSeq, curElement dstSeq = {} for i = 1 to length(srcSeq) do curElement = srcSeq[i] & toAppend dstSeq = append(dstSeq, curElement) end for return dstSeq end function --seqConcat() s1 = seqConcat({"abc","def","ghi"}, '\n')
My main concern is speed since I will be doing such operations on some large sequences. Terry Constant
2. Re: Append or concatenate to each element in sequence
- Posted by Tommy Carlier <tommy.carlier at telenet.be> Aug 20, 2004
- 492 views
Terry Constant wrote: > > Is there a better/faster way to concatenate or append to each element > in a sequence than by using a loop, something like illustrated below? > > > }}} <eucode> > sequence s1 > > global function seqConcat(sequence srcSeq, object toAppend) > sequence dstSeq, curElement > > dstSeq = {} > for i = 1 to length(srcSeq) do > curElement = srcSeq[i] & toAppend > dstSeq = append(dstSeq, curElement) > end for > return dstSeq > end function --seqConcat() > > s1 = seqConcat({"abc","def","ghi"}, '\n') > </eucode> {{{ You don't need to create a new sequence inside the function: you can just use srcSeq, like this:
sequence s1 global function seqConcat(sequence srcSeq, object toAppend) for i = 1 to length(srcSeq) do srcSeq &= toAppend end for return srcSeq end function --seqConcat() s1 = seqConcat({"abc","def","ghi"}, '\n')
-- tommy online: http://users.telenet.be/tommycarlier Euphoria Message Board: http://uboard.proboards32.com
3. Re: Append or concatenate to each element in sequence
- Posted by Tommy Carlier <tommy.carlier at telenet.be> Aug 20, 2004
- 469 views
Sorry, my previous post had a bug in it: instead of srcSeq &= toAppend, it should be srcSeq[i] &= toAppend.
sequence s1 global function seqConcat(sequence srcSeq, object toAppend) for i = 1 to length(srcSeq) do srcSeq[i] &= toAppend end for return srcSeq end function --seqConcat() s1 = seqConcat({"abc","def","ghi"}, '\n')
-- tommy online: http://users.telenet.be/tommycarlier Euphoria Message Board: http://uboard.proboards32.com
4. Re: Append or concatenate to each element in sequence
- Posted by "Christian Cuvier" <Christian.CUVIER at agriculture.gouv.fr> Aug 20, 2004
- 472 views
> Subject: Append or concatenate to each element in sequence > > > posted by: Terry Constant <EUforum at terryconstant.com> > > Is there a better/faster way to concatenate or append to each element > in a sequence than by using a loop, something like illustrated below? > > > }}} <eucode> > sequence s1 > > global function seqConcat(sequence srcSeq, object toAppend) > sequence dstSeq, curElement > > dstSeq = {} > for i = 1 to length(srcSeq) do > curElement = srcSeq[i] & toAppend > dstSeq = append(dstSeq, curElement) > end for > return dstSeq > end function --seqConcat() > > s1 = seqConcat({"abc","def","ghi"}, '\n') > </eucode> {{{ > > > My main concern is speed since I will be doing such operations > on some large sequences. > > Terry Constant Since the length of dstSeq is known, don't waste time with dynamic mallocs. And since your function only knows about a local copy of srcSeq, no need ro do any alloc for a temp sequence that will get copied on return:
sequence s1 global function seqConcat(sequence srcSeq, object toAppend) for i = 1 to length(srcSeq) do srcSeq[i] &= toAppend --so is it to be appended or to concatenated? end for return srcSeq end function --seqConcat() s1 = seqConcat({"abc","def","ghi"}, '\n')
I didn't benchmark it, but there are so many reasons for this to be faster... CChris
5. Re: Append or concatenate to each element in sequence
- Posted by Terry Constant <EUforum at terryconstant.com> Aug 20, 2004
- 471 views
Carlier wrote: "You don't need to create a new sequence inside the function: you can just use srcSeq, like this:" Thanks for your response. How you suggested is how my actual routine is. I just used the curElement sequence in my illustration so that it would be a bit more readable. Again, thanks for the response. I am hoping (seem to remember reading something, but cannot find it) for a more built/faster way than this obvious way. Terry Constant
6. Re: Append or concatenate to each element in sequence
- Posted by Terry Constant <EUforum at terryconstant.com> Aug 20, 2004
- 468 views
Cuvier wrote: "--so is it to be appended or to concatenated" I have not decided yet which I will actually use. Determining now (benchmarking and checking usability and so forth) the best option for for the actual data with which I will be working. Thus I used the same variable names in my various test routines. Unless someone chimes in with something really new, it will be concatenation (toConcat) with the faster routines (not exactly like the one I posted which was for more for readability) like you, Carlier, and I all agree on. I was actually hoping for a built-in method that I could not find and am now concluding does not exist. Terry Constant
7. Re: Append or concatenate to each element in sequence
- Posted by Terry Constant <EUforum at terryconstant.com> Aug 20, 2004
- 465 views
Christian, I forgot to say, that your routine initially looks like it is the fastest and I am going to use it. Thanks again for the help. Terry Constant
8. Re: Append or concatenate to each element in sequence
- Posted by Terry Constant <EUforum at terryconstant.com> Aug 21, 2004
- 500 views
- Last edited Aug 22, 2004
I did more analysis on my data. I will need to use both concatenation and appending. I did some benchmark testing. The two fastest routines are shown below. An interesting result is that seqAppend() is a tiny bit faster than seqConcat(). For single character appends/concats they produce the same results. For string appends/concat I will have to select which one to use. A note: my testing was only for short sequences like the one in the code below. I am posting this just FYI.
s0 = {"abc","def","ghi","jkl"} function seqConcat(sequence srcSeq, object toConcat) for i = 1 to length(srcSeq) do srcSeq[i] &= toConcat end for return srcSeq end function --seqConcat() function seqAppend(sequence srcSeq, object toAppend) for i = 1 to length(srcSeq) do srcSeq[i] = append(srcSeq[i], toAppend) end for return srcSeq end function --seqConcat() s4 = seqAppend(s0, '\n') s1 = seqConcat(s0, '\n')
Terry Constant