1

I Have a bit of an odd question:

void _fnc1( const class& _var )  // takes up reference of _var , but cant modify it 
{                                // i'd do this if i need speed.
    // do something with _var...
};

void _fnc2( class _var ) // takes up a copy of _var
{                        // any modification of _var now stays only in the function scope
    // do something with _var...
};

If I later do something like:

_fnc1( 1 );

and

_fnc2( 1 );

would _fnc1 still be faster ( let's say "class" is int )?

Enn Michael
  • 1,328
  • 14
  • 30
  • For `int` it won't make any difference in speed. It will matter if the objects are larger. – Danny_ds Jan 11 '16 at 00:50
  • Thanks! That's exactly the answer i needed. Would any other of the built-in types make a difference? – Enn Michael Jan 11 '16 at 00:52
  • Think about it in terms of what has to be passed. For reference parameters, essentially a pointer gets passed 'under the hood'. You're going to see no benefits - and maybe even minor reduction in performance - for anything close in size to a pointer. Pretty much all primitive types (integer types, floating point types, Booleans, etc.) are all very close in size to a pointer; there will be no benefit. The break-even point is probably a bit larger than 10 bytes for x86ish architectures, but it'll depend on your machine. So do the responsible thing and measure what the outcome is! – James Picone Jan 11 '16 at 00:59
  • Possible duplicate of [C++: Why pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types](http://stackoverflow.com/questions/5346853/c-why-pass-by-value-is-generally-more-efficient-than-pass-by-reference-for-bu) – Felix Glas Jan 11 '16 at 01:03
  • Oh okay. Thanks man, that's a very complete answer! – Enn Michael Jan 11 '16 at 01:03
  • @EnnMichael - See my answer for some more info. – Danny_ds Jan 11 '16 at 01:05
  • While we're at it, can you guys give me an answer to this: is it slower to declare our iterator within the for loop, such as: for ( int i = 0; ... ) ( say we have a bunch of loops that could use the same iterator within the same scope ), or is it better to declare it in the outside scope, as a variable, such as int i; for ( i = 0; ... );? My logic is that it's faster to declare it outside, if we could reuse it, but i can't be completely sure ( noob ). – Enn Michael Jan 11 '16 at 01:18
  • The only thing the scope of a variable affects is when it is constructed and destructed. The "constructor" and "destructor" for primitive types is somewhere between trivial and nonexistent, so it won't really matter. These kinds of questions are best left to the compiler until analysis of the completed program shows performance issues, and even then you're mostly better off looking at algorithmic factors than this kind of micro-optimization. – James Picone Jan 11 '16 at 03:10

1 Answers1

0

For int it won't make much difference in speed. Although the compiler could make some extra optimizations in certain cases - also when using const for example.

It will matter if the objects are larger, and/or their copy constructor needs to be called.

Small parameter types like int's other built-in types can be stored in registers directly in some cases. Using a pointer or a reference for such types wouldn't make much difference in speed (they take about the same space), except for some compiler optimizations.

is it slower to declare our iterator within the for loop, such as: for ( int i = 0; ... ) ( say we have a bunch of loops that could use the same iterator within the same scope ), or is it better to declare it in the outside scope, as a variable, such as int i; for ( i = 0; ... );? My logic is that it's faster to declare it outside, if we could reuse it, but i can't be completely sure

The general rule is to declare the variables as late as possible (i.e. as close as possible to where you need them). But again, compilers can be very smart, and will optimize that away in most cases.

As for your specific example: it's always interesting to do a test. Chances are the compiler will optimize most of the differences away and might even generate the same code (or inline the whole function).

The best test is look at the generated assembly code. A lot can be learned from that.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46