3 Replies - 532 Views - Last Post: 29 August 2011 - 05:34 AM Rate Topic: -----

#1 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Is it an optimization or not?

Posted 29 August 2011 - 04:12 AM

If i have this function:
f1(char a)
I dont want to modify the value of a in the function.

If I modify the function like this:
f1(const char& a)
then is this an optimization or not?

My guess is that it isn't since the pointer used for reference is bigger the the size of a char being copied.

I'm I right?

Thanks!

This post has been edited by livium: 29 August 2011 - 04:13 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Is it an optimization or not?

#2 Adak   User is offline

  • D.I.C Lover
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Is it an optimization or not?

Posted 29 August 2011 - 04:30 AM

Most computer hardware is optimized for integers. Char's use less space, but are no faster, on such a system.

The first one would be faster, because the second one involves a pointer - which would have to be de- referenced at run time. Just use const char a, for max efficiency. The stack will be given it's copy, but that is done with very few cpu cycles. The const allows the compiler to optimize the code, as well as it is able.
Was This Post Helpful? 3
  • +
  • -

#3 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: Is it an optimization or not?

Posted 29 August 2011 - 04:43 AM

Ok, Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Is it an optimization or not?

Posted 29 August 2011 - 05:34 AM

Note that for larger data such as arbitrary objects a const reference can and often will be faster, especially when the class has an expensive constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1