What is the most efficient way...

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the most efficient way...

George Hovey-2
... to convert a Rexx variable containing a string of space-delimited words into a Rexx array whose elements are those words?

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

Robert L Hamilton
One way to start is: string = space(string,1,',') to get one comma between each word.  Then put string between [  and ].  Maybe.

Sorta wingin' here.

BobH

On Mon, Oct 31, 2011 at 11:43 AM, George Hovey <[hidden email]> wrote:
... to convert a Rexx variable containing a string of space-delimited words into a Rexx array whose elements are those words?

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/




_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

alansam


On 31 October 2011 11:11, Robert Hamilton <[hidden email]> wrote:
One way to start is: string = space(string,1,',') to get one comma between each word.  Then put string between [  and ].  Maybe.

Sorta wingin' here.

BobH


That would require Interpret which isn't available in NetRexx.

For fun I ran some comparisons looping on Rexx.words() and String.split() and got the following:

word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using String.split: 
0.000903 0.067337 0.003178 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using Rexx.words: 
0.265916 0.461035 0.284033 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a String array using String.split: 
0.000700 0.002405 0.000794 

So it would appear that using the String.split() method is faster than Rexx.words() by an order of magnitude.  If you can get awat with a String[] then String.split() is even more efficient.

Here's my test program:

/* NetRexx */
options replace format comments java nocrossref savelog symbols

lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." -
         "Duis et sem elit, id ullamcorper erat." -
         "Maecenas dictum suscipit neque, non pharetra velit aliquet sit amet." -
         "Cras tincidunt, orci at tempus tincidunt, ligula tellus rutrum est," -
         "hendrerit congue lectus justo vel tortor." -
         "Vivamus sit amet consequat tellus." -
         "Pellentesque sapien augue, ornare ac tempor id, eleifend id nisl." -
         "Aliquam ultricies mauris ac velit mollis ut eleifend lorem hendrerit." -
         "Class aptent taciti sociosqu ad litora torquent per conubia nostra," -
         "per inceptos himenaeos." -
         "Nulla facilisi." -
         "Morbi ultricies faucibus vulputate." -
         "Nam lobortis, nibh id accumsan porttitor, urna dolor lacinia lectus," -
         "vitae placerat risus sapien id lorem." -
         "Etiam semper magna a lorem vestibulum eu scelerisque mi convallis." -
         "Ut condimentum tristique sem, eget adipiscing nunc venenatis sit amet." -
         "Phasellus laoreet laoreet facilisis." -
         "Nullam nulla leo, imperdiet ut tristique vitae, tempor sed arcu."

loop for 5
  lipsum = lipsum lipsum
  end

startTime = long
endTime = long

Numeric digits 20

tests = 100

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumRWords = RexxArrayViaStringSplit(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumRWords.length
say 'Min, max and avg time for' tests 'iterations to create a Rexx array using String.split:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumRWords = null

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumRWords = RexxArrayViaRexxWords(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumRWords.length
say 'Min, max and avg time for' tests 'iterations to create a Rexx array using Rexx.words:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumRWords = null

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumSWords = StringArrayViaStringSplit(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumSWords.length
say 'Min, max and avg time for' tests 'iterations to create a String array using String.split:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumSWords = null

return

method RexxArrayViaStringSplit(wordList) private static binary returns Rexx[]
  sWords = wordList.toString.split("\\s+")
  rWords = Rexx[sWords.length]
  loop r_ = 0 to sWords.length - 1
    rWords[r_] = sWords[r_]
    end r_

  return rWords

method RexxArrayViaRexxWords(wordList) private static binary returns Rexx[]
  rWords = Rexx[wordList.words]
  loop r_ = 1 to wordList.words
    rWords[r_ - 1] = wordList.word(r_)
    end r_

  return rWords

method StringArrayViaStringSplit(wordList) private static binary returns String[]
  sWords = wordList.toString.split("\\s+")

  return sWords


Alan.

--
Can't tweet, won't tweet!

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Alan

--
Needs more cowbell.
Reply | Threaded
Open this post in threaded view
|

RE: What is the most efficient way...

Mike Cowlishaw

 
For fun I ran some comparisons looping on Rexx.words() and String.split() and got the following:

word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using String.split: 
0.000903 0.067337 0.003178 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using Rexx.words: 
0.265916 0.461035 0.284033 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a String array using String.split: 
0.000700 0.002405 0.000794 

So it would appear that using the String.split() method is faster than Rexx.words() by an order of magnitude.  If you can get awat with a String[] then String.split() is even more efficient. 
 
Hardly surprising ... if LEN=words(wordlist) then the split() approach only does one pass over the wordlist string and parses LEN words, whereas repeated calls to word() do LEN passes, parsing increasingly more words in each call: LEN! (LEN factorial) in all....
 
Mike  [offline for next 2-3 weeks]

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

George Hovey-2
Dave,
I wondered if I was missing any Rexx method that did this in an efficient way -- those I thought could be used to concoct a solution all seemed to require multiple passes over the characters.   As you and Mike point out, String.Split beats the Rexx approaches, even using regular expressions, and I can use this.
Thanks for your help!

On Tue, Nov 1, 2011 at 2:40 AM, Mike Cowlishaw <[hidden email]> wrote:

 
For fun I ran some comparisons looping on Rexx.words() and String.split() and got the following:

word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using String.split: 
0.000903 0.067337 0.003178 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using Rexx.words: 
0.265916 0.461035 0.284033 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a String array using String.split: 
0.000700 0.002405 0.000794 

So it would appear that using the String.split() method is faster than Rexx.words() by an order of magnitude.  If you can get awat with a String[] then String.split() is even more efficient. 
 
Hardly surprising ... if LEN=words(wordlist) then the split() approach only does one pass over the wordlist string and parses LEN words, whereas repeated calls to word() do LEN passes, parsing increasingly more words in each call: LEN! (LEN factorial) in all....
 
Mike  [offline for next 2-3 weeks]

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/




_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

alansam


On 1 November 2011 05:28, George Hovey <[hidden email]> wrote:
Dave,
 
Dave?
 
I wondered if I was missing any Rexx method that did this in an efficient way -- those I thought could be used to concoct a solution all seemed to require multiple passes over the characters.   As you and Mike point out, String.Split beats the Rexx approaches, even using regular expressions, and I can use this.
Thanks for your help!


Bearing in mind that NetRexx is perfectly suited to exploit everything that's available in the Java class libraries; I don't think you have.  Leveraging/plundering the Java class library to solve problems is one of NetRexx's major strengths and a significant reason why the language itself doesn't need to be significantly altered.  All it needs is the ability to keep up with Java's latest language devices.

Alan.

--
Can't tweet, won't tweet!

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Alan

--
Needs more cowbell.
Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

ThSITC
In reply to this post by alansam
Hello Alan, thanks for your testcase (and the resulting FINDINGS)...

... and it's also GOOD to recall my ancient LATIN Knowledge again;-)
Thomas.
===================================================================================
Am 31.10.2011 21:33, schrieb Alan Sampson:


On 31 October 2011 11:11, Robert Hamilton <[hidden email]> wrote:
One way to start is: string = space(string,1,',') to get one comma between each word.  Then put string between [  and ].  Maybe.

Sorta wingin' here.

BobH


That would require Interpret which isn't available in NetRexx.

For fun I ran some comparisons looping on Rexx.words() and String.split() and got the following:

word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using String.split: 
0.000903 0.067337 0.003178 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a Rexx array using Rexx.words: 
0.265916 0.461035 0.284033 
 
word count: 4320 
Min, max and avg time for 100 iterations to create a String array using String.split: 
0.000700 0.002405 0.000794 

So it would appear that using the String.split() method is faster than Rexx.words() by an order of magnitude.  If you can get awat with a String[] then String.split() is even more efficient.

Here's my test program:

/* NetRexx */
options replace format comments java nocrossref savelog symbols

lipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." -
         "Duis et sem elit, id ullamcorper erat." -
         "Maecenas dictum suscipit neque, non pharetra velit aliquet sit amet." -
         "Cras tincidunt, orci at tempus tincidunt, ligula tellus rutrum est," -
         "hendrerit congue lectus justo vel tortor." -
         "Vivamus sit amet consequat tellus." -
         "Pellentesque sapien augue, ornare ac tempor id, eleifend id nisl." -
         "Aliquam ultricies mauris ac velit mollis ut eleifend lorem hendrerit." -
         "Class aptent taciti sociosqu ad litora torquent per conubia nostra," -
         "per inceptos himenaeos." -
         "Nulla facilisi." -
         "Morbi ultricies faucibus vulputate." -
         "Nam lobortis, nibh id accumsan porttitor, urna dolor lacinia lectus," -
         "vitae placerat risus sapien id lorem." -
         "Etiam semper magna a lorem vestibulum eu scelerisque mi convallis." -
         "Ut condimentum tristique sem, eget adipiscing nunc venenatis sit amet." -
         "Phasellus laoreet laoreet facilisis." -
         "Nullam nulla leo, imperdiet ut tristique vitae, tempor sed arcu."

loop for 5
  lipsum = lipsum lipsum
  end

startTime = long
endTime = long

Numeric digits 20

tests = 100

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumRWords = RexxArrayViaStringSplit(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumRWords.length
say 'Min, max and avg time for' tests 'iterations to create a Rexx array using String.split:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumRWords = null

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumRWords = RexxArrayViaRexxWords(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumRWords.length
say 'Min, max and avg time for' tests 'iterations to create a Rexx array using Rexx.words:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumRWords = null

tmin = Long.MAX_VALUE
tmax = Long.MIN_VALUE
tsum = 0
loop for tests
  startTime = System.nanoTime()
  lipsumSWords = StringArrayViaStringSplit(lipsum)
  endTime = System.nanoTime()
  tmin = Math.min(endTime - startTime, tmin)
  tmax = Math.max(endTime - startTime, tmax)
  tsum = tsum + endTime - startTime
  end
tavg = tsum / tests
say 'word count:' lipsumSWords.length
say 'Min, max and avg time for' tests 'iterations to create a String array using String.split:'
say (tmin / 1000000000).format(null, 6) (tmax / 1000000000).format(null, 6) (tavg / 1000000000).format(null, 6)
say
lipsumSWords = null

return

method RexxArrayViaStringSplit(wordList) private static binary returns Rexx[]
  sWords = wordList.toString.split("\\s+")
  rWords = Rexx[sWords.length]
  loop r_ = 0 to sWords.length - 1
    rWords[r_] = sWords[r_]
    end r_

  return rWords

method RexxArrayViaRexxWords(wordList) private static binary returns Rexx[]
  rWords = Rexx[wordList.words]
  loop r_ = 1 to wordList.words
    rWords[r_ - 1] = wordList.word(r_)
    end r_

  return rWords

method StringArrayViaStringSplit(wordList) private static binary returns String[]
  sWords = wordList.toString.split("\\s+")

  return sWords


Alan.

--
Can't tweet, won't tweet!


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/



--
Thomas Schneider (Founder of www.thsitc.com) Member of the Rexx Languge Asscociation (www.rexxla.org) Member of the NetRexx Developer's Team (www.netrexx.org)

_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Thomas Schneider, Vienna, Austria (Europe) :-)

www.thsitc.com
www.db-123.com
Reply | Threaded
Open this post in threaded view
|

Re: What is the most efficient way...

Aviatrexx
In reply to this post by alansam
On 11/1/11 16:11 Alan Sampson said:
>
> Leveraging/plundering the Java class library to solve problems is one
> of NetRexx's major strengths and a significant reason why the language
> itself doesn't need to be significantly altered.

THANK YOU, Alan!

Please say that often, loud & proud.

René, I think we have a new quote for the netrexx.org website.

-Chip-


_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/