gpt4 book ai didi

c++ - D 与 C++ 相比有多快?

转载 作者:IT老高 更新时间:2023-10-28 11:33:41 25 4
gpt4 key购买 nike

我喜欢 D 的一些功能,但如果它们带有运行时惩罚?

为了比较,我实现了一个简单的程序,用 C++ 和 D 计算许多短 vector 的标量积。结果令人惊讶:

  • 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;
}

最佳答案

要启用所有优化并禁用所有安全检查,请使用以下 DMD 标志编译您的 D 程序:

-O -inline -release -noboundscheck

编辑:我已经用 g++、dmd 和 gdc 尝试过你的程序。 dmd 确实落后,但 gdc 的性能非常接近 g++。我使用的命令行是 gdmd -O -release -inline(gdmd 是 gdc 的包装器,它接受 dmd 选项)。

查看汇编程序列表,它看起来既不是 dmd 也不是 gdc 内联 scalar_product,但 g++/gdc 确实发出了 MMX 指令,因此它们可能正在自动矢量化循环。

关于c++ - D 与 C++ 相比有多快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com