Passing an array to a subroutine

I have this Ubuntu C++ package. Everything works but I need it to work as fast as it can, so I am trying to salvage microseconds. I want to make sure that when I pass an array of doubles to a subroutine, only a pointer is passed because this subroutine is a part of a loop and I don't want this array to be passed naked so many times. I know that an address should be passed but I need a confirmation, because on the bottom of my soul I have ridiculous doubts. Thanks, - A.
It is.

If you really don't trust it, then explicitly pass the address of the first element in the array.

Then, profile your code as it runs and work out where the time is actually being spent, so you fix things that definitely make it faster instead of guessing.
Thanks. -A
You can (and, perhaps, arguably should) use a std::vector like this:
1
2
3
4
5
6
7
8
9
10
11
void my_superfast_function(double* data, std::size_t size)
{
    // do super quick stuff here
}

int main()
{
    std::vector<double> v{0.23, 3.82, 1.4, 2.2}; // etc...

    my_superfast_function(v.data(), v.size()); // passes raw pointer to internal data
}


But first I would try simply passing the vector by reference as the pointer to its internal data will likely be cached by the compiler to make access just as performant as the raw pointer:

1
2
3
4
void my_superfast_function(std::vector<double> const& v) // almost certainly just as fast
{
    // do super quick stuff here
}
Last edited on
Thank you. The reason I am not using vectors is that I need complex<double> and also a "devotion" to tradition, or simply a habit.

Thanks, - A.
Last edited on
vector<complex<double>> should solve the first of those. Fixing the second is up to you.
Topic archived. No new replies allowed.
  • 1007451581 2018-02-22
  • 8908121580 2018-02-22
  • 141161579 2018-02-22
  • 9421578 2018-02-22
  • 2826901577 2018-02-22
  • 3647361576 2018-02-22
  • 5717551575 2018-02-22
  • 523811574 2018-02-22
  • 6439871573 2018-02-22
  • 8109431572 2018-02-22
  • 8757321571 2018-02-22
  • 5265111570 2018-02-22
  • 3351351569 2018-02-22
  • 5109361568 2018-02-22
  • 4455391567 2018-02-22
  • 9091121566 2018-02-22
  • 24791565 2018-02-22
  • 2486841564 2018-02-21
  • 9847231563 2018-02-21
  • 9264681562 2018-02-21