下面是我的一点点重构(+一些额外的代码,因为out-parameters)简单的方法,我尝试使用Parallel.Invoke(...)来加快速度。
(请忽略变量中的 async 字词:))。
同步版本是相同的 - 同步调用相同的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;
}
答案 0 :(得分:2)
Parallel.Invoke
方法将 possibly run the code asynchronously ,具体取决于代码是否会同步运行更快。在您的情况下,如果您只是填充集合并且没有昂贵的I / O或服务调用,那么它可能只是同步运行所有内容。
您应该做的是将低成本构造分组到一个代码块中,然后将更昂贵的调用(即DB或Web服务)分成单独的块,因为这些可能会增加更多的等待时间。
编辑:MSDN
的附加引用不保证操作执行的顺序或它们是否并行执行。