Parallel.Foreach を使用している場合でも、完了までに 7 分以上かかる次のコードがあります。私が反復するリスト「final_products」には、約 7000 個の製品が含まれています。
public void GenerateTreeFromAllFinalProducts()
{
XmlSerializer serializer = new XmlSerializer(typeof(ImageFeature<float>[]));
DSTableAdapters.Products_UniqueTableAdapter pft = new DSTableAdapters.Products_UniqueTableAdapter();
DSTableAdapters.Products_Unique_SURFTableAdapter pus = new DSTableAdapters.Products_Unique_SURFTableAdapter();
DS.Products_UniqueDataTable final_products = pft.GetData();
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Parallel.ForEach(final_products.AsParallel(), row =>
{
//Get SURF data for all images found similar to this image
Types.Products_Unique_SURFRow surfData = GetDataByUniqueProductID(row.id);
ImageFeature<float>[] row_features = (ImageFeature<float>[])serializer.Deserialize(new StringReader(Decompress(surfData.SURF)));
if (row_features != null)
flann.AddSurfDescriptors(row_features, row.id);
});
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
}
この時間がかかるのは正常ですか? そうでない場合、どうすればコードを最適化できますか?
GetDataByUniqueProductID(row.id) は、1 行を返すデータベースへの呼び出しです。
private static Types.Products_Unique_SURFRow GetDataByUniqueProductID(int rowid)
{
Types.Products_Unique_SURFRow ret = new Types.Products_Unique_SURFRow();
string sqlText = "SET ROWCOUNT 1 SELECT SURF from Products_Unique_SURF WHERE unique_product_id =" + rowid;
using (SqlConnection myConn = new SqlConnection(global::SCBot.Properties.Settings.Default.DataConnectionString))
{
myConn.Open();
SqlCommand cmd = new SqlCommand(sqlText, myConn);
try
{
cmd.CommandType = CommandType.Text;
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Types.Products_Unique_SURFRow row = new Types.Products_Unique_SURFRow();
row.SURF = Convert.ToString(reader["SURF"]);
ret = row;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
return ret;
}
私の最初のコードは次のとおりでした
public void GenerateTreeFromAllFinalProducts()
{
XmlSerializer serializer = new XmlSerializer(typeof(ImageFeature<float>[]));
DSTableAdapters.Products_UniqueTableAdapter pft = new DSTableAdapters.Products_UniqueTableAdapter();
DSTableAdapters.Products_Unique_SURFTableAdapter pus = new DSTableAdapters.Products_Unique_SURFTableAdapter();
DS.Products_UniqueDataTable final_products = pft.GetData();
foreach (DS.Products_UniqueRow row in final_products)
{
//Get SURF data for all images found similar to this image
List<DS.Products_Unique_SURFRow> surfData = pus.GetDataByUniqueProductID(row.id).ToList();
foreach (DS.Products_Unique_SURFRow data in surfData)
{
ImageFeature<float>[] row_features = (ImageFeature<float>[])serializer.Deserialize(new StringReader(Decompress(data.SURF)));
flann.AddSurfDescriptors(row_features, row.id);
}
}
}
しかし、これは遅すぎたので、Parallel.Foreach を実行しようとしました。