2

I am not sure if this is the place for this question, but here it goes.

I am working on computational physics model in C++ and I received a big code that I am tiding up.

The field is quantum electronics, so there are lots of integrals and expressions, but my question concerns the optimization.

Initially the code needed 4 hours to finish the simulation, I tidied this up to 30 min after removing several bottlenecks.

What I still do not like is the fact that the data in the code (mostly arrays of type double) mix std::vector<(double)>, std::valarray<(double)> and armadilo's arma::vec.

Which one of these is the best way and should I even try to make uniform data type for the arrays?

I think the reason previous PhD students used valarray is its friendliness since operators *,/,+,- are overloaded and there is some scarce online promise that valarrays are faster then vectors (big online debate about this). Armadillo was brought in, due to its similarity to MATLAB syntax and extremely well documented library, and std::vector<(T)> is kept when T is class and not double.

I personally prefer armadillo, because it has very simple logic and it feels natural, valarrays annoy me sometimes because they do not have many member functions, but iteration through them is quite easy, vectors are very annoying, arithmetic operators are not overloaded for <(double)> and if I chose these I would probably need to make my own class for Array of doubles which I do not really want to do.

Keep in mind that I have no idea how these arrays behave in memory, I started C++ last year, and I have a long way to go. I am planning of developing this code for 2 more dimensions (currently it's 1D) and execution time will certainly grow.

I am therefor seeking advice with which array type I should continue and should I make the code uniform (force all arrays of double to be of the same type (std::valarray, std::vector or arma::vec))

  • "which one is best" depends on a lot of things. If the purpose of these containers is to act as matrices or vectors (in the mathematical sense) then armadillo or eigen would make sense semantically and from a performance point of view. Honestly once these type of questions get too domain specific, I'd hop over to [Computational Science](https://scicomp.stackexchange.com/). – Cory Kramer Jul 13 '17 at 12:34
  • For computation purpose Eigen is always the best choice. http://eigen.tuxfamily.org – vadikrobot Jul 13 '17 at 13:02
  • 1
    [Armadillo](http://arma.sourceforge.net) is a good bet, with its clear syntax. Eigen has a horrible API, where you spend more time debugging code than getting work done. A colleague doing computational physics simulations mentioned that using it is like "shaving a yak" – hbrerkere Jul 13 '17 at 15:40
  • `std::vector<(T)>`: the parentheses look weird. – Marc Glisse Jul 13 '17 at 17:14
  • _"`std::vector<(T)>` is kept due to its polymorphism"_ and, setting aside the spurious parentheses, what polymorphism is that, exactly? I can't think of any. Besides, beyond the usual 'use `vector` unless you have a specific reason not to', I think this will be too broad and/or opinion-based to be useful for SO. CS as mentioned, or if you can get enough code to cite Code Review, are probably better places. – underscore_d Jul 13 '17 at 17:27
  • Sorry, I mixed std::vector with boost (which was also used in one place). I fixed what I wanted to say. Weird () are there because stack overflow automatically deletes anything in < ... > brackets, and I am too lazy to check why. – Aleksandar Demić Jul 13 '17 at 18:30

0 Answers0