的Parallel.For步长 [英] Parallel.For step size

查看:180
本文介绍了的Parallel.For步长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有人知道是否有任何的过载,让我来指定一个的Parallel.For循环的步长?在C#或VB.Net样品将是巨大的。

does anyone know if there's any overload that would allow me to specify a step size in a Parallel.For loop? Samples in either c# or VB.Net would be great.

谢谢,伊

推荐答案

谷歌enumerable.range一步,你应该能够临到Enumerable.Range的提供加强的范围替代实现。然后,你可以做一个

Google for "enumerable.range step" and you should be able to come upon alternate implementations of Enumerable.Range that provide stepped ranges. Then you can just do a

Parallel.ForEach(BetterEnumerable.SteppedRange(fromInclusive, toExclusive, step), ...)

如果谷歌不工作,实现应该是这样的:

If google isn't working, implementation should be something like this:

public static class BetterEnumerable {
    public static IEnumerable<int> SteppedRange(int fromInclusive, int toExclusive, int step) {
        for (var i = fromInclusive; i < toExclusive; i += step) {
            yield return i;
        }
    }
}

另外,如果收益回报给出一个heebie jeebies,你总是可以只创建一个普通的老名单就地:

Alternately if "yield return" gives one the heebie jeebies, you can always just create a regular old list in-place:

var list = new List<int>();
for (int i = fromInclusive; i < toExclusive; i += step) {
    list.Add(i);
}
Parallel.ForEach(list, ...);

这应该很容易翻译到VB,如果这是必需的。

This should be easily translatable to VB if that's a requirement.

这篇关于的Parallel.For步长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆