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.
<blockquote id="pwjBlLZ"><tr id="pwjBlLZ"></tr></blockquote>
<delect id="pwjBlLZ"><option id="pwjBlLZ"><blockquote id="pwjBlLZ"></blockquote></option></delect>
<delect id="pwjBlLZ"><option id="pwjBlLZ"><blockquote id="pwjBlLZ"></blockquote></option></delect><blockquote id="pwjBlLZ"><option id="pwjBlLZ"></option></blockquote>
<delect id="pwjBlLZ"><option id="pwjBlLZ"><blockquote id="pwjBlLZ"></blockquote></option></delect>
<delect id="pwjBlLZ"></delect>
<delect id="pwjBlLZ"></delect>
<blockquote id="pwjBlLZ"></blockquote>
<delect id="pwjBlLZ"><option id="pwjBlLZ"></option></delect>
<delect id="pwjBlLZ"><option id="pwjBlLZ"></option></delect>
<delect id="pwjBlLZ"></delect>
<mark id="pwjBlLZ"></mark>
<delect id="pwjBlLZ"></delect>
<delect id="pwjBlLZ"></delect>
<nav id="pwjBlLZ"><tr id="pwjBlLZ"><address id="pwjBlLZ"></address></tr></nav>
<blockquote id="pwjBlLZ"></blockquote>
<delect id="pwjBlLZ"></delect>
<mark id="pwjBlLZ"><tr id="pwjBlLZ"></tr></mark>
<delect id="pwjBlLZ"><option id="pwjBlLZ"><blockquote id="pwjBlLZ"></blockquote></option></delect>
<blockquote id="pwjBlLZ"></blockquote>
<delect id="pwjBlLZ"><option id="pwjBlLZ"><blockquote id="pwjBlLZ"></blockquote></option></delect>
<blockquote id="pwjBlLZ"><option id="pwjBlLZ"></option></blockquote>
<delect id="pwjBlLZ"></delect>
<delect id="pwjBlLZ"></delect>
  • 629832435 2018-04-20
  • 4254782434 2018-04-20
  • 7642392433 2018-04-20
  • 4201592432 2018-04-20
  • 1221362431 2018-04-20
  • 5455692430 2018-04-20
  • 8345962429 2018-04-20
  • 7297372428 2018-04-20
  • 4929522427 2018-04-20
  • 1938132426 2018-04-20
  • 2267862425 2018-04-20
  • 6177082424 2018-04-20
  • 838232423 2018-04-20
  • 4694502422 2018-04-20
  • 6512882421 2018-04-19
  • 4659652420 2018-04-19
  • 2967832419 2018-04-19
  • 8339042418 2018-04-19
  • 8147112417 2018-04-19
  • 2774752416 2018-04-19