6

我正在编写一个公寓和房屋租赁网站。由于出租的房产永远不会超过 10,000 个,因此将它们全部加载到内存中是没有问题的。现在,当用户想要搜索特定的一个时,他可以为价格、房间、自动扶梯等定义非常多的过滤器。

每个属性都有一组非常不同的属性。一个属性可能具有另一个属性没有的属性。因此,在 C# 中创建一个具有所有属性的类,而只使用其中的一些属性对我来说不是一个好主意。我决定改用字典。

一些基准测试之后,我发现,字典作为类访问属性的速度大约慢了 40 倍。我还为 node.js 做了一个基准测试,它只是将对象用作字典。这非常有趣,因为在 node.js 中完全相同的程序的性能甚至比带有原生类的 C# 示例还要好。

事实上,我得到了以下结果:

C# 字典:~820ms C# 类:~26ms Node.js 对象:~24ms

每个基准测试都使用相同的标准搜索 1'000'000 个对象。

我知道 Node.js 版本之所以这么快,是因为 Google 的 V8 引擎。你知道是否有一个 C# 类使用与 V8 引擎类似的技术并获得几乎相同的性能?

C# 字典基准测试

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# 类基准测试

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 基准测试

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

1 回答 1

4

好的,C# 变慢的原因是 V8 针对这种情况进行了优化(许多字典具有完全相同的成员)。

您在这里有点滥用 C#。而不是字典,只需使用具有自动属性的普通类。那时 C# 会快得多,甚至比 V8 还要快(因为你是在发挥它的优势而不是它的弱点)。

这就是为什么您的“特定对象”基准测试速度最快的原因。

于 2012-05-28T12:17:30.707 回答