Perl Large Scalar Question?

B

Bob Mariotti

Here's one for the perl internals guru's:

We have a module that is used by many programs in our suite.
Currently it works and has been working just fine for several years.

What it does is take a string that is passed to it and sends it out a
tty port to a remote host then it enters into a read loop
contatenating all returned records until it receives one beginning
with "0999". Here is a sample snippet (NOT ACTUAL CODE)
$DATA="";
open(XXX,"+>/dev/ttyxxxx") or die $!;
# Send Request to host
print XXX "$Message\015" or die $!;
# Get Response from host
while (<XXX>) {
# Exit if Last Record Signal
if (m/^0999/) { last; }
# Contruct Host Response String
$DATA.=$_;
}
return $DATA;

If the very most cases this works just absolutely fine as the records
coming back are either one or a few or at most a dozen or two.

However, we've added a request that will return upwards of 1000+
records of 115 bytes each. The time to execute is becoming
unacceptable and my thought is that the reallocation of the scalar
variable $DATA is costing too much in time and resource.

Is there any good way to preallocate that scalar to approx 1000 x 115
before entering the loop so the re-allocation, data content move, and
destruction of the old scalar doesn't have to happen????

Also, can anyone recommend a more efficient method for obtaining and
passing these/this data back to the calling routine???

Thanks all.

Bob Mariotti
"perl is great!"
 
T

thundergnat

Bob said:
However, we've added a request that will return upwards of 1000+
records of 115 bytes each. The time to execute is becoming
unacceptable and my thought is that the reallocation of the scalar
variable $DATA is costing too much in time and resource.

Unfortunately, I would suspect that the tranfer of 115000 bytes of data
over a serial link is going to be three or more orders of magnitude
slower than allocating 115000 bytes of space in memory. It sounds like
you are trying to optimize the wrong thing.

You would likely get more bang for your buck trying to transfer the data
in larger blocks, compressing the data before it is sent, or using a
faster transport method. Not knowing the particulars of your
application, it is difficult to make any really useful observations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top