2017-06-17 59 views
-3

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

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"); 
    } 
} 

这里是围棋

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,进去约4200请求/秒的结果,而d约2800请求/秒!

+1

我没有看到任何明显的性能问题,但很难说,因为您的示例代码不可编译,因此我无法对其进行配置。有什么办法可以将它转换成http://www.sscce.org/?编辑:忘了说,但也请包括有关您使用什么编译器和在什么设置,这可能会导致巨大的差异的信息。 – cym13

+0

我正在使用默认的D anf Go编译器!我的意思是最新版本的配音和Google Go!这些要求只是2个包: \t“依赖性”:{ \t“vibe-d”:“〜> 0.7.30”, “cbor-d”:“〜> 0.5。4" \t}, – Kamyar

+1

什么表现的结果是,你得到什么?你期待什么呢? –

回答

6

您不仅仅是Go与D的基准测试。您还可以针对您选择的非标准Go和D库进行基准测试:cbor,vibe,iris等。您正在基准测试您的特定实现which can easily vary by 1000x in performance

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


您可以通过performance profiling减少其中一些变量的影响;在去这将是go tool pprof。这将告诉你什么功能和线路被称为多少次,并考虑多少资源。有了这个,你可以找到瓶颈,在代码中占据大量资源的位置,并在那里集中优化工作。

当您为每个版本进行配置文件和优化轮次时,您将会比较真实的优化实现。或者你将更好地理解每种语言和图书馆的有效性,以及它们不具备的功能。


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

立即想到的是The Computer Language Benchmarks Game。他们做Go,但不是D.也许你可以添加它?

相关问题