这件事让我困惑了3天。我有一个应用程序需要评估一组非常少元素的整数多项式(多个参数)。我已经用Java编写了一个实现,现在正在移植到C++。
在测试过程中,我注意到C++版本比Java版本慢了几个数量级。当然,我知道JIT和这种编译器场景很匹配,但我看到的与我预期的差距太大了。
以下是示例代码,您需要使用boost来编译C++代码(但只有在进行简单的时间测量时才需要此依赖项)。
显然,使用clang-3.3编译的C++版本在纯计算能力方面运行略快,但是当多项式被解释时,Java(openjdk 1.7.0.60)表现更优。我猜测,这是因为我的C++代码由于迭代小向量(在示例中为1个元素)而不太优化。我假设JVM在缓存命中/未命中方面做得更好。
有没有办法让我的C++版本表现更好?还有其他原因我没看到吗?顺便说一句:有没有办法衡量C++和Java进程的缓存一致性?
C++代码如下:
在测试过程中,我注意到C++版本比Java版本慢了几个数量级。当然,我知道JIT和这种编译器场景很匹配,但我看到的与我预期的差距太大了。
以下是示例代码,您需要使用boost来编译C++代码(但只有在进行简单的时间测量时才需要此依赖项)。
choeger@daishi ~/uebb % clang++ -O3 -std=c++11 polytest.cpp -lboost_timer -lboost_system
choeger@daishi ~/uebb % ./a.out
0.011694s wall, 0.010000s user + 0.000000s system = 0.010000s CPU (85.5%)
Ideal Result: 1e+07
0.421986s wall, 0.420000s user + 0.000000s system = 0.420000s CPU (99.5%)
Result: 1e+07
choeger@daishi ~/uebb % javac PolyTest.java
choeger@daishi ~/uebb % java PolyTest
evals: 10000000 runtime: 17ms
Ideal Result: 1.0E7
evals: 10000000 runtime: 78ms
Result: 1.0E7
显然,使用clang-3.3编译的C++版本在纯计算能力方面运行略快,但是当多项式被解释时,Java(openjdk 1.7.0.60)表现更优。我猜测,这是因为我的C++代码由于迭代小向量(在示例中为1个元素)而不太优化。我假设JVM在缓存命中/未命中方面做得更好。
有没有办法让我的C++版本表现更好?还有其他原因我没看到吗?顺便说一句:有没有办法衡量C++和Java进程的缓存一致性?
C++代码如下:
#include <boost/timer/timer.hpp>
#include <iostream>
#include <vector>
using namespace std;
struct Product {
int factor;
vector<int> fields;
};
class SumOfProducts {
public:
vector<Product> sum;
/**
* evaluate the polynomial with arguments separated by width
*/
inline double eval(const double* arg, const int width) const {
double res = 0.0;
for (Product p : sum) {
double prod = p.factor;
for (int f : p.fields) {
prod *= arg[f*width];
}
res += prod;
}
return res;
};
};
double idealBenchmark(const double* arg, const int width) {
boost::timer::auto_cpu_timer t;
double res = 0.0;
// run 10M evaluations
for (long l = 0; l < 10000000; l++) {
res = res + arg[width] * arg[width];
}
return res;
}
double benchmark(const double* arg, const SumOfProducts& poly) {
boost::timer::auto_cpu_timer t;
double res = 0.0;
// run 10M evaluations
for (long l = 0; l < 10000000; l++) {
res = res + poly.eval(arg, 1);
}
return res;
}
int main() {
//simple polynomial: x_1^2
Product p;
p.factor = 1;
p.fields.push_back(1);
p.fields.push_back(1);
SumOfProducts poly;
poly.sum.push_back(p);
double arg[] = { 0, 1 };
double res = idealBenchmark(arg, 1);
cout << "Ideal Result: " << res << endl;
res = benchmark(arg, poly);
cout << "Result: " << res << endl;
}
Java版本如下:
public class PolyTest {
static class Product {
public final int factor;
public final int[] fields;
public Product(int pFactor, int[] pFields) {
factor = pFactor;
fields = pFields;
}
}
static class SumOfProducts {
final Product[] sum;
public SumOfProducts(Product[] pSum) {
sum = pSum;
}
/**
* evaluate the polynomial with arguments separated by width
*/
double eval(final double[] arg, final int width) {
double res = 0.0;
for (Product p : sum) {
double prod = p.factor;
for (int f : p.fields) {
prod *= arg[f*width];
}
res += prod;
}
return res;
}
}
static double idealBenchmark(final double[] arg, final int width) {
final long start = System.currentTimeMillis();
double res = 0.0;
long evals = 0;
// run 10M evaluations
for (long l = 0; l < 10000000; l++) {
evals++;
res = res + arg[width] * arg[width];
}
System.out.println("evals: " + evals + " runtime: " + (System.currentTimeMillis() - start) + "ms");
return res;
}
static double benchmark(final double[] arg, final SumOfProducts poly) {
final long start = System.currentTimeMillis();
double res = 0.0;
long evals = 0;
// run 10M evaluations
for (long l = 0; l < 10000000; l++) {
evals++;
res = res + poly.eval(arg, 1);
}
System.out.println("evals: " + evals + " runtime: " + (System.currentTimeMillis() - start) + "ms");
return res;
}
public static void main(String[] args) {
//simple polynomial: x_1^2
Product p = new Product(1, new int[]{1, 1});
SumOfProducts poly = new SumOfProducts(new Product[]{p});
double arg[] = { 0, 1 };
double res = idealBenchmark(arg, 1);
System.out.println("Ideal Result: " + res);
res = benchmark(arg, poly);
System.out.println("Result: " + res);
}
}