The nail in the coffin of C++ or why don't GO there...

Ervin Bosenbacher via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 30 04:03:03 PDT 2017


On Thursday, 30 March 2017 at 10:28:01 UTC, dennis luehring wrote:
> Am 30.03.2017 um 08:58 schrieb Ervin Bosenbacher:
>> That is the same, that came as a shock to me.
>
> most compilers (for many languages) can optimize your 
> super-trivial example down to nothing - for at least the last 
> 10 years or more
>
> so whats the point? you're talkin about "performance is 
> critical for me"
> but missing even minor knowledge about todays compiler powers?
>
> for a benchmark you need:
> -loops, running millions of times, preventing IO and do now 
> fall into the completely-optimized-to-nothing-trap etc.

Tried again, threw in summing up a number 10 million times and 
repeat the arrays 10 million times, I have used arrays on the 
stack for the D version. And when I am talking about performance 
I am curious about CPU bound stuff.

The D version:

import std.stdio;

void main()
{
     long sum = 0;
     foreach (long i; 0 .. 10000000) {
         int[12] months = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 
31, 30, 31 ];
         int[3] first = months[0 .. 3];
         int[3] second = months[3 .. 6];
         int[3] third = months[6 .. 9];
         int[3] fourth = months[9 .. 12];

         sum += i;
     }
     writeln(sum);
}

The C++11 version:

#include <iostream>
#include <array>
#include <iterator>

using namespace std;

template<class X, class Y>
X CopyArray(const Y& src, const size_t start, const size_t end)
{
     X dst;
     std::copy(src.begin() + start, src.begin() + end, 
dst.begin());
     return dst;
}

template <class T, std::size_t N>
ostream& operator<<(ostream& o, const array<T, N>& arr)
{
         std::cout << "[";
         for (auto& elem : arr) {
                 std::cout << elem;
                 if (&elem != &arr.back()) printf(", ");
         }
         std::cout << "]";
         return o;
}

int main() {

         long sum = 0;
         for (long i = 0; i < 10000000; i++) {
                 array<int, 12> _arr = { 31, 28, 31, 30, 31, 30, 
31, 31, 30, 31, 30, 31 };
                 array<int, 3> first = 
CopyArray<decltype(first)>(_arr, 0, 3);
                 array<int, 3> second = 
CopyArray<decltype(second)>(_arr, 3, 6);
                 array<int, 3> third = 
CopyArray<decltype(third)>(_arr, 6, 9);
                 array<int, 3> fourth = 
CopyArray<decltype(fourth)>(_arr, 9, 12);

                 sum += i;
         }

         cout << sum << endl;
         /*cout << first << endl;
         cout << second << endl;
         cout << third << endl;
         cout << fourth << endl; */
}

Test

With DMD

$ dmd app.d -O
$ time ./app
49999995000000

real	0m0.290s
user	0m0.282s
sys	0m0.003s


With LDC

$ ldc2 app.d -O
$ time ./app
49999995000000

real	0m0.004s
user	0m0.001s
sys	0m0.002s


And then the C++
$ g++ app.cpp -O3 -o app_cpp
$ time ./app_cpp
49999995000000

real	0m0.004s
user	0m0.001s
sys	0m0.002s

I am still convinced that D is awesome.

Happy coding!



More information about the Digitalmars-d mailing list