Question

Discuss what happens when strings are concatenated together within the computer's memory. Describe any ideas on...

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.

Homework Answers

Answer #1

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

  1. simple concatenation-->:( s1 += s2)
  2. MutableString --> (s1 += s2, where s1 is a MutableString)
  3. Append string using sizable array of char
  4. Constructing a collection of strings, then invoke .join()
  5. Write IO operation using cStringIO memory alloc buffer with in build method write()
  6. List comprehension Inline method using join

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)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Question -Organizational change goes beyond promotions and the threat of layoffs. What ways other than those...
Question -Organizational change goes beyond promotions and the threat of layoffs. What ways other than those discussed in the case would you use to entice people to embrace proposed changes? Provide several suggestions and justify their rationale. CASE STUDY- Blue Cross and Blue Shield, and Others: Understanding the Science behind Change Kevin Sparks has been trying to get his staff to change the way it monitors and supports the data center for the past year, but he hasn’t been getting...
What are 4 key things you learned about the topic from reading their paper? How does...
What are 4 key things you learned about the topic from reading their paper? How does the topic relate to you and your current or past job? Critique the paper in terms of the organization and quality. Team 3 answer questions above. Part I In today’s world we see fear among people when dealing with sexual harassment. This leads to people not reporting sexual harassment. A misconception about sexual harassment is that it’s only about touching and forcing other people...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect...
read Seasons of Love chapter:measuring a child's life after suicide. please answer the questions : reflect on what happens to the families when there is a suicide in the family, based on the Seasons of Love chapter...how should people be told? What details are best left unshared? below is the story These theories may have a certain face-validity, but they often neglect environmental or contextual factors that are innate to answering the question of “why” a person might engage in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT