views:

88

answers:

2

Suppose some function that always gets some parameter s that it does not use.

def someFunc(s):
  # do something _not_ using s, for example
  a=1

now consider this call

someFunc("the unused string")

which gives a string as a parameter that is not built during runtime but compiled straight into the binary (hope thats right).

The question is: when calling someFunc this way for, say, severalthousand times the reference to "the unused string" is always passed but does that slow the program down?

in my naive thoughts i'd say the reference to "the unused string" is 'constant' and available in O(1) when a call to someFunc occurs. So i'd say 'no, that does not hurt performance'.

Same question as before: "Am I right?"

thanks for some :-)

+2  A: 

The string is passed (by reference) each time, but the overhead is way too tiny to really affect performance unless it's in a super-tight loop.

Max Shawabkeh
False - that is not a reference for an immutable object, it is a copy
jcoon
jcoon, it is not copied.
FogleBird
>>> def f(x): print id(x)>>> s = 'immutable string'>>> id(s)11829760>>> f(s)11829760
FogleBird
@jcoon: All Python objects are passed as references, so you're saying the reference is copied? Or you're saying that the underlying string object is first copied and then a new reference provided? What are you saying?
S.Lott
that is what would interest me too, because thats actually an important part of the question i was asking - just imagine a really large string copied at each call. That would be a case to not do this. if only a ref was passed there would not be any (performance) hit.
bb
The former - If you do anything the string that is passed, it is done to a copy of the variable being referenced that applies only to the scope of that function.
jcoon
@jcoon: no, it's not done to a copy. A new object is created any time you "change" an immutable object.
Max Shawabkeh
@Max S. right.@jcoon you assign a new value to the copy of the reference, that is the 'address' of the new string.
bb
@Max - that make more sense, thanks
jcoon
+1  A: 

this is an implementation detail of CPython, and may not apply to other pythons but yes, in many cases in a compiled module, a constant string will reference the same object, minimizing the overhead.

In general, even if it didn't, you really shouldn't worry about it, as it's probably imperceptibly tiny compared to other things going on.

However, here's a little interesting piece of code:

>>> def somefunc(x):
...    print id(x) # prints the memory address of object pointed to by x
... 
>>> 
>>> def test():
...    somefunc("hello")
... 
>>> test()
134900896
>>> test()
134900896 # Hooray, like expected, it's the same object id
>>> somefunc("h" + "ello")
134900896  # Whoa, how'd that work?

What's happening here is that python keeps a global string lookup and in many cases, even when you concatenate two strings, you will get the same object if the values match up.

Note that this is an implementation detail, and you should NOT rely on it, as strings from any of: files, sockets, databases, string slicing, regex, or really any C module are not guaranteed to have this property. But it is interesting nonetheless.

Crast