C# 多线程和TPL不能加快C的执行速度#
我有一个程序,它利用C# 多线程和TPL不能加快C的执行速度#,c#,multithreading,async-await,task-parallel-library,C#,Multithreading,Async Await,Task Parallel Library,我有一个程序,它利用SQL Server从数据库中提取信息,然后执行一系列插入到其他表中的操作,并发送一封包含已停用数据的电子邮件 程序执行大约需要3.5分钟,数据库中只有5行数据。我正试图以任何方式减少这一时间,我已经尝试了多线程,这似乎会进一步降低速度,而TPL既不会增加也不会减少时间。有人知道我为什么没有看到性能改进吗 我使用的是Intel Core i5,我知道它有两个内核,因此使用两个以上的内核会降低性能。以下是我如何整合任务的使用: private static void M
SQL Server
从数据库中提取信息,然后执行一系列插入到其他表中的操作,并发送一封包含已停用数据的电子邮件
程序执行大约需要3.5分钟,数据库中只有5行数据。我正试图以任何方式减少这一时间,我已经尝试了多线程
,这似乎会进一步降低速度,而TPL
既不会增加也不会减少时间。有人知道我为什么没有看到性能改进吗
我使用的是Intel Core i5
,我知道它有两个内核,因此使用两个以上的内核会降低性能。以下是我如何整合任务的使用:
private static void Main(string[] args)
{
Util util = new Util(); //Util object
List<Data> dataList = new List<Data>(); //List of Data Objects
//Reads each row of data and creates Data obj for each
//Then adds each object to the list
dataList = util.getData();
var stopwatch = Stopwatch.StartNew();
var tasks = new Task[dataList.Count];
int i = 0; //Count
foreach (Data data in dataList)
{
//Perform insertions and send email with data
tasks[i++] = Task.Factory.StartNew(() => util.processData(data));
}
Task.WaitAll(tasks); //Wait for completion
Console.WriteLine("DONE: {0}", stopwatch.ElapsedMilliseconds);
}
async/await
并不意味着加快执行速度,尽管在某些情况下可能会加快执行速度。它的目的是提供并简化异步执行。提示:您有很多样板代码,可以用dataList.AsParallel()代替。ForAll(i=>util.processData(i))代码>首先,使用ORM(例如EF)简化访问=>现在你a)把所有东西都拉下来,然后b)处理。你可以一边拉一边处理。第二,这在很大程度上取决于什么是insider RenderData()——因为您没有提供代码,所以很难详细说明。这只是一个非常大胆的猜测:RenderData函数是否包含任何锁定?(如果调用锁定了同一个对象,线程仍需彼此等待)拼图中缺少的部分太多。LoadReport
在做什么,IO工作?渲染是做什么的?当您不在循环中时,为什么要使用break
?
class Util
{
// create and open a connection object
SqlConnection conn = new SqlConnection("**Connection String**");
//Gets all results from table, and adds object to list
public List<Data> getData()
{
conn.Open();
SqlCommand cmd = new SqlCommand("REF.GET_DATA", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = cmd.ExecuteReader();
List<Data> dataList = new List<Data>();
while (reader.Read())
{
//** Take data from table and assigns them to variables
//** Removed for simplicity
Data data= new Data(** pass varaibles here **);
dataList.Add(data); //Add object to datalist
}
return dataList;
}
public void processData(Data data)
{
//** Perform range of trivial operations on data
//** Removed for simplicity
byte[] results = data.RenderData(); //THIS IS WHAT TAKES A LONG TIME TO COMPLETE
data.EmailFile(results);
return;
} //END handleReport()
}
//returns byte data of report results which will be attatched to email.
public byte[] RenderData(string format, string mimeType, ReportExecution.ParameterValue[] parameters)
{
ReportExecutionService res = new ReportExecutionService();
res.Credentials = System.Net.CredentialCache.DefaultCredentials;
res.Timeout = 600000;
//Prepare Render arguments
string historyID = null;
string deviceInfo = String.Empty;
string extension = String.Empty;
string encoding = String.Empty;
ReportExecution.Warning[] warnings = null;
string[] streamIDs = null;
byte[] results = null;
try
{
res.LoadReport(reportPath, historyID);
res.SetExecutionParameters(parameters, "en-gb"); //"/LSG Reporting/Repossession Sales (SAL)/SAL004 - Conveyancing Matter Listing"
results = res.Render(format, deviceInfo, out extension, out mimeType, out encoding, out warnings, out streamIDs);
break;
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace)
}
return results;
}