views:

25

answers:

1

Hey guys,

In my application, i need to show made calls to the user. The user can arrange some filters, according to what they want to see. The problem is that i find it quite hard to filter the calls without losing performance. This is what i am using now :

private void ProcessFilterChoice()
    {
        _filteredCalls = ServiceConnector.ServiceConnector.SingletonServiceConnector.Proxy.GetAllCalls().ToList();
        if (cboOutgoingIncoming.SelectedIndex > -1)
            GetFilterPartOutgoingIncoming();
        if (cboInternExtern.SelectedIndex > -1)
            GetFilterPartInternExtern();
        if (cboDateFilter.SelectedIndex > -1)
            GetFilteredCallsByDate();
        wbPdf.Source = null;
        btnPrint.Content = "Pdf preview";
    }
    private void GetFilterPartOutgoingIncoming()
    {
        if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Outgoing"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
        else if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Incoming"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Called.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
    }
    private void GetFilterPartInternExtern()
    {
        if (cboInternExtern.SelectedItem.ToString().Equals("Intern"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
        else if (cboInternExtern.SelectedItem.ToString().Equals("Extern"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if ((_filteredCalls[i].Called.E164.Length < 5 && _filteredCalls[i].Caller.E164.Length < 5) || _filteredCalls[i].Called.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
    }
    private void GetFilteredCallsByDate()
    {
        DateTime period = DateTime.Now;
        switch (cboDateFilter.SelectedItem.ToString())
        {
            case "Today":
                period = DateTime.Today;
                break;
            case "Last week":
                period = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
                break;
            case "Last month":
                period = DateTime.Today.AddMonths(-1);
                break;
            case "Last year":
                period = DateTime.Today.AddYears(-1);
                break;
            default:
                return;
        }
        for (int i = _filteredCalls.Count - 1; i > -1; i--)
        {
            if (_filteredCalls[i].Start < period)
                _filteredCalls.RemoveAt(i);
        }
    }

_filtered calls is a list of "calls". Calls is a class that looks like this :

[DataContract]
public class Call
{
    private User caller, called;
    private DateTime start, end;
    private string conferenceId;
    private int id;
    private bool isNew = false;

    [DataMember]
    public bool IsNew
    {
        get { return isNew; }
        set { isNew = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string ConferenceId
    {
        get { return conferenceId; }
        set { conferenceId = value; }
    }
    [DataMember]
    public DateTime End
    {
        get { return end; }
        set { end = value; }
    }
    [DataMember]
    public DateTime Start
    {
        get { return start; }
        set { start = value; }
    }
    [DataMember]
    public User Called
    {
        get { return called; }
        set { called = value; }
    }
    [DataMember]
    public User Caller
    {
        get { return caller; }
        set { caller = value; }
    }

Can anyone direct me to a better solution or make some suggestions.

+1  A: 

Try using a foreach loop instead of an explicit for loop with an iterator variable. If _filteredCalls is a List, then _filteredCalls[i] will take O(n) time. Using a foreach will use the built in iterator of the list and will likely get you a speed increase.

Edit: I see you are actually looping backward through _filteredCalls, and also modifying the list.

So something like this would be better:

_filteredCalls = _filteredCalls.Where(c => c.Caller.E164.Length > 4 || c.Caller.E164.Equals("0"))
Matt Bridges
thought of that, but then i am using 2 loops and thought it'd take even more time to run it.
djerry
you're edit was helpfullwhen debugging i also noticed something painfull : i called the entire process 3-4 times (due to combobox selection changing )major performance update now, nice programming line, tnx matt
djerry