none
c# Performance question. RRS feed

  • Question

  • I've created a class that holds two dictionaries and it also has a nested class inside called a Group, and a struct called an Entry like this:

    public class Cache
        {
            private struct Entry
            {
                private string _Name;
                private object _Value;
    
                public string Name
                { get { return _Name; } }
                public object Value
                { get { return _Value; } }
    
                public Entry(string Name, object Value)
                {
                    _Name = Name;
                    _Value = Value;
                }
            }
            private class Group
            {
                private Dictionary<string, Group> _ChildGroups;
                private Dictionary<string, Entry> _ChildEntries;
    
                private string _Name;
    
                private int _ChildGroupsCount;
                private int _ChildEntriesCount;
    
                public string Name
                { get { return _Name; } }
    
                public int ChildGroupsCount
                { get { return _ChildGroupsCount; } }
                public int ChildEntriesCount
                { get { return _ChildEntriesCount; } }
    
                public Group(string Name)
                {
                    _ChildGroups = new Dictionary<string, Group>();
                    _ChildEntries = new Dictionary<string, Entry>();
    
                    _Name = Name;
                }
                public void AddChildGroup(string Name)
                {
                    if (_ChildGroups.ContainsKey(Name))
                        throw new Exception();
    
                    _ChildGroups.Add(Name, new Group(Name));
                    _ChildGroupsCount++;
                }
                public void AddChildEntry(string Name, object Value)
                {
                    if (_ChildEntries.ContainsKey(Name))
                        throw new Exception();
    
                    _ChildEntries.Add(Name, new Entry(Name, Value));
                    _ChildEntriesCount++;
                }
                public void RemoveChild(string Name, int SearchTypeID)
                {
                    if (SearchTypeID == 0)
                    {
                        if (!_ChildGroups.ContainsKey(Name))
                            throw new Exception();
    
                        _ChildGroups.Remove(Name);
                    }
                    else
                    if (SearchTypeID == 1)
                    {
                        if (!_ChildEntries.ContainsKey(Name))
                            throw new Exception();
    
                        _ChildEntries.Remove(Name);
                    }
                    else throw new Exception();
                }
                public bool ChildExists(string Name, int SearchTypeID)
                {
                    if (SearchTypeID == 0)
                    {
                        if (_ChildGroups.ContainsKey(Name)) return true;
                        else return false;
                    }
                    else
                    if (SearchTypeID == 1)
                    {
                        if (_ChildEntries.ContainsKey(Name)) return true;
                        else return false;
                    }
                    else throw new Exception();
                }
    
                public object this[string Name, int SearchTypeID]
                {
                    get
                    {
                        if (SearchTypeID == 0)
                        {
                            if (!_ChildGroups.TryGetValue(Name, out Group Child))
                                throw new Exception();
    
                            return Child;
                        }
                        else
                        if (SearchTypeID == 1)
                        {
                            if (!_ChildEntries.TryGetValue(Name, out Entry Child))
                                throw new Exception();
    
                            return Child;
                        }
                        else throw new Exception();
                    }
                }
            }
            private Dictionary<string, Group> _ROOTGROUPS;
            private Dictionary<string, Entry> _ROOTENTRIES;
    
            public Cache()
            {
                _ROOTGROUPS = new Dictionary<string, Group>();
                _ROOTENTRIES = new Dictionary<string, Entry>();
            }
        }
     

    Just as a quick background, the group allows dynamic nesting of another group or an entry using dictionaries in the group class. But the problem is when I do a simple:

    Cache Cache = new Cache();

    The time of initializing takes anywhere from 2 to 3 milliseconds. Considering the only thing that should be happening is creating two new dictionary instances for _ROOTGROUPS and _ROOTENTRIES, I don't understand why it is taking so long. Shouldn't it take under a millisecond? This is really baffling to me. PS: I am using a release build and using a stopwatch to benchmark.

    Monday, February 12, 2018 8:54 PM

All replies

  • Hello,

     Why is small amount of time an issue? Does your app, desktop, OS, computer become unresponsive?

    Just my opinion but the time is meaningless for CPU capabilities even on Windows XP. About the only

    time I seen delays, as in seconds not milliseconds, is in the UI Thread doing too much work. When a

    Background Thread should have been used instead.  As for using the same CPU to measure time while

    doing the work that is measured... Well, that is not accurate but will give a broad measurement.

     

     Sorry :)

     

     

    Tuesday, February 13, 2018 1:17 AM