开发者

Do something for every hundredth element in an array

开发者 https://www.devze.com 2023-04-05 10:05 出处:网络
I have 1 million elements in an array, I need to divide these elements into groups of 100, do a function and continue working on the next hundred

I have 1 million elements in an array, I need to divide these elements into groups of 100, do a function and continue working on the next hundred

foreach (string value in lines)
{
    files开发者_如何学JAVAcreated++;
    if (filescreated == ?????????)
    {
        do stuff 
    }
}

???? is equal to value divisible by 100


is equal to value divisable by 100

foreach (...)
{
    filescreated++;

    if (filescreated % 100 == 0) 
    {
        // do stuff for the every 100th element
    }

    // do other stuff for every element
}

Reference: modulus (%) operator

Use this if you need to do something special for every 100th element, but you still need to process every element.

If you only need to process every 100th element, refer to Reed's answer.


What about this (if you need in order and % isn't good for you)?

The question is confusing as you talk about every hundredth element, then after about packs of 100. So here would be a guess.

        string[] lines = new string[1000000];
        for (int i = 0; i < 10000; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                DoSomething(lines[100*i + j], i);
            }
        }


I need to divide these elements into groups of 100, do a function and continue working on the next hundred

You can do this directly, if this is an array, just by using a for loop and incrementing by 100:

int chunkSize = 100;

for (int start=0; start<lines.Length;start += chunkSize)
{        
    ProcessSectionOfArray(lines, start, Math.Min(start+chunkSize-1, lines.Length-1));
}


Here is a solution which separates the partitioning logic into a separate function.

    // A separate static function
    private static IEnumerable<IEnumerable<T>> BreakIntoBlocks<T>(T[] source, int blockSize)
    {
        for (int i = 0; i < source.Length; i += blockSize)
        {
            yield return source.Skip(i).Take(blockSize);
        }
    }


    // And in your code

    string[] lines = new string[1000000];
    foreach(IEnumerable<string> stringBlock in BreakIntoBlocks(lines, 100))
    {
            // stringblock is a block of 100 elements
            // Here is where you put the code that processes each separate group
    }

The attempt above should be faster than my first attempt (below)

        int blockSize = 100;
        int i = 0;
        IEnumerable<IEnumerable<string>> query = from s in lines
                                                let num = i++
                                                group s by num / blockSize into g
                                                select g;
        foreach(IEnumerable<string> stringBlock in query)
        {
            // Stringblock will be a block of 100 elements.
            // Process this 100 elements here.
        }

The problem which using the grouping clause is that LINQ will allocate every one of those 1000000 element to groups before it returns the first element.

0

精彩评论

暂无评论...
验证码 换一张
取 消