欢迎访问联博统计,联博统计是用以太坊区块链的高度为数据统计!

足球免费贴士:关于c++、go、nodejs、python的计算性能测试,结果令人惊讶

1个月前39℃

计算性能在计算密集型的服务上,是非常重要的, 一直以为,在计算性能上,肯定是C++  > go > nodejs >= python

但测试结果却让人大跌眼镜!!!

实际的结果是:

go > nodejs > c++ > python

 

各语言同样逻辑下的运行结果,如下:

其中, ./t是go编译的程序, ./a.out是c++编译的程序, nodejs和python直接跑脚本

不用关注target size的内容,这个是验证结果一致的,保证算法是一致

主要看use time, 单位是秒:

这个结果很奇妙,反映出在计算密集的场景下,C++并非想象中那么快,而nodejs表现却非常亮眼 

难道是我的代码问题?各位看官看看有没办办法优化性能的?

 

相关的编译器、执行器版本如下:

,

足球免费贴士www.hgbbs.vip)是国内最权威的足球赛事报道、预测平台。免费提供赛事直播,免费足球贴士,免费足球推介,免费专家贴士,免费足球推荐,最专业的足球心水网。

,

go: 1.15.2

g++:  4.8.2

nodejs: 14.18.0

python:3.7.3 

 

各语言的测试代码如下, 计算逻辑是完全一致的:

Go:

package main;

import "fmt"
import "time"


type Data struct {
    x float64
    y float64
    a int
}

func MakeData(num int) []Data {
    var vec = make([]Data, 0, num)
    for i:=0; i< num; i++ {
        var data Data
        data.x = float64(i) + 0.5;
        data.y = float64(i) + 1.5;
        data.a = i;
        vec = append(vec, data)
    }
    return vec
}

func Cal(data []Data, idx int, num int) float64 {
    var sum1 float64 = 0.0;
    var sum2 float64 = 0.0;
    for i:= idx-num+1; i <= idx; i++ {
        if i <0 {
            continue;
        }
        var elem = data[i];
        sum1 += elem.x;
        sum2 += elem.y;
    }

    var avg1 = sum1/float64(num);
    var avg2 = sum2/float64(num);

    return (avg1 + avg2)/2;
}

func Make(data []Data) {
    var target = make([]float64, 0, len(data));
    for i := 0; i < len(data); i++ {
        var v = Cal(data, i, 1000);
        if v > 1000 {
            target = append(target, v)
        }
    }
    fmt.Println("target size:" , len(target))
}

            
func main() {
    var t1 = time.Now().UnixNano()
    var data = MakeData(300*365*5);
    Make(data);
    var t2 = time.Now().UnixNano()
    fmt.Println("use time:", float64(t2-t1)/1000000000)
}

 

C++:

#include <stdio.h>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <unistd.h>
#include <sys/time.h>

struct Data {
    double x;
    double y;
    int    a;
};

std::vector<Data> MakeData(int num) {
    std::vector<Data> vec;
    vec.reserve(num);
    for (int i=0; i< num; i++) {
        Data data;
        data.x = static_cast<double>(i) + 0.5;
        data.y = static_cast<double>(i) + 1.5;
        data.a = i;
        vec.push_back(std::move(data));
    }
    return std::move(vec);
}

double Cal(std::vector<Data> & data, int idx, int num) {
    double sum1 = 0.0;
    double sum2 = 0.0;
    for (int i = idx-num+1; i <= idx; i++) {
        if (i <0) {
            continue;
        }
        auto & elem = data[i];
        sum1 += elem.x;
        sum2 += elem.y;
    }

    auto avg1 =sum1/num;
    auto avg2 =sum2/num;

    return (avg1 + avg2)/2;
}

void Make(std::vector<Data> & data) {
    std::vector<double> target;
    target.reserve(data.size());
    for (int i = 0; i < data.size(); i++) {
        auto v = Cal(data, i, 1000);
        if (v > 1000) {
            target.push_back(v);
        }
    }
    std::cout << "target size:" << target.size() << std::endl;
}
            
int main(int argc,char** argv)
{
    struct timeval t1;
    struct timeval t2;
    gettimeofday(&t1, NULL);
    auto data = MakeData(300*365*5);
    Make(data);
    gettimeofday(&t2, NULL);
    auto usetime = double((t2.tv_sec*1000000 + t2.tv_usec) - (t1.tv_sec*1000000 + t1.tv_usec))/1000000;
    std::cout <<"use time: " << usetime << std::endl;
}

 

NodeJs:

class Data {
    constructor() {
        this.x = 0.0;
        this.y = 0.0;
        this.a = 0;
    }
};


function MakeData(num) {
    let vec = [];
    for (let i=0; i< num; i++) {
        let data = new Data();
        data.x = i + 0.5;
        data.y = i + 1.5;
        data.a = i;
        vec.push(data);
    }
    return vec;
}



function Cal(data, idx, num) {
    let sum1 = 0.0;
    let sum2 = 0.0;
    for (let i = idx-num+1; i <= idx; i++) {
        if (i <0) {
            continue;
        }
        let elem = data[i];
        sum1 += elem.x;
        sum2 += elem.y;
    }

    let avg1 =sum1/num;
    let avg2 =sum2/num;

    return (avg1 + avg2)/2;
}


function Make(data) {
    let target = [];
    for (let i = 0; i < data.length; i++) {
        let v = Cal(data, i, 1000);
        if (v > 1000) {
            target.push(v);
        }
    }
    console.log("target size:", target.length);
}
            
t1 = new Date().getTime();
let data = MakeData(300*365*5);
Make(data);
t2= new Date().getTime();
console.log("use time:", (t2-t1)/1000)

 

Python:

import time

class Data:
    def __init__(self):
        self.x = 0.0
        self.y = 0.0
        self.a = 0


def MakeData(num):
    vec = []
    for i in range(0, num):
        data = Data()
        data.x = i + 0.5
        data.y = i + 1.5
        data.a = i
        vec.append(data)
    return vec



def  Cal(data, idx, num):
    sum1 = 0.0
    sum2 = 0.0
    i = idx-num+1
    while i<=idx:
        if i <0:
            i+=1
            continue
        elem = data[i]
        sum1 += elem.x
        sum2 += elem.y
        i+=1

    avg1 =sum1/num
    avg2 =sum2/num

    return (avg1 + avg2)/2


def Make(data):
    target = []
    data_len = len(data)
    for i in range(0, data_len): 
        v = Cal(data, i, 1000)
        if v > 1000: 
            target.append(v)
    print("target size:" , len(target))
            

t1=time.time()
data = MakeData(300*365*5)
Make(data)
print("use time:", time.time() -  t1)

 

上一篇:新2会员手机管{guan}理端:【伊{yi}朗超19:30阿拉{la}克铝业vs高尔高赫】

上一篇:手机 ji[新2管理端:甘肃『su』省整体通过县域义务教育基 ji[本均「jun」衡发展国家实地督导《dao》检查

猜你喜欢

请发表您的评论

最新文章
热门文章
热评文章
随机文章
热门标签