Van*_*ing 6 javascript c# dictionary v8 node.js
我正在编制公寓和房屋租赁网站.由于租赁的属性从不超过10'000,因此将em全部加载到内存中是没有问题的.现在,当用户想要搜索特定的用户时,他可以为价格,房间,自动扶梯等定义非常多的过滤器.
每个属性都有一组非常不同的属性.一个属性可能具有另一个属性不具有的属性.因此,在C#中创建具有所有属性的类,而只使用其中的一些属性对我来说不是一个好主意.我决定改用字典.
稍后我发现,一些基准测试,字典作为一个类访问属性的速度要快40倍.我还为node.js做了一个基准测试,它只使用了对象作为dictionarys.这非常有趣,因为node.js中完全相同的程序比使用本机类的C#示例表现得更好.
事实上我得到了以下结果:
C#字典:~820ms C#类:~26ms Node.js对象:~24ms
每个基准测试通过相同的标准搜索1'000'000个对象.
我知道Node.js的版本是那么快,因为谷歌的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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
好吧,C# 速度较慢的原因是 V8 专门针对这种情况进行了优化(许多字典具有完全相同的成员)。
您在这里有点滥用 C#。只需使用具有自动属性的普通类,而不是字典。那么 C# 将会快得多,甚至比 V8 还要快(因为你正在发挥它的优势而不是它的弱点)。
这就是为什么你的“特定对象”基准测试是最快的。
归档时间: |
|
查看次数: |
493 次 |
最近记录: |