0

我正在使用 iPhone APP 获取重复的电话号码,我已经编写了代码,但性能太差而且需要时间。这是我的代码:

    OrderedDictionary persons = new OrderedDictionary ();
    ABAddressBook ab = new ABAddressBook ();

    foreach (ABPerson p in ab.GetPeople()) {

            foreach (var phoneNumber in p.GetPhones().GetValues()) {
                var duplicates = SearchByPhoneNumber (ab, phoneNumber);
                if (duplicates.Count > 1) {
                    if (!persons.Contains (phoneNumber)) {
                        persons.Add (phoneNumber, duplicates);
                    }
                }

            }
        }


    List<ABPerson> SearchByPhoneNumber ( ABAddressBook ab, string phoneNumber)
{
        List<ABPerson> duplicatepeople = new List<ABPerson> ();
        phoneNumber = Regex.Replace (phoneNumber, "[^0-9]", "");


        var people = ab.Where(x=> x is ABPerson).Cast<ABPerson>().Where(x=> x.GetPhones()
                                                                        .Where(p=> Regex.Replace(p.Value,"[^0-9]", "")==phoneNumber || phoneNumber ==Regex.Replace(p.Value,"[^0-9]", "")).Count() > 0).ToArray();

        foreach(ABPerson person in people)
        {
            if( duplicatepeople.Intersect(person.GetRelatedNames().Cast<ABPerson>()).Count() <= 0)
            {
                duplicatepeople.Add(person);
            }

        }

        return duplicatepeople;
    }
4

1 回答 1

0

我没有时间检查两者之间的时间,但我确实注意到您正在多次迭代通讯簿号码(对于每个电话号码,您使用每个 .Where 再次迭代列表)。此外,我注意到您在循环中多次为每个电话号码执行相同的正则表达式。

这可能不完全符合您的需求,但它确实在我的通讯录中找到了重复项,而且速度不算太慢。

ABAddressBook ab = new ABAddressBook();

// flatten out the list of all phone numbers and perform the regEx just once for each phone number
var processedList = new List<Tuple<string, ABPerson>>();
foreach (ABPerson p in ab.GetPeople()) 
{
    foreach (var phoneNumber in p.GetPhones().GetValues()) 
    {
        processedList.Add(new Tuple<string, ABPerson>(Regex.Replace (phoneNumber, "[^0-9]", ""), p));
    }
}

var duplicates = (from person in processedList
    group person by person.Item1 into g
    where g.Count() > 1
    select new { PhoneNumber = g.Key, Person = g }).ToList();

foreach (var d in duplicates)
{
    Console.WriteLine("{0}  {1}", d.PhoneNumber, d.Person.Count());
    foreach (var p in d.Person)
    {
        Console.WriteLine(p.Item2.FirstName);
    }
}

我希望这有帮助。

于 2013-02-10T12:38:27.973 回答