I have a parallel loop that checks around 1 mil records in the database and sends many requests to the database. I re-declare the new DB object on each parallel loop iteration.
DataSet ds = new psqlWork().getDataSet("SELECT * FROM z_sitemap_links");
DataTable dt = ds.Tables[0];
Parallel.ForEach(dt.AsEnumerable(), dr =>
{
new Sitemap().runSitemap(dr[1].ToString(), counter);
counter++;
});
Is it proper to declare the new Sitemap() object outside the loop, or on each execution?
The meaning of the code is totally different in these 2 cases, not sure why this “performance” question…
If your object is shared across all iterations – declare outside and share. Otherwise declare locally.