Home » Nodejs » V8-like Hashtable for C#?

V8-like Hashtable for C#?

Posted by: admin January 30, 2018 Leave a comment

Questions:

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);
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.