gpt4 book ai didi

c# - 我怎样才能使这段代码更快? List().Find(lambda_expression)

转载 作者:行者123 更新时间:2023-12-02 22:02:13 25 4
gpt4 key购买 nike

我需要将电子邮件发送与电子邮件退回进行匹配,以便确定它们是否已送达。问题是,我必须将退回限制在发送后的 4 天内,以消除匹配错误发送到退回的情况。发送记录分布在 30 天内。

LinkedList<event_data> sent = GetMyHugeListOfSends(); //for example 1M+ records
List<event_data> bounced = GetMyListOfBounces(); //for example 150k records

bounced = bounced.OrderBy(o => o.event_date).ToList(); //this ensures the most accurate match of bounce to send (since we find the first match)

List<event_data> delivered = new List<event_data>();
event_data deliveredEmail = new event_data();

foreach (event_data sentEmail in sent)
{

event_data bounce = bounced.Find(item => item.email.ToLower() == sentEmail.email.ToLower() && (item.event_date > sentEmail.event_date && item.event_date < sentEmail.event_date.AddDays(deliveredCalcDelayDays)));

//create delivered records
if (bounce != null)
{
//there was a bounce! don't add a delivered record!
}
else
{
//if sent is not bounced, it's delivered
deliveredEmail.sid = siteid;
deliveredEmail.mlid = mlid;
deliveredEmail.mid = mid;
deliveredEmail.email = sentEmail.email;
deliveredEmail.event_date = sentEmail.event_date;
deliveredEmail.event_status = "Delivered";
deliveredEmail.event_type = "Delivered";
deliveredEmail.id = sentEmail.id;
deliveredEmail.number = sentEmail.number;
deliveredEmail.laststoretransaction = sentEmail.laststoretransaction;

delivered.Add(deliveredEmail); //add the new delivered
deliveredEmail = new event_data();

//remove bounce, it only applies to one send!
bounced.Remove(bounce);
}

if (bounced.Count() == 0)
{
break; //no more bounces to match!
}
}

所以我做了一些测试,它每秒处理大约 12 条发送的记录。超过 100 万条记录,处理时间将超过 25 小时!

两个问题:

  1. 我怎样才能准确找到最耗时的线路?
  2. 我假设是 lambda 表达式发现反弹花费的时间最长,因为在我把它放在那里之前它要快得多。我怎样才能加快速度?

谢谢!

编辑

---想法---

  1. 我刚刚想到的一个想法是像对退回邮件一样按日期对发送进行排序,这样通过退回邮件进行搜索会更有效率,因为提前发送也可能会遇到提前退回邮件。
  2. 我刚刚想到的另一个想法是并行运行其中的几个进程,尽管我不希望对这个简单的应用程序进行多线程处理。

最佳答案

我会相当有信心地说是的,正是您的发现正在花时间。

看起来您确定 find 方法将仅返回 0 或 1 条记录(而不是列表),在这种情况下,加快速度的方法是创建查找(字典)而不是为创建 List<event_data>您的反弹 var,改为创建一个 Dictionary<key, event_data>,然后您可以按键查找值而不是查找。

诀窍在于创建您的 key (我对您的应用了解不多,无法提供帮助),但本质上与您的查找条件相同。

编辑。 (添加一些伪代码)

void Main()
{
var hugeListOfEmails = GetHugeListOfEmails();
var allBouncedEmails = GetAllBouncedEmails();
IDictionary<string, EmailInfo> CreateLookupOfBouncedEmails = CreateLookupOfBouncedEmails(allBouncedEmails);

foreach(var info in hugeListOfEmails)
{
if(CreateLookupOfBouncedEmails.ContainsKey(info.emailAddress))
{
// Email is bounced;
}
else
{
// Email is not bounced
}
}

}

public IEnumerable<EmailInfo> GetHugeListOfEmails()
{
yield break;
}

public IEnumerable<EmailInfo> GetAllBouncedEmails()
{
yield break;
}

public IDictionary<string, EmailInfo> CreateLookupOfBouncedEmails(IEnumerable<EmailInfo> emailList)
{
var result = new Dictionary<string, EmailInfo>();
foreach(var e in emailList)
{
if(!result.ContainsKey(e.emailAddress))
{
if(//satisfies the date conditions)
{
result.Add(e.emailAddress, e);
}
}
}
return result;
}

public class EmailInfo
{
public string emailAddress { get; set; }
public DateTime DateSent { get; set; }
}

关于c# - 我怎样才能使这段代码更快? List<custom_class>().Find(lambda_expression),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16704689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com