1

I'm developing a piece of code which makes scientific calculation, and as a result I use extensive use of double variables. These variables travel from function to function, but most of them are never modified and hence defined as const in function signatures.

I have used double* in function signatures, but then I started to wonder that whether it's better to use double instead because:

  • double* and double are same size, 8 bytes in C++.
  • When I send in double*, I need another fetch operation in the function.

When using the variables in const fashion, are there any valid reasons to use double* instead of double?

bayindirh
  • 425
  • 6
  • 21
  • 13
    It's almost always wiser to user pass/return by value for native types. Please check this http://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference – themagicalyang Nov 01 '16 at 07:30
  • 7
    I'd suggest `double const &` rather than `double const *`, but the behaviour will be almost the same. Whether to use a reference or a copy of the value. well, there's probably not much difference. As a rule, I take built in types by value unless I actually need a reference. If you are worrying about speed, don't. Or at least, profile, this is too subtle to make a good educated guess. – BoBTFish Nov 01 '16 at 07:31
  • @BoBTFish Thanks. Yes, it's a micro-optimization, but I'm more concerned about best-practices. Nevertheless, sometimes a small fetch operation, hammered enough, makes a big difference in performance. – bayindirh Nov 01 '16 at 07:33
  • @themagicalyang Thanks! It looks like I wasn't searching general enough. – bayindirh Nov 01 '16 at 07:49
  • 1
    You can check this link to cover it better : http://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value – Prometheus Nov 01 '16 at 07:52
  • @Prometheus Thanks, another interesting aspect to consider. – bayindirh Nov 01 '16 at 07:55
  • @bayindirh anyway, always use `double` instead of `const double` in function signature, if you really need a reference, use `double&` – Danh Nov 01 '16 at 07:56
  • 2
    @bayindirh *"sometimes a small fetch operation, hammered enough, makes a big difference in performance"* - that may be true in very rare cases, but you are better off not thinking about it until it happens. You cannot possibly predict whether `double` or `double*` will be faster in the specific case and after the optimizations done by the compiler and by the CPU – zvone Nov 01 '16 at 07:57
  • 1
    @zvone, you're right. I never optimize without profiling. I just wanted to learn the best practice to homogenize my code base. Functionality first, optimizations later. – bayindirh Nov 01 '16 at 08:01
  • @Danh any reasons to skip `const`? I won't modify the value anyway, so I think it's good to mark it as such. – bayindirh Nov 01 '16 at 08:12
  • @bayindirh you should ask if there is any reason to mark it `const`? It's a local value, changing it won't change outer's state. Marking it `const` will prohibit future change in it implementation, which, likely, want to change it. – Danh Nov 01 '16 at 08:15
  • 2
    @Danh the values are constants are in mathematical formulas, so it won't become non-const in the future. I'm marking const them because of two reasons. First to give compiler play area to make its optimizations, if the compiler chooses to and to hint the other developers to be confident that the value will not be mangled with. You're correct about outer state, though. – bayindirh Nov 01 '16 at 08:18
  • @Prometheus Most of the discussion there is about objects, not native data types. And the assumption seems to be that the objects will be relatively large, so the expense of copying will be more noticeable. – Barmar Nov 01 '16 at 08:20

1 Answers1

1

Here's the crust of the matter:

  • First of all it's always a good idea to use pass-by-value for native data types (see here) if you have no special requirements such as:
    • Transferring arrays via the variable
    • Keep inter-object relations (as outlined here)
    • And similar
  • Transferring references have negligible performance affects for most cases.
  • Be careful while using const, if you need to change the variable later, it's extra refactorization work, nevertheless const correctness is important.
Community
  • 1
  • 1
bayindirh
  • 425
  • 6
  • 21