2014-01-07 44 views
-2

下面是我的一点点重构(+一些额外的代码,因为out-parameters)简单的方法,我尝试使用Parallel.Invoke(...)更快地完成任务。c#Parallel.Invoke(零速度改进)

(请忽略async word内部变量:))。

同步版本是相同的 - 同步调用相同的7个方法。

所有这7种方法都很简单 - 它们只是填充和返回集合。

现在 - 我发现惊人的是这样的:方法执行几乎相同时间(〜6秒)。

这怎么可能我错过了什么?

private static void GenerateCachingHelpersAsync(
    SingleVODCache _svc, 
    Dictionary<int, DataRow> _dicFirstContentsFilter, 
    DataSet _ds, 
    out ConcurrentDictionary<string, DataRow> _cdicValidContentCatalogPrices, 
    out ConcurrentDictionary<int, DataRow> _cdicAllMetaDataHelper, 
    out List<KeyValuePair<int, int>> _helperContentMetaDataSync, 
    out ConcurrentDictionary<int, DataRow> _cdicAllAssetsHelper, 
    out List<KeyValuePair<int, int>> _helperContentAssetsSync, 
    out Dictionary<int, OneContentAllViewsHelper> _dicAllContentsViewsHelper, 
    out ConcurrentDictionary<int, List<Category>> _cdicValidContentCategories 
    ) 
    { 
    ConcurrentDictionary<string, DataRow> _cdicValidContentCatalogPricesAsync = new ConcurrentDictionary<string, DataRow>(); 
    ConcurrentDictionary<int, DataRow> _cdicAllMetaDataHelperAsync = new ConcurrentDictionary<int, DataRow>(); 
    List<KeyValuePair<int, int>> _helperContentMetaDataSyncAsync = new List<KeyValuePair<int, int>>(); 
    ConcurrentDictionary<int, DataRow> _cdicAllAssetsHelperAsync = new ConcurrentDictionary<int, DataRow>(); 
    List<KeyValuePair<int, int>> _helperContentAssetsSyncAsync = new List<KeyValuePair<int, int>>(); 
    Dictionary<int, OneContentAllViewsHelper> _dicAllContentsViewsHelperAsync = new Dictionary<int, OneContentAllViewsHelper>(); 
    ConcurrentDictionary<int, List<Category>> _cdicValidContentCategoriesAsync = new ConcurrentDictionary<int, List<Category>>(); 

    Parallel.Invoke(
     () => 
     { 
      _cdicValidContentCatalogPricesAsync = BuildValidContentCatalogPricesHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.CONTENT_CATALOG_PRICE_SYNC)]); 
     }, 
     () => 
     { 
      _cdicAllMetaDataHelperAsync = BuildAllMetaDataHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.META_DATA)]); 
     }, 
     () => 
     { 
      _helperContentMetaDataSyncAsync = BuildContentMetaDataSyncHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.CONTENT_META_DATA_SYNC)]); 
     }, 
     () => 
     { 
      _cdicAllAssetsHelperAsync = BuildAllAssetsHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.ASSETS)]); 
     }, 
     () => 
     { 
      _helperContentAssetsSyncAsync = BuildContentAssetsSyncHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.CONTENT_ASSET_SYNC)]); 
     }, 
     () => 
     { 
      _dicAllContentsViewsHelperAsync = BuildAllContensViewsVotesHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.CONTENT_VIEWS)], _dicFirstContentsFilter, _svc.MetaData.HowManyPastDaysForMostViewed); 
     }, 
     () => 
     { 
      _cdicValidContentCategoriesAsync = BuildValidContentCategoriesHelper(_ds.Tables[GetCMSDataTableIndex(m_eCMSDataTablesIndexes.CONTENT_CATEGORY_SYNC)], _dicFirstContentsFilter, _svc.AllCategories); 
     } 
     ); 

    _cdicValidContentCatalogPrices = _cdicValidContentCatalogPricesAsync; 
    _cdicAllMetaDataHelper = _cdicAllMetaDataHelperAsync; 
    _helperContentMetaDataSync = _helperContentMetaDataSyncAsync; 
    _cdicAllAssetsHelper = _cdicAllAssetsHelperAsync; 
    _helperContentAssetsSync = _helperContentAssetsSyncAsync; 
    _dicAllContentsViewsHelper = _dicAllContentsViewsHelperAsync; 
    _cdicValidContentCategories = _cdicValidContentCategoriesAsync; 
    } 
+3

您的计算是否受CPU限制。 I/O绑定或内存绑定? – matcheek

+0

CPU /内存绑定。没有I/O操作。 – sabiland

回答

2

Parallel.Invoke方法将possibly run the code asynchronously取决于代码是否将运行得更快同步与否。就你而言,如果你只是填充集合,并且没有昂贵的I/O或服务调用,那么它可能只是同步运行所有内容。

你应该做的是将低成本的构造分组到一个代码块中,然后将更昂贵的调用(即数据库或Web服务)分成不同的块,因为这可能会增加更多的等待时间。

编辑:附加报价从MSDN

无担保都对这些操作执行或者是否并行执行的顺序。