KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I like some features of D, but would be interested if they come with a runtime penalty? To compare, I implemented a simple program that computes scalar products of many short vectors both in C++ and in D. The result is surprising: D: 18.9 s [see below for final runtime] C++: 3.8 s Is C++ really almost five times as fast or did I make a mistake in the D program? I compiled C++ with g++ -O3 (gcc-snapshot 2011-02-19) and D with dmd -O (dmd 2.052) on a moderate recent linux desktop. The results are reproducible over several runs and standard deviations negligible. Here the C++ program: #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, 10
Tags (comma-separated)
Save Edits
Cancel