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.