我這裏有一個代碼,使用LINQ工程師列表生成XML。我的問題是,是否有改進和加快這一方法有人可以幫助我提高這個LINQ
public static string CreateXMLforEngineersByLinq(List<Engineers> lst)
{
string x = "<Engineers>\n";
x += string.Concat(lst.Select(s =>
string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID, s.LastName, s.FirstName, s.MiddleName)));
return x + "</Engineers>";
}
結果的任何方式:
嗨,下面的代碼被添加爲我展示我公司生產多種答案的方法實際速度和修訂再次歡迎感謝那些誰的人誰幫助這裏:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using test.Classes;
namespace test.LINQ
{
public static class BatchOperations
{
delegate string Operations(List<Engineers> i);
public static List<int> BatchAddition(List<int> lstNumbers)
{
lstNumbers = (from nl in lstNumbers
select nl + 2).ToList();
return lstNumbers;
}
public static string CreateXMLforEngineersTheSimpleWay(IEnumerable<Engineers> lst)
{
StringBuilder x = new StringBuilder();
x.AppendLine("<Engineers>");
foreach (var s in lst)
{
x.AppendFormat("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID,
s.LastName,
s.FirstName,
s.MiddleName);
}
x.AppendLine("</Engineers1>");
return x.ToString();
}
public static string CreateXMLforEngineersByLinq(List<Engineers> lst)
{
string x = "<Engineers>\n";
x += string.Concat(lst.Select(s =>
string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n",
s.LicenseID, s.LastName, s.FirstName, s.MiddleName)));
return x + "</Engineers2>";
}
public static string CreateXMLforEngineersByLoop(List<Engineers> lst)
{
string XmlForEngineers = "<Engineers>";
foreach (Engineers item in lst)
{
XmlForEngineers += string.Format("<Engineer>\n<LicenseID>{0}</LicenseID>\n<LastName>{1}</LastName>\n<FirstName>{2}</FirstName>\n<MiddleName>{3}</MiddleName>\n</Engineer>\n"
, item.LicenseID, item.LastName, item.FirstName, item.MiddleName);
}
XmlForEngineers += "</Engineers3>";
return XmlForEngineers;
}
public static void ShowEngineersByLicense()
{
List<Engineers> lstEngr = new List<Engineers>();
Engineers tom = new Engineers();
tom.FirstName = "Tom";
tom.MiddleName = "Brook";
tom.LastName = "Crook";
tom.LicenseID = "1343-343434";
Engineers ken = new Engineers();
ken.FirstName = "ken";
ken.MiddleName = "Brook";
ken.LastName = "Crook";
ken.LicenseID = "1343-343434";
Engineers ben = new Engineers();
ben.FirstName = "ben";
ben.MiddleName = "Brook";
ben.LastName = "Crook";
ben.LicenseID = "1343-343434";
for (int y = 0; y <= 1000; y++)
{
lstEngr.Add(tom);
lstEngr.Add(ken);
lstEngr.Add(ben);
}
List<Operations> j = new List<Operations>();
j.Add(a => CreateXMLforEngineersTheSimpleWay(lstEngr));
j.Add(i => CreateXMLforEngineersByLinq(lstEngr));
j.Add(i => CreateXMLforEngineersByLoop(lstEngr));
DateTime start, end;
TimeSpan diff1 = new TimeSpan();
foreach (Operations currentMethod in j)
{
start = DateTime.Now;
Console.Write(currentMethod(lstEngr));
end = DateTime.Now;
diff1 = end - start;
Console.WriteLine(diff1);
Console.Write("\n\n");
}
}
}
}
你應該重新進行基準測試。 1:列表中需要超過3人才能產生顯着差異(嘗試10,000)。 2:他們需要分配不同的字符串(這在現實世界中很可能是這種情況)。 C#具有不可變的字符串,所以擁有相同的4個字符串將非常快速。使用Guid.NewGuid()。ToString()。第三,你需要多次運行測試(比如說100),並獲取平均值。我發現很難相信字符串+ =字符串表現超越StringBuilder – Rob
以5000人運行,我得到的結果是:LINQ:0.019ms,For-Loop:12.059 _seconds_,簡單的方法:0.010ms – Rob
我欣賞那條評論@Rob –