为什么我的D代码不像预期的那么高性能?

我正在为自己的乐趣做一个基准测试! 我用许多编程语言编写了一部分代码,并使用ab进行基准测试,以查看哪个更快,更快。 我知道这个方法可能不是那么有效,不能用作一些明显的用法,但是为了我自己的信息,我正在这样做。 我想知道的另一个因素是在每种语言中编写相同的样本是多么容易/困难。 我用Python / Python(asyncio),Haskell,Go,Kotlin和D编写了代码。我认为D端口比Go更快(或者至少等于速度)。 但不幸的是我的D代码比Go慢得多。 在这里,我把其他代码,请帮助我为什么代码不如预期的那么快。 或者我的期望绝对错了?

import cbor; import std.array : appender; import std.format; import std.json; import vibe.vibe; struct Location { float latitude; float longitude; float altitude; float bearing; } RedisClient redis; void main() { auto settings = new HTTPServerSettings; redis = connectRedis("localhost", 6379); settings.port = 8080; settings.bindAddresses = ["::1", "127.0.0.1"]; listenHTTP(settings, &hello); logInfo("Please open http://127.0.0.1:8080/ in your browser."); runApplication(); } void hello(HTTPServerRequest req, HTTPServerResponse res) { if (req.path == "/locations") { immutable auto data = req.json; immutable auto loc = deserializeJson!Location(data); auto buffer = appender!(ubyte[])(); encodeCborAggregate!(Flag!"WithFieldName".yes)(buffer, loc); auto db = redis.getDatabase(0); db.set("Vehicle", cast(string) buffer.data); res.writeBody("Ok"); } } 

这就是Go

 package main import ( "github.com/kataras/iris" "github.com/kataras/iris/context" ) import "github.com/go-redis/redis" import ( "bytes" "github.com/2tvenom/cbor" ) type Location struct { Latitude float32 `json:"latitude"` Longitude float32 `json:"longitude"` Altitude float32 `json:"altitude"` Bearing float32 `json:"bearing"` } func main() { app := iris.New() client := redis.NewClient(&redis.Options{Addr: "localhost:6379"}) app.Post("/locations", func(ctx context.Context) { var loc Location ctx.ReadJSON(&loc) var buffTest bytes.Buffer encoder := cbor.NewEncoder(&buffTest) encoder.Marshal(loc) client.Set("vehicle", buffTest.Bytes(), 0) client.Close() ctx.Writef("ok") }) app.Run(iris.Addr(":8080"), iris.WithCharset("UTF-8")) } 

使用ab,Go的结果约为4200 req / sec,而D约为2800 req / sec!

你不仅仅是Go和D的基准测试。你还可以针对你的非标准Go和D库的特定选择进行基准测试: cborvibeiris等等。而且你正在基准化你的特定实现,以1000倍的表现 。

有了这么多的variables,原始的基准数字对于比较两种语言的性能来说是没有意义的。 这些第三方库中的任何一个都可能导致性能问题。 真的,你只是比较这两个特定的程序。 这是试图比较不同语言中不重要的程序的核心问题:variables太多了。


您可以通过性能分析来降低其中一些variables的影响; 在Go这将是go tool pprof 。 这将告诉你什么function和线路被称为多少次,并采取多少资源。 有了这个,你可以find瓶颈,在耗费大量资源的代码中,并在那里集中优化工作。

在为每个版本进行配置文件和优化时,您将比较真实的优化实现。 或者,您将更好地了解每种语言和图书馆的有效性,以及不了解的内容。


比较语言的问题很大程度上受到特定问题和特定程序员的影响。 X程序员总是认为X是最好的语言,不是因为X是最好的语言,而是因为在X中编写X程序员时他们是最好的,他们可能选择了一个他们喜欢的问题。 正因为如此,有许多项目可以将每种语言的最佳实现集中在一起。

立即想到的是计算机语言基准游戏 。 他们去做,但不是D.也许你可以添加它?

Interesting Posts