2.2.2 parallel_for_each版的Credit Review
并行版的信用度分析程序与串行版本几乎相差无几:
- void UpdatePredictionsParallel(AccountRepository& accounts)
- {
- parallel_for_each(accounts.begin(), accounts.end(),
- [ ]
- (AccountRepository::value_type& record)
- {
- Account& account = record.second;
- Trend trend = Fit(account.Balances());
- double prediction = PredictIntercept(trend,
- (account.Balances().size() + g_predictionWindow));
- account.ParPrediction() = prediction;
- account.ParWarning() = prediction < account.GetOverdraft();
- });
- }
如你所见,除了用parallel_for_each替代for_each外,UpdatePredictionsParallel方法的代码与UpdatePredictionsSequential几乎完全一致。