How efficient is Python substring extraction?
I've got the entire contents of a text file (at least a few KB) in string myStr
.
Will the following code create a copy of the string (less the first character) in memory?
myStr = myStr[1:]
I'm hoping it just refers to a different location in the same internal buffer. If not, is there a more efficient way to do this?开发者_如何学编程
Thanks!
Note: I'm using Python 2.5.
At least in 2.6, slices of strings are always new allocations; string_slice() calls PyString_FromStringAndSize(). It doesn't reuse memory--which is a little odd, since with invariant strings, it should be a relatively easy thing to do.
Short of the buffer API (which you probably don't want), there isn't a more efficient way to do this operation.
As with most garbage collected languages, strings are created as often as needed, which is very often. The reason for this is because tracking substrings as described would make garbage collection more difficult.
What is the actual algorithm you are trying to implement. It might be possible to give you advice for ways to get better results if we knew a bit more about it.
As for an alternative, what is it you really need to do? Could you use a different way of looking at the issue, such as just keeping an integer index into the string? Could you use a array.array('u')
?
One (albeit slightly hacky) solution would be something like this:
f = open("test.c")
f.read(1)
myStr = f.read()
print myStr
It will skip the first character, and then read the data into your string variable.
Depending on what you are doing, itertools.islice
may be a suitable memory-efficient solution (should one become necessary).
精彩评论