4 回答

TA贡献1871条经验 获得超8个赞
(根据上述答案的提示,我做了更多测试,并添加了一个C
版本)
在我的 linux 机器上,times = 100000000。
测试结果:
当指数 = 2.4
Java: result: 1.053906e+24, during: 7432 ms C: result: 1.053906e+24, during: 5544 ms Go: result: 1.053906e+24, during: 8.716807708s
当 exponent = 2时,仍然使用
pow()
orPow()
。
Java: result: 1.543194e+21, during: 630 ms C: result: 1.543194e+21, during: 852 ms Go: result: 1.543194e+21, during: 3.336549272s
当 exponent = 2时,而是使用
x * x
。
Java: result: 1.543194e+21, during: 636 ms C: result: 1.543194e+21, during: 340 ms Go: result: 1.543194e+21, during: 115.491272ms
概括:
总的来说,
go
是真的快,根据上次的测试,比Java
,甚至还要快C
。但是,根据前 2 个测试,
Java
确实有一个很好的实现。pow()
代码
Test.java:
/**
* Compile:
* javac Test.java
* Run:
* java Test
*/
public class Test {
public static void main(String[] args) {
Test test = new Test();
long startAt = System.currentTimeMillis();
double re = test.testFun(10, 16666611, 100000000);
long during = System.currentTimeMillis() - startAt;
System.out.printf("%10s: result: %e, during: %d ms\n", "Java", re, during);
}
private double testFun(double first, double second, long times) {
int i = 0;
double result = 0;
double dx = (second - first) / times;
for (; i < times; i++) {
result += fun(first + i * dx);
}
return result * dx;
}
private double fun(double v) {
return v * v - v;
// return Math.pow(v, 2) - v;
// return Math.pow(v, 2.4) - v;
}
}
测试.c:
/**
* compile with:
* gcc test.c -lm
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
double fun(double v) {
return v * v - v;
// return pow(v, 2) - v;
// return pow(v, 2.4) - v;
}
double testFun(double first, double second, long times) {
int i;
double result = 0;
double dx = (second - first) / times;
for (i = 0; i < times; i++) {
result += fun(first + i * dx);
}
return result * dx;
}
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds =
te.tv_sec * 1000LL + te.tv_usec / 1000; // calculate milliseconds
// printf("milliseconds: %lld\n", milliseconds);
return milliseconds;
}
int main(int argc, char *argv[]) {
long long startAt = current_timestamp();
double re = testFun(10, 16666611, 100000000);
long long during = current_timestamp() - startAt;
printf("%10s: result: %e, during: %lld ms\n", "C", re, during);
return 0;
}
测试去:
/**
* How to run:
* go run test.go
*/
package main
import (
"fmt"
"math"
"time"
)
func main() {
startAt := time.Now()
result := testFun(10, 16666611, 100000000)
during := time.Since(startAt)
fmt.Printf("%10s: result: %e, during: %v\n", "Go", result, during)
_ = math.Pow
}
func fun(x float64) float64 {
return x*x - x
// return math.Pow(x, 2) - x
// return math.Pow(x, 2.4) - x
}
func testFun(first float64, second float64, times int) float64 {
var i = 0
var result float64 = 0
var dx float64
dx = (second - first) / float64(times)
for ; i < times; i++ {
result += fun(first + float64(i)*dx)
}
return result * dx
}
编译:
javac Test.java; gcc test.c -lm; go build test.go
跑步:
java Test; ./a.out ; ./test
@Update - 带有-O2
或-O3
选项的 C 程序
正如Raffaello
评论中所建议的那样,在编译C程序时,可以使用-O2
或-O3
进一步优化程序。
并且,以下是程序的测试结果C
:
当指数 = 2.4时。
C: result: 1.543194e+21, during: 5805 ms C with `-O2`: result: 1.543194e+21, during: 5324 ms C with `-O3`: result: 1.543194e+21, during: 5326 ms
当 exponent = 2时,仍然使用
pow()
orPow()
。
C: result: 1.543194e+21, during: 897 ms C with `-O2`: result: 1.543194e+21, during: 119 ms C with `-O3`: result: 1.543194e+21, during: 121 ms
当 exponent = 2时,而是使用
x * x
。
C: result: 1.543194e+21, during: 353 ms C with `-O2`: result: 1.543194e+21, during: 122 ms C with `-O3`: result: 1.543194e+21, during: 119 ms
摘要-(-O2
和-O3
选项):
有
-O2
和-O3
选项当 base 是整数(例如 2)时,将使 c 程序更快,快几倍。
当 base 是浮点数(例如 2.4)时,它也更快,但非常小。
比较
-O2
和-O3
,它们在上述测试中非常接近。

TA贡献1942条经验 获得超3个赞
不要从另一种语言翻译。在 Go 中编写程序的 Go 版本。例如x*x - x,
package main
import (
"fmt"
"math"
"time"
)
func main() {
start := time.Now()
v := testFun(10, 16666611, 1000000000)
since := time.Since(start)
fmt.Printf("value is %v\ntime is %v\n", v, since)
}
func fun(x float64) float64 {
return x*x - x
}
func testFun(first float64, second float64, times int) float64 {
sum := float64(0)
dx := (second - first) / float64(times)
for i := 0; i < times; i++ {
sum += fun(first + float64(i)*dx)
}
return sum * dx
}
输出:
$ go version
go version devel +5c11480631 Fri Aug 10 20:02:31 2018 +0000 linux/amd64
$ go run speed.go
value is 1.543194272428967e+21
time is 1.011965238s
$
你得到什么结果?

TA贡献1828条经验 获得超3个赞
我建议math.Pow(x,y)
在 Go 中实际上没有对whilex^y
的整数值进行任何优化,而只是为。至少在两个程序中用简单替换时,Java 的时间为 6.5 秒,而 Go 的时间为 1.4 秒。使用代替我仍然得到 6.5 秒的 Java 而 29.4 秒的围棋。y
Math.pow(x,y)
x*x
y==2
pow
x*x
pow

TA贡献2080条经验 获得超4个赞
从理论上讲,Go 在开始时需要时间来编译所有源代码,因此它的启动时间会很慢,但是当它完成启动并一条一条地执行命令时,它会运行得很快。执行时间可以与 C/C++ 进行比较。你不能仅仅在几行代码中比较它们,你会发现启动时间在千文件程序中会受到很大影响。
- 4 回答
- 0 关注
- 240 浏览
添加回答
举报