java - 为什么这段代码在 Java 中比在 C++ 和 C# 中更快

标签 java c# c++ c performance

我正在做一个简单的家庭作业,我必须用 C 语言开发一个软件来找到 100 个点之间最近的两个点。

当我完成时,我很想知道在启用更多点和完整的 VC++ 优化的情况下运行它需要多少时间。我尝试了 10000,大约需要 8~9 秒。然后我很好奇 C# 和 Java 需要多少时间来做同样的事情。不出所料,C#用的时间稍长一些,9~10秒;然而,Java 只用了大约 400 毫秒!为什么会这样?!

这是我用 C、C# 和 Java 编写的代码:

C:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <Windows.h>

long perfFrequency = 0;

typedef struct
{
    double X;
    double Y;
} Point;

double distance(Point p1, Point p2)
{
    return sqrt(pow(p1.X - p2.X, 2) + pow(p1.Y - p2.Y, 2));
}

double smallerDistance(Point *points, int size, Point *smallerA, Point  *smallerB)
{
    int i, j;
    double smaller = distance(points[0], points[1]);

    for (i = 0; i < size; i++)
    {
        for (j = i + 1; j < size; j++)
        {
            double dist = distance(points[i], points[j]);
            if (dist < smaller)
            {
                smaller= dist;
                *smallerA = points[i];
                *smallerB = points[j];
            }
        }
    }
    return smaller;
}

void main()
{
    // read size and points from file.
    int size;
    Point *points= (Point *)malloc(size * sizeof(Point));

    // just to make sure everything is ready before the benchmark begins    
    system("pause");

    Point smallerA, smallerB;
    if (!QueryPerformanceFrequency((LARGE_INTEGER *)&perfFrequency))
        printf("Couldn't query performance frequency.");

    long long start, end;   
    double smaller;
    QueryPerformanceCounter((LARGE_INTEGER *)&start);

    smaller= smallerDistance(points, size, &smallerA, &smallerB);

    QueryPerformanceCounter((LARGE_INTEGER *)&end);

    printf("The smaller distance is: %lf. The coordinates of the most close points are: (%lf, %lf) and (%lf, %lf). Time taken: %lfms\n",
        smaller, smallerA.X, smallerA.Y, smallerB.X, smallerB.Y, (end - start) * 1000.0 / perfFrequency);

}

C#:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace StructuredTest
{
    struct Point
    {
        public double X;
        public double Y;
    }

    class Program
    {
        static double Distance(Point p1, Point p2)
        {
            return Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
        }

        static double SmallerDistance(Point[] points, int size, out Point smallerA, out Point smallerB)
        {
            int i, j;
            double smaller = Distance(points[0], points[1]);
            smallerA = default(Point);
            smallerB = default(Point);

            for (i = 0; i < size; i++)
            {
                for (j = i + 1; j < size; j++)
                {
                    double dist = Distance(points[i], points[j]);
                    if (dist < smaller)
                    {
                        smaller = dist;
                        smallerA = points[i];
                        smallerB = points[j];
                    }
                }
            }

            return smaller;
        }

        static void Main(string[] args)
        {
            // read size and points from file 
            int size = int.Parse(file[0]);
            Point[] points= new Point[size];                   

            // make sure everything is ready
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);

            Point smallerA, smallerB;
            double smaller;

            Stopwatch sw = new Stopwatch();
            sw.Restart();

            smaller = SmallerDistance(points, size, out smallerA, out smallerB);

            sw.Stop();

            Console.WriteLine($"The smaller distance is: {smaller}. The coordinates of the most close points are: ({smallerA.X}, {smallerA.Y}) and " +
                $"({smallerB.X}, {smallerB.Y}). Time taken: {sw.ElapsedMilliseconds}ms.");

        }
    }
}

Java:

package structuredtest;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

class Point {

    public Point(double X, double Y) {
        this.X = X;
        this.Y = Y;
    }

    double X;
    double Y;
}

class Result {

    double distance;
    Point p1;
    Point p2;
}

public class StructuredTest {

    static double distance(Point p1, Point p2) {
        return Math.sqrt(Math.pow(p1.X - p2.X, 2) + Math.pow(p1.Y - p2.Y, 2));
    }

    static Result smallerDistance(Point[] points, int size) {
        int i, j;
        double smaller = distance(points[0], points[1]);
        Result r = new Result();

        for (i = 0; i < size; i++) {
            for (j = i + 1; j < size; j++) {
                double dist = distance(points[i], points[j]);
                if (dist < smaller) {
                    smaller = dist;
                    r.p1 = points[i];
                    r.p2 = points[j];
                }
            }
        }

        r.distance = smaller;
        return r;
    }

    public static void main(String[] args) throws IOException {
        // read size and points from file
        int size = Integer.parseInt(file[0]);
        Point[] points = new Point[size];

        // make sure everything is ready    
        System.out.println("Press any key to continue...");
        System.in.read();

        double start = System.nanoTime(), end;

        Result r = smallerDistance(points, size);

        end = System.nanoTime();

        System.out.println("The smaller distance is: " + r.distance + ". The most close points are: ("
                + r.p1.X + "," + r.p1.Y + ") and " + r.p2.X + "," + r.p2.Y + "). Time taken: " + (end - start) / 1000000 + "ms.");

    }

}

如果 java 以微小的优势击败了 C 和 C# 我不会感到惊讶,但是快 20 倍?!

文件格式如下:

3 // number of points in the file. Note that there no comments in the actual file
(3.7098722472288, 4.49056397953787) // point (X,Y)
(8.90232811621332, 9.67982769279173)
(5.68254334818822, 1.71918922506136)
(6.22585901842366, 9.51660500242835)

有趣的是:起初我用来做benchmark的那个10000点的文件,其实只是将另一个100个随机点的文件复制粘贴了100次而已。像这样:

(Point 1)
(Point 2)
(Point 3)
(Point 1)
(Point 2)
(Point 3)

我认为没有必要生成 10000 个随机点,因为无论如何代码都必须遍历所有数字,这几乎没有什么区别(只是更多的分配)。但后来我决定生成 10000 个随机点,看看它们会如何 react :C 和 C# 仍然在大约同一时间运行(增加约 50 毫秒);另一方面,Java 增加了约 500 毫秒。

此外,我认为值得注意的是,java 在 NetBeans 中运行时大约需要 11 秒(即使是在“运行”模式下,而不是“调试”模式下)。

我也尝试过编译为 C++ 而不是 C,但没有任何区别。

我正在为 C 和 C# 使用 VS 2015。

这些是每种语言的设置:

C:

x64
Optimization: Maximize Speed (/O2)
Intrinsic Functions: Yes (/Oi)
Favor Size or Speed: Favor fast code (/Ot)
Enable Fiber-Safe Optimizations: Yes (/GT)
Security Check: Disable Security Check (/GS-)
Floating point model: Fast (/fp:fast)
Everything else: default

C#:

x64
Release Mode
Optimize Code: Enabled
Check for arithmetic overflow: Disabled
.NET 4.5.2 

Java:

JRE/JDK 1.8
Default settings (if there are any)

编辑:

好的,我按照建议重新做了测试:

首先,我在 C 和 C# 中都使用了 Result 类/结构。我在 java 中使用它而不在 C/C# 中使用它的原因是因为 java 不能通过引用传递。 其次,我现在在 main() 函数中重复测试。 并感谢@Tony D 发现了那个错误! :)

我不会发布代码,因为更改很小:只需在其他测试中完全实现 java 版本,这就是我所做的。

这次我只测试了 7000 个点(不是 10000 个)并且只有 30 次迭代,因为测试时间很长,而且这里已经很晚了。

结果变化不大:C# 平均耗时 5228 毫秒,C 4424 毫秒,Java 223 毫秒。 Java 仍然以快 20 倍或更多的速度取胜。

然后我尝试删除对 Math.Pow 的调用(只需更改为 ((p1.X - p2.X) * (p1.X - p2.X)) + ((p1.Y - p2. Y) * (p1.Y - p2.Y))), 然后一切都变了。新结果:

Java:平均 220 毫秒

C#:平均 195 毫秒

C:平均 195 毫秒

如果我之前只检查过 :p

正如我评论的那样,我考虑过这样做,但后来决定最好测试每个编译器内联函数和优化这种简单调用的能力。然而,当我得到那些奇怪的结果时,我本应该回去做这件事的,但我太紧张了,以至于忘记了做这件事。

无论如何,老实说,我很惊讶 Java 编译器能够完全优化那行代码,而 C# 和 C++ 却不能。尽管我知道 C# 上的极端情况检查和内部调用,但我发现 Java 编译器能够注意到在该代码中根本不需要极端情况检查真的很有趣。

最佳答案

如解释here并检查了here ,在纯 C 中,没有整数幂的重载,如下所示:

double pow(double base, int exponent );

这意味着当你在C中调用pow时,它的处理方式类似于:

double pow(double base, double exponent) {
    return exp(log(base) * exponent);
}

还应该对负底和整数幂的情况进行一些检查,这是以特殊方式处理的。在此处添加 if (exponent == 1.0)if (exponent == 2.0) 之类的条件不是一个好主意,因为它会减慢数学代码的速度将 pow 用于适当的目的。结果,平方变慢了 in twelve times or something like that .

原则上,将pow(x, 2) 优化为x * x 的唯一合理方法是让编译器识别此类反模式并为它们生成特殊代码。它只是发生了,所以你的 C 和 C# 编译器与你的设置无法做到这一点,而 Java 编译器可以做到。请注意,它与内联功能无关:仅内联 exp(log(a) * b) 不会使此代码更快。

作为结论,我想指出,任何擅长编写数学代码的程序员都不会在他的代码中编写 pow(x, 2)

关于java - 为什么这段代码在 Java 中比在 C++ 和 C# 中更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428297/

相关文章:

c# - 找不到独特的词

c++ - 在 typedef 结构中声明一个数组

java - 如何找出 IntelliJ 中定义库的位置?

Java 对象转 JSON,使用 javascript 解析

java - JAXB:解码时未定义 id ""

c# - 将 Ctrl+Up 发送到窗口

c++ - 为什么以不同的顺序使用 std::remove_reference 和 std::remove_const 会产生不同的结果?

java - 重新编译 Android Launcher2 (Gingerbread)

c# - 从代码隐藏中单击按钮时刷新更新面板

c# - 使用 &lt;input type ="file"> 上传图像并修复/验证其大小?