为什么我的程序在分配更多线程后执行时间更长?

huangapple go评论95阅读模式
英文:

Why my programs is taking longer to execute with more threads allocated?

问题

我写了两个程序,使用黎曼和方法计算函数的面积,一个是用Go语言写的,另一个是用C++写的。

目标是测量执行时间,看哪种语言在多线程上更快。

我在一个32核的服务器上运行了这个程序(双路Intel Xeon),使用一个bash脚本来以1、2、4、8、16和32个线程运行它。脚本使用time --format %U来获取执行时间。

但是从结果中可以看出,使用1个核心运行Go版本需要1.19秒,而使用32个核心需要1.69秒!我原以为使用更多的核心会使计算更快...

我在编写程序时是否犯了错误?时间的测量准确吗?或者结果是好的,但是为什么呢?

非常感谢您提前的回答!

来源:

Go代码:https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/CalculGo/Calcul.go

C++代码:https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/CalculCpp/Calcul.cpp

启动脚本:https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/script/Launch.sh

黎曼和方法:http://mathworld.wolfram.com/RiemannSum.html

初步结果:https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/results/GoVSCpp.txt

英文:

I wrote two programs that computes area of a function with a certain number of rectangles using Riemann sums, one is written in Go and the other with C++.

The objective was to measure execution time and see which language is the faster on multithreading.

I ran the program on a 32-cores server (Dual Intel Xeon) using a bash script4 to run it with 1, 2, 4, 8, 16 and 32 threads. The script use time --format %U to get the execution time .

But as you can see in the results running the Go version using 1 core is 1.19 seconds and with 32 cores it's 1.69 seconds ! I thought using more cores would have made computations faster...

Did I made an error writing my programs ? Is the measure of time accurate ? Or maybe the results are good but how ?

Thank you in advance for your answers !

Sources :

Go code : https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/CalculGo/Calcul.go

C++ code : https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/CalculCpp/Calcul.cpp

launch script : https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/script/Launch.sh

Riemann sums : http://mathworld.wolfram.com/RiemannSum.html

first results : https://github.com/Mistermatt007/Benchmark-go-vs-cpp/blob/master/results/GoVSCpp.txt

答案1

得分: 9

根据man time的说明:

> U 进程直接使用的CPU秒数(在用户模式下),以秒为单位。

你正在测量CPU秒数,即每个CPU累积使用的时间,而不是“墙钟”秒数。这个度量不会随着额外的线程而减少,因为它与工作量成正比。另一方面,随着线程数量的增加,这个度量可能会增加,因为每个新线程都会产生一些额外的记录。

如果你想要列出“真实”时间,请使用%e格式说明符。

英文:

According to man time:

> U Total number of CPU-seconds that the process used directly (in user mode), in seconds.

You are measuring CPU-seconds, i.e. time spent by each CPU cumulatively, not "wallclock" seconds. This measure won't go down with additional threads, because it is proportional to amount of work which is constant. On the other hand, this may go up with number of threads, as every new thread incurs some additional bookkeeping.

If you want to list "real" time, use %e specifier.

huangapple
  • 本文由 发表于 2015年11月26日 17:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/33934841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定