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

Ervin Bosenbacher via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 29 23:58:30 PDT 2017


Just would like to share something. For months I couldn't decide 
what language to use for my pet project, analysed dozens of 
languages from rust to go and I always fell back to C++ 
(performance is critical for me). After rust I had a look at 
DLang but as a C++ veteran I had my (wrong) feelings, prejudices 
etc but I got infected and started to go down a long route of 
trying to accept it and trying to love it. I am a slow learner 
but forced to open my mind. I am also a Python dev so I kinda 
liked the syntax and the simplicity of the language, the speed of 
compilation and execution was also appealing but still... But 
then something has happened which basically helped to make up my 
mind and to go with D finally.

 From the  Programming in D book i  typed in the below code, I use 
Sublime.

```
import std.stdio : writeln;

void main()
{
     int[12] months = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 
30, 31 ];
     int[] first = months[0 .. 3];
     int[] second = months[3 .. 6];
     int[] third = months[6 .. 9];
     int[] fourth = months[9 .. 12];
     writeln(first);
     writeln(second);
     writeln(third);
     writeln(fourth);
}
```

Then I have decided to reimplement this in C++. The code is not 
perfect hacked it together on the train. The length and the 
complexity is already on the negative side.

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

using namespace std;

template<class X, class Y>
X slice(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() {

         array<int, 12> _arr = { 31, 28, 31, 30, 31, 30, 31, 31, 
30, 31, 30, 31 };
         array<int, 3> first = slice<decltype(first)>(_arr, 0, 3);
         array<int, 3> second = slice<decltype(second)>(_arr, 3, 
6);
         array<int, 3> third = slice<decltype(third)>(_arr, 6, 9);
         array<int, 3> fourth = slice<decltype(fourth)>(_arr, 9, 
12);

         cout << first << endl;
         cout << second << endl;
         cout << third << endl;
         cout << fourth << endl;
}

Then  came the performance test, here we go:

Performance test:
rvinMacBookProLimegg:source ervinbosenbacher$ time ./app
[31, 28, 31]
[30, 31, 30]
[31, 31, 30]
[31, 30, 31]

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

ErvinMacBookProLimegg:source ervinbosenbacher$ time ./a.out
[31, 28, 31]
[30, 31, 30]
[31, 31, 30]
[31, 30, 31]

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

That is the same, that came as a shock to me.

Happy coding al!


More information about the Digitalmars-d mailing list