【发布时间】:2011-07-05 18:27:54
【问题描述】:
我喜欢 D 的一些功能,但如果它们带有 运行时惩罚?
为了比较,我实现了一个简单的程序,用 C++ 和 D 计算许多短向量的标量积。结果令人惊讶:
- D:18.9 秒 [最终运行时间见下文]
- C++:3.8 秒
C++ 真的快五倍还是我在 D 中犯了一个错误? 程序?
我使用 g++ -O3 (gcc-snapshot 2011-02-19) 编译了 C++,使用 dmd -O (dmd 2.052) 在最近的 linux 桌面上编译了 D。结果可在多次运行中重现,标准偏差可忽略不计。
这里是 C++ 程序:
#include <iostream>
#include <random>
#include <chrono>
#include <string>
#include <vector>
#include <array>
typedef std::chrono::duration<long, std::ratio<1, 1000>> millisecs;
template <typename _T>
long time_since(std::chrono::time_point<_T>& time) {
long tm = std::chrono::duration_cast<millisecs>( std::chrono::system_clock::now() - time).count();
time = std::chrono::system_clock::now();
return tm;
}
const long N = 20000;
const int size = 10;
typedef int value_type;
typedef long long result_type;
typedef std::vector<value_type> vector_t;
typedef typename vector_t::size_type size_type;
inline value_type scalar_product(const vector_t& x, const vector_t& y) {
value_type res = 0;
size_type siz = x.size();
for (size_type i = 0; i < siz; ++i)
res += x[i] * y[i];
return res;
}
int main() {
auto tm_before = std::chrono::system_clock::now();
// 1. allocate and fill randomly many short vectors
vector_t* xs = new vector_t [N];
for (int i = 0; i < N; ++i) {
xs[i] = vector_t(size);
}
std::cerr << "allocation: " << time_since(tm_before) << " ms" << std::endl;
std::mt19937 rnd_engine;
std::uniform_int_distribution<value_type> runif_gen(-1000, 1000);
for (int i = 0; i < N; ++i)
for (int j = 0; j < size; ++j)
xs[i][j] = runif_gen(rnd_engine);
std::cerr << "random generation: " << time_since(tm_before) << " ms" << std::endl;
// 2. compute all pairwise scalar products:
time_since(tm_before);
result_type avg = 0;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
avg += scalar_product(xs[i], xs[j]);
avg = avg / N*N;
auto time = time_since(tm_before);
std::cout << "result: " << avg << std::endl;
std::cout << "time: " << time << " ms" << std::endl;
}
这里是 D 版本:
import std.stdio;
import std.datetime;
import std.random;
const long N = 20000;
const int size = 10;
alias int value_type;
alias long result_type;
alias value_type[] vector_t;
alias uint size_type;
value_type scalar_product(const ref vector_t x, const ref vector_t y) {
value_type res = 0;
size_type siz = x.length;
for (size_type i = 0; i < siz; ++i)
res += x[i] * y[i];
return res;
}
int main() {
auto tm_before = Clock.currTime();
// 1. allocate and fill randomly many short vectors
vector_t[] xs;
xs.length = N;
for (int i = 0; i < N; ++i) {
xs[i].length = size;
}
writefln("allocation: %i ", (Clock.currTime() - tm_before));
tm_before = Clock.currTime();
for (int i = 0; i < N; ++i)
for (int j = 0; j < size; ++j)
xs[i][j] = uniform(-1000, 1000);
writefln("random: %i ", (Clock.currTime() - tm_before));
tm_before = Clock.currTime();
// 2. compute all pairwise scalar products:
result_type avg = cast(result_type) 0;
for (int i = 0; i < N; ++i)
for (int j = 0; j < N; ++j)
avg += scalar_product(xs[i], xs[j]);
avg = avg / N*N;
writefln("result: %d", avg);
auto time = Clock.currTime() - tm_before;
writefln("scalar products: %i ", time);
return 0;
}
【问题讨论】:
-
顺便说一句,您的程序在这一行有一个错误:
avg = avg / N*N(操作顺序)。 -
您可以尝试使用数组/向量操作重写代码digitalmars.com/d/2.0/arrays.html
-
为了提供更好的比较,您应该使用相同的编译器后端。 DMD 和 DMC++ 或 GDC 和 G++
-
@Sion Sheevok 不幸的是,dmd 分析似乎不适用于 Linux? (如果我错了,请纠正我,但如果我说
dmd ... trace.def,我会得到error: unrecognized file extension def。optlink 的 dmd 文档仅提及 Windows。 -
啊,从不关心它吐出的那个 .def 文件。计时在 .log 文件中。 “它包含链接器应该链接它们的顺序的函数列表” - 也许这有助于 optlink 优化某些东西?另请注意,“此外,ld 完全支持标准的“*.def”文件,这些文件可以像目标文件一样在链接器命令行上指定”-因此,如果您非常需要,可以尝试通过 -L 传递 trace.def到。
标签: c++ performance runtime d