批量处理RACSequence或RACSignal

时间:2014-03-04 15:55:35

标签: ios objective-c functional-programming reactive-cocoa

更新3/5/14: RACSequence在ReactiveCocoa 3.0中已弃用(请参阅评论),但我仍然对使用RACSequence或RACSignal批量处理数据的最佳方式感到好奇。

我正在尝试使用ReactiveCocoa处理潜在的大量值。我的想法是将输入序列分成最小尺寸的较小序列,然后单独处理它们(可能并行处理)。

我在RACSequence上编写了一个类别方法来进行分块:

// Returns a sequence of sequences of at most `size` objects

- (RACSequence *)chunk:(NSInteger)size
{
    if ([self head] == nil) {
        return [RACSequence empty];
    }

    RACSequence *chunk = [self take:size];
    RACSequence *rest  = [self skip:size];

    return [RACSequence sequenceWithHeadBlock:^id {
        return chunk;
    } tailBlock:^RACSequence *{
        return [rest chunk:size];
    }];
}

这有效,但对于大型序列来说速度极慢。 I wrote an iterative version and a utility to compare the two approaches。这是10000个数字序列的差异,这些数字被分解成最多30个对象的较小序列:

$ time ./main i 10000 30 # Iterative
2014-03-04 10:48:28.845 main[59637:507] Number of chunks: 334

real    0m0.012s
user    0m0.007s
sys 0m0.004s

$ time ./main r 10000 30 # Recursive
2014-03-04 10:48:45.423 main[59645:507] Number of chunks: 334

real    0m15.513s
user    0m15.133s
sys 0m0.378s

有更好的方法以较小的批次处理RACSequence吗?

0 个答案:

没有答案