为什么for循环中的异步处理不会缩短执行时间?

我试图了解并发性,因此我尝试从A Tour of C ++(第二版)15.7.3,页面中编写Stroustrup示例代码(comp4())的更灵活的版本(my_comp())。 205。 它给出了正确的答案,但没有使用并发来缩短执行时间。我的问题是:为什么my_comp()不能按预期运行,该如何解决?

NavigationLink(destination:
         TrendSlider(
            title: .constant(title),col: .constant(self.dataModel.queryColumnSeasonWinPercentageTeam1),opIndx: self.$dataModel.seasonWinPercentageTeam1OperatorIndxValue,val: self.$dataModel.seasonWinPercentageTeam1SliderValue,lBound: .constant(self.dataModel.seasonWinPercentageTeam1Values.first!),uBound: .constant(self.dataModel.seasonWinPercentageTeam1Values.last!),lRange: self.$dataModel.seasonWinPercentageTeam1RangeSliderLowerValue,uRange: self.$dataModel.seasonWinPercentageTeam1RangeSliderupperValue,step: .constant(1.0),displayReadableQuery: $readableQuery

   )) {
       HStack {
          if readableQuery.hasSuffix("IS ANY") {
                Text(title)
                Spacer()
                Text("IS ANY").foregroundColor(Color("TPLightGrey"))
            }else{
                Text(readableQuery).foregroundColor(Color("TPOrange"))

            }
       }
   }.simultaneousGesture(TapGesture().onEnded{
       sendViewSelectionEvent(name: self.title,referral: "Game")

   })

已与Visual C ++ 2019(ISO C ++ 17 Standard(/ std:c ++ 17))X64版本一起编译。典型输出为:

424毫秒 输出= 1.93496(累计)

431毫秒比率:1.01651 输出= 1.93496(my_comp)

117毫秒比率:0.275943 输出= 1.93496(comp4)

我知道并行算法和std :: reduce。我的问题不是如何优化这种特定的计算,而是要学习一些有关如何编写按预期方式运行的并发代码的知识。

wxw398800941 回答:为什么for循环中的异步处理不会缩短执行时间?

您的问题在这里:(i / conc)。一旦0 <= i < conciconc为整数,这意味着该计算始终为零。

要解决您的问题,请删除括号:

auto f = async(accum,v0 + sz * i / conc,v0 + sz * (i + 1) / conc,0.0);
本文链接:https://www.f2er.com/3163527.html

大家都在问