Solved

# Efficient Group Processing

Posted on 2009-02-19
194 Views
This should be easy and want to try to get another way of doing this. I will not show my answer because I don't want to taint your way of thinking. I want to find an easy but efficient way to solve this issue.

In Parameters (2)

String[] s = {"KJDKF","SDJFK","JENN","HDEEJ","JSKE","UAJS"}
4 = Max items I want in each dimension

string[] result = Splitter(s,4);

Tho expected output:

result [0] = "KJDKF","SDJFK","JENN","HDEEJ"
result [1] = "JSKE","UAJS"

I currently have it going through 2 loops but I think there is a quicker way and my brain now hurts from other login I was doing.

Thanks

0

LVL 9

Expert Comment

not tested, but here's how I would do it...
``````public string[][] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);

int remainder = numElements % maxNum;

List<string[]> result = new List<string[]>(numResultElements);

for (int i = 0; i < numResultElements; i++) {

string[] currArray;

int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

currArray = new string[numCurrElements];

Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);

}

return result.ToArray();

}
``````
0

LVL 9

Accepted Solution

Sorry, ignore the above - try this:
``````public string[] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);

int remainder = numElements % maxNum;

List<string> result = new List<string>(numResultElements);

for (int i = 0; i < numResultElements; i++) {

string[] currArray;

int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

currArray = new string[numCurrElements];

Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);

}

return result.ToArray();

}
``````
0

LVL 29

Expert Comment

It's quite interestin but I came to a nearly same solution as rob263. Independently :)

My is checked.
``````    private void button1_Click(object sender, EventArgs e)

{

////HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com/helloworld.xml");

////string result = ((HttpWebResponse)myRequest.GetResponse()).ToString();

//XmlNodeList greeting = doc.GetElementsByTagName("greeting");

//MessageBox.Show(greeting[0].InnerXml);

//    MessageBox.Show("checked");

//}

//else{

//    MessageBox.Show("not checked");

//}

String[] s = { "KJDKF", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS", "SDJFK", "JENN", "HDEEJ", "JSKE", "UAJS" };

int limit = 4;

List<string[]> splitted = splitter(s,limit);

}

private List<string[]> splitter(string[] origArray, int limit)

{

List<string[]> splittedArrays = new List<string[]>();

string[] currentArray = null;

for (int i = 0; i < origArray.Length; i++)

{

if ((i % limit) == 0)

{

if ((origArray.Length - limit * splittedArrays.Count) >= limit)

{

currentArray = new string[limit];

}

else

{

currentArray = new string[origArray.Length - limit * splittedArrays.Count];

}

}

currentArray[i - (splittedArrays.Count - 1) * limit] = origArray[i];

}

return splittedArrays;

}
``````
0

LVL 29

Expert Comment

Sorry! Ignore this HTTP commented part - just some rubbish :)
0

LVL 4

Author Comment

Thats great, I will post one tomorrow that was suggested to me. The concept made since after I stepped back and then it hit my "why didn't I think of that" hopefully it will work.

Thanks for the input, will check them out tomorrow.
0

LVL 4

Author Comment

This is what I came up with ... Any other people want to submit an example?

``````

private static string[] SplitString(string sInput, int maxItemsInArray)

{

List<string> items = new List<string>();

string[] splitter = sInput.Split(",".ToCharArray());

int pos = 0;

int group = maxItemsInArray;

while (pos < splitter.Length)

{

if (pos < splitter.Length - maxItemsInArray && group < splitter.Length)

{

StringBuilder sbVal = new StringBuilder();

for (int i = 0; i < maxItemsInArray; i++)

{

sbVal.Append(splitter[pos] + ",");

pos++;

}

group += 100;

}

else

{

StringBuilder sbVal = new StringBuilder();

for (int i = pos; i < splitter.Length; i++)

{

sbVal.Append(splitter[pos] + ",");

pos++;

}

}

}

return items.ToArray();

}
``````
0

LVL 9

Expert Comment

with all your use of stringbuilders and Substring(), I think your solution is less efficient.
0

LVL 4

Author Comment

I would tend to agree, thanks for the feedback. It's interesting finding different ways to do the same thing.
0

LVL 4

Author Comment

Here are the results that I recorded processing 247 GUIDS in groups of 50 max size:

My Code:
Start 0
Finish 7296576 ticks 2 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 7498099 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 784833 ticks 0 ms

0

LVL 4

Author Comment

Actually I needed to take the average of 5 different runs without debugging

My Code:
Start 0
Finish 15484805 ticks 5 ms
-----------------------------------
anarki_jimbel:
Start 0
Finish 6445026 ticks 2 ms
-----------------------------------
rob263:
Start 0
Finish 620564 ticks 0 ms

0

LVL 9

Expert Comment

Try the following - I think it might be even better.  The difference is that I no longer allocate a new array in every iteration of the loop.  Since memory allocation is a relatively slow operation, it will probably help.
``````public string[] Splitter(string[] s, int maxNum) {

int numResultElements = (int)Math.Ceiling(s.length/(double)maxNum);

int remainder = numElements % maxNum;

List<string> result = new List<string>(numResultElements);

string[] currArray = new string[maxNum];

for (int i = 0; i < numResultElements; i++) {

int numCurrElements = (i == numResultElements-1) ? remainder : maxNum;

if (numCurrElements != maxNum) {

currArray = new string[numCurrElements];

}

Array.Copy(s, i * maxNum, currArray, 0, numCurrElements);

}

return result.ToArray();

}
``````
0

LVL 9

Expert Comment

There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0

LVL 9

Expert Comment

There were valid answers given.  Points should be awarded to rob263 and anarki_jimbel
0

LVL 29

Expert Comment

And it looks like rob's code is most efficient!
0

LVL 9

Expert Comment

I recommend http:#23686840 - since that's what was used in the author's benchmark test and is an order of magnitude faster than the other solutions.

0

## Featured Post

### Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…