3 ms·
A string type that is not a sequence of characters can be allowed to be more efficient by being lazy. Say you are constructing a string like this (example in Py
by bjourne 13y ago
A string type that is not a sequence of characters can be allowed to be more efficient by being lazy. Say you are constructing a string like this (example in Python, but the principle holds no matter what language):
s = ""
for x in range(100):
s += str(x)
print s
If s is a sequence of characters, then for each iteration of the loop s needs to be reallocated to encompass the appended str(x) characters.
If s is an abstract string, then the += operation can be implemented as just adding a reference to the string to eventually be appended. Then when the print operation occurs, the whole string can be materialized in one go so that it can be printed.
Another, and more philosophical argument against string as a sequence of characters is that a string is no more a sequence of characters, than it is a sequence of symbols or a sequence of words or a sequence of sentences.
- pekk 13y agoIncidentally, this way of building strings in Python is a performance pessimization and not the right way to do it to begin with.
- serichsen 13y agos = [] for x in range(100): s = s.append(x) format s In other words, this is something that holds just the same for vectors of any type. The "philosophical" "argument" is really not relevant, since the difference between characters and "symbols" seems to be just what you want (whatever that is), while words and sentences can be represented by strings themselves. This is just a tangent that adds nothing to the question of how to represent a string.
- bjourne 13y agoYou're missing the point. If you are appending items to a sequence, you are better of using a linked list type which is why your argument doesn't hold because you wouldn't be using an array type in the first place. But with "strings are character arrays" you can't choose. In the next step you get a proliferation of competing text types all because the default one wasn't defined as an adt. Furthermore it isn't god given that the right way to iterate a text is character by character. Word by word or line by line is equally common. It's therefore completely arbitrary whether it's a character sequence, a line sequence or something else.
- serichsen 13y agoNo amount of abstraction will save you from finally having to determine how to represent the end result. That is totally orthogonal to the ability to delay the accumulation of something to be represented that way (which is just one strategy to achieve efficiency for this case anyway).