关于c++、go、nodejs、python的计算性能测试,结果令人惊讶
计算性能在计算密集型的服务上,是非常重要的, 一直以为,在计算性能上,肯定是C++ > go > nodejs >= python
但测试结果却让人大跌眼镜!!!
实际的结果是:
go > nodejs > c++ > python
各语言同样逻辑下的运行结果,如下:
其中, ./t是go编译的程序, ./a.out是c++编译的程序, nodejs和python直接跑脚本
不用关注target size的内容,这个是验证结果一致的,保证算法是一致
主要看use time, 单位是秒:
这个结果很奇妙,反映出在计算密集的场景下,C++并非想象中那么快,而nodejs表现却非常亮眼
难道是我的代码问题?各位看官看看有没办办法优化性能的?
相关的编译器、执行器版本如下:
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#include #include #include #include <string> #include #include struct Data { double x; double y; int a; }; std::vector MakeData(int num) { std::vector 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, 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) { 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)