Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

V8-like Hashtable for C#?

I'm programming an apartment & house rental site. Since there are never more than 10'000 properties for rent, it's no problem to load em all into memory. Now, when a users want to search for a specific one, he can define very much filters for price, room, escalator etc.

Every property has a very different set of attributes. One property may have an attribute that another property does not have. So, creating a Class in C# that has all the attributes, while only a few of them are used is not a good idea to me. I decided to use a Dictionary instead.

A few benchmarks later, I found out, that the Dictionary is about 40 times slower in accessing attributes as a Class. I also did a benchmark for node.js, which just used objects as dictionarys. This was absolutely interesting because the exact same program in node.js performed even better than the C# example with a native class.

In fact I got the following results:

C# Dictionary: ~820ms C# Class: ~26ms Node.js Object: ~24ms

Each benchmark searched 1'000'000 objects by the same criterias.

I know that the Node.js version is that fast because of the V8 engine by Google. Do you know if there is a C# class that uses similar techniques as the V8 engine and gets almost the same performance?

C# Dictionary Benchmark

namespace Test {
    class Program {
        static void Main(string[] args) {

            PropertyList p = new PropertyList();
            long startTime = DateTime.Now.Ticks;
            for (int i = 0; i < 100; i++) {
                p.Search();
            }
            Console.WriteLine((DateTime.Now.Ticks - startTime) / 10000);
        }
    }

    class PropertyList {
        List<Property> properties = new List<Property>();
        public PropertyList() {
            for (int i = 0; i < 10000; i++) {
                Property p = new Property();
                p["Strasse"] = "Oberdorfstrasse";
                p["StrassenNr"] = 6;
                p["Plz"] = 6277;
                p["Ort"] = "Lieli";
                p["Preis"] = 600;
                p["Fläche"] = 70;
                p["Zimmer"] = 2;
                p["Lift"] = true;
                p["Verfügbarkeit"] = 7;
                p["Keller"] = false;
                p["Neubau"] = true;
                p["ÖV"] = false;

                properties.Add(p);
            }
        }
        public void Search() {
            int found = 0;

            for (int i = 0; i < properties.Count; i++) {
                Property p = properties[i];
                if ((string)p["Strasse"] == "Oberdorfstrasse" &&
                   (int)p["StrassenNr"] == 6 &&
                   (int)p["Plz"] == 6277 &&
                   (string)p["Ort"] == "Lieli" &&
                   (int)p["Preis"] >= 500 && (int)p["Preis"] <= 1000 &&
                   (int)p["Fläche"] >= 10 && (int)p["Fläche"] <= 200 &&
                   (int)p["Zimmer"] == 2 &&
                   (bool)p["Lift"] == true &&
                   (int)p["Verfügbarkeit"] >= 2 && (int)p["Verfügbarkeit"] <= 8 &&
                   (bool)p["Keller"] == false &&
                   (bool)p["Neubau"] == true &&
                   (bool)p["ÖV"] == true
                ) {
                    found++;
                }
            }
        }
    }

    class Property {
        private Dictionary<string, object> values = new Dictionary<string, object>();

        public object this[string key] {
            get {
                return values[key];
            }
            set {
                values[key] = value;
            }
        }
    }
}

C# Class Benchmark

namespace Test {
    class Program {
        static void Main(string[] args) {

            SpecificPropertyList p2 = new SpecificPropertyList();

            long startTime2 = DateTime.Now.Ticks;
            for (int i = 0; i < 100; i++) {
                p2.Search();
            }

            Console.WriteLine((DateTime.Now.Ticks - startTime2) / 10000);

        }
    }

    class SpecificPropertyList {
        List<SpecificProperty> properties = new List<SpecificProperty>();
        public SpecificPropertyList() {
            for (int i = 0; i < 10000; i++) {
                SpecificProperty p = new SpecificProperty();
                p.Strasse = "Oberdorfstrasse";
                p.StrassenNr = 6;
                p.Plz = 6277;
                p.Ort = "Lieli";
                p.Preis = 600;
                p.Fläche = 70;
                p.Zimmer = 2;
                p.Lift = true;
                p.Verfügbarkeit = 7;
                p.Keller = false;
                p.Neubau = true;
                p.ÖV = false;

                properties.Add(p);
            }
        }
        public void Search() {
            int found = 0;

            for (int i = 0; i < properties.Count; i++) {
                SpecificProperty p = properties[i];
                if (p.Strasse == "Oberdorfstrasse" &&
                   p.StrassenNr == 6 &&
                   p.Plz == 6277 &&
                   p.Ort == "Lieli" &&
                   p.Preis >= 500 && p.Preis <= 1000 &&
                   p.Fläche >= 10 && p.Fläche <= 200 &&
                   p.Zimmer == 2 &&
                   p.Lift == true &&
                   p.Verfügbarkeit >= 2 && p.Verfügbarkeit <= 8 &&
                   p.Keller == false &&
                   p.Neubau == true &&
                   p.ÖV == true
                ) {
                    found++;
                }
            }
        }
    }

    class SpecificProperty {
        public string Strasse;
        public int StrassenNr;
        public int Plz;
        public string Ort;
        public int Preis;
        public int Fläche;
        public int Zimmer;
        public bool Lift;
        public int Verfügbarkeit;
        public bool Keller;
        public bool Neubau;
        public bool ÖV;
    }
}

Node.js Benchmark

var properties = [];

for(var i = 0; i < 10000; i++){
    var p = {
        Strasse:"Oberdorfstrasse",
        StrassenNr:6,
        Plz:6277,
        Ort:"Lieli",
        Preis:600,
        Fläche:70,
        Zimmer:2,
        Lift:true,
        Verfügbarkeit:7,
        Keller:false,
        Neubau:true,
        ÖV:false
    };
    properties.push(p);
}



function search(){
    var found = 0;
    for(var i = 0; i < properties.length; i++){
        var p = properties[i];
        if(p.Strasse == "Oberdorfstrasse" && p.StrassenNr == 6 && p.Plz == 6277 && p.Ort == "Lieli" &&
            p.Preis >= 500 && p.Preis <= 1000 &&
            p.Fläche>= 10 && p.Fläche <= 100 &&
            p.Zimmer == 2 &&
            p.Verfügbarkeit >= 2 && p.Verfügbarkeit <= 8 &&
            p.Keller == false && p.Neubau == true && p.ÖV == false
        ){
            found++;
        }
    }
}
var startTime = new Date().getTime();
for(var i = 0; i < 100; i++){
    search();
}
console.log(new Date().getTime()-startTime);
like image 616
Van Coding Avatar asked May 28 '12 11:05

Van Coding


People also ask

Is there a hash table in C?

A Hash Table in C/C++ (Associative array) is a data structure that maps keys to values. This uses a hash function to compute indexes for a key. Based on the Hash Table index, we can store the value at the appropriate location.

Is Hashtable faster than array?

Hash tables tend to be faster when it comes to searching for items. In arrays, you have to loop over all items before you find what you are looking for while in a hash table you go directly to the location of the item. Inserting an item is also faster in Hash tables since you just hash the key and insert it.

Why is Hashtable performance slow?

Hash tables in general exhibit poor locality of reference—that is, the data to be accessed is distributed seemingly at random in memory. Because hash tables cause access patterns that jump around, this can trigger microprocessor cache misses that cause long delays.

Is Hashtable fast?

Hash tables are useful because they are fast. The theoretical average running time for find , insert , and erase is the optimal O(1) — meaning no matter how big the hash table gets, the average number of steps needed to perform those operations on any hypothetical computer has a fixed limit.


1 Answers

Ok, the reason for C# being slower is that V8 is optimized for exactly this scenario (lots of dictionaries having exactly the same members).

You are kind of misusing C# here. Instead of a dictionary, just use a normal class with auto-properties. C# will be much faster then, even faster than V8 (because you are playing to its strength and not to its weakness).

And that is why your "specific object" benchmark is the fastest.

like image 188
usr Avatar answered Oct 06 '22 01:10

usr