Discuss what happens when strings are concatenated
together within the computer's memory.
Describe any ideas on how to make the concatenation
process as fast as possible considering the immutability of
strings.
this is for fundamentalls of programming and logic and current topics are arround python programing I need help answering these questions as we have no text books.
When strings are concatenated together within the computer's memory
Explanation: String datatype is immutable in python that means it cannot be changed in place memory location. Any changes to string allocate a new memory location. Due to immutable nature string concatenation operation is memory intensive.
For given example string str
str = "hi"
str += ", there!"
CPython interpreter executes string concatenation code, conceptually it performs below set of task
1. Allocate memory buffer for variable string "hi" 2 bytes =>
len("hi") + 1, and copy the value into it
2. Datatype string variable str reference value "hi"3.
3. Allocate a new memory with size 11 bytes => len("hi") +
len(", there") + 1
4. Later copy "hi", then ", there" into new memory buffer, and
variable str reference it
5. Finally it dispose-decrement reference on the old buffer memory
string, eventually memory is release.
Ideas to make the concatenation process as fast as possible considering the immutablility of strings
Explanation:
There are many out of the box and
custom way to optimize or call string concatenation as per the size
of operation. Some of them are listed below. Careful every version
of python gives you different set of performance response time
throughoutput
Based on code execution analysis List comprehension inline method is much faster when run on test data for large string data sets. It is O(1) operation takes one cpu cycle to perform the task as compare to other immutable operations which consume time on memory and cpu cylce.
''.join(str for str in largeStringList)
Get Answers For Free
Most questions answered within 1 hours.