Reputation: 1714
I have got a function which I need to pass a value to for read-only purposes only.
For example:
unsigned short strlen(String str)
{
short i = 0;
while(str[i] != '\0')
i++;
return i;
}
As you can see, I do not want to change the original value I work with, I only need to read its length. Would it - in terms of performance - be better to replace the parameter String str
, which is making a copy of the original variable, by a reference like String &str
?
Upvotes: 2
Views: 185
Reputation: 126552
Would it - in terms of performance - be better to replace the parameter String str, which is making a copy of the original variable, by a reference like String &str?
Yes, it would. Instead of creating a copy, which you do not seem to need here, a reference would be bound. Since you are not modifying the object, a reference to const
would be better:
unsigned short strlen(String const& str)
// ^^^^^^
Upvotes: 5