2017-06-17 47 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.也許你可以添加它?

相關問題