Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I'm writing an application that, when the user requests something, I prepare some output for them, and return it to them.

I wrote (or rather, adapted an existing) server skeleton, which accepts new connections, and creates input and output buffers for each new connection. Currently the output buffer for each user is a single string.

In my server, I get the users input, and send it on to another file to prepare the appropriate output. That file generates the output and returns it to the server, which places it in the outgoing buffer. All pretty standard, right?

So my question: the output for each request is generated in chunks, sometimes as large as a few kilos per chunk. Each request tends to have at least 2 chunks generated, and as many as 5. Currently, to add the new chunk to the existing output, I do

$outBuffer .= $newChunk

Is this copying of strings incredibly inefficient, as it seems to me that it might be? Would I be better off pushing each chunk of output onto an outgoing array. I assume that an array would be more memory inefficient, but speed is more important. Are there other issues/solutions I haven't thought of?

(It's a pretty significant change to my program, so I tried running the benchmark module on a small example I contrived. Alas, my company doesn't have perl configured correctly to run a lot of modules, benchmark being one of those)


In reply to append to a string or make an array? (memory and time efficiency) by swkronenfeld

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2022-12-03 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?