Forums Register Login

Is there a faster / less memory-intensive way of doing this?

+Pie Number of slices to send: Send
let's say i have a String array and another string with commas in it:
String[] sArr = new String[] { "a", "b" };
String s = "c,d,e";
Is there a better way to do all this)?

[ January 30, 2003: Message edited by: Robert Paris ]
+Pie Number of slices to send: Send
I think that last line should be
System.arrayCopy( s1Arr, 0, complete, sArr.length, s1Arr.length );
Other than that, I think you've hit on the best reasonably simple way to do this. If you determine that you really need to improve performance, it may be possible to replace it with something quicker. Use a profiler to find out where the bottleneck is currently. I'm guessing the split() method could be improved - since it accepts a regex pattern as an argument (which may be arbitrarily complex), it may not be optimized for the fastest possible search for something nice and simple like ",". You could hand-make something that loops through the comma-delimited string using indexOf(',', lastCommaPosition). You'd probably want to go through the sting twice - once just to count commas (so you can correctly size the complete array), and once to load the values in. You could also use StringTokenizer to do this; I'm guessing you could make a slightly faster version yourself, but a StingTokenizer version would be worth looking at for comparison.
+Pie Number of slices to send: Send
yeah, my last line I had wrong but I caught that in time. Thanks!
I thought about looping through the string - do you really think that's faster than arraycopy? I never would have thought that. Especially since I do have to loop through twice.
It just all seems so wasteful and inelegant. I was thinking, there just has to be a better way.
+Pie Number of slices to send: Send
Well, you don't have to loop through twice. The alternative is to use an ArrayList to store the tokens as you find them. That's what the split() method does internally. I'm not really sure which is faster in this case - I think they might have used the ArrayList solution inside split() because they didn't know how complex the pattern might be. Since a really complex pattern might be really slow, they decided to minimize the risk and only loop once. Paying for it with the slightly slower access methods of an ArrayList vs an array, plus an extra array copy. (To go from the internal array of ArrayList to the exactly-sized array returned by split()). For a really simple pattern it might be quicker to loop twice and get the array size just right in the first place. Just a guess; could be way off.
In any event, I don't think the main possible slowness of the first method is in the System.arraycopy() - I think it's inside split(). And if you replace split() correctly you may not need another arraycopy() after anyway.
[ January 30, 2003: Message edited by: Jim Yingst ]
Are you here to take over the surface world? Because this tiny ad will stop you!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1056 times.
Similar Threads
Writing data to a byte array.
get() method not returning values properly
using FTP view client machine files
K&B-6-pg-591 - Backed Collections - exam watch
Return type problem in function
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:41:34.