replacement of slow unpack

C

cyl

this block of code took about 25 seconds in my computer

open A,"a_500mb_file";
binmode A;
while(sysread(A,$x,256)){
#do nothing
}
close A;

and this took 215 seconds

open A,"a_500mb_file";
binmode A;
while(sysread(A,$x,256)){
my @c=unpack('C*',$x);
}
close A;

So why is unpack so slow here? I thought it very fast before. Do I use
it in a wrong way or is there any replacement? Thanks.
 
J

John W. Krahn

cyl said:
this block of code took about 25 seconds in my computer

open A,"a_500mb_file";
binmode A;
while(sysread(A,$x,256)){
#do nothing
}
close A;

and this took 215 seconds

open A,"a_500mb_file";
binmode A;
while(sysread(A,$x,256)){
my @c=unpack('C*',$x);
}
close A;

So why is unpack so slow here? I thought it very fast before. Do I use
it in a wrong way or is there any replacement? Thanks.

Yes it is slow, but it is faster then the alternatives. The lesson here is
try to use the contents of $x without dividing it up into individual characters.



John
 
C

cyl

John W. Krahn 寫é“:
Yes it is slow, but it is faster then the alternatives. The lesson here is
try to use the contents of $x without dividing it up into individual characters.

In my case, the contents are network packet. If I don't unpack it, is
there other way to analyze it (like parsing IP header) with the binary
string?
 
X

xhoster

cyl said:
John W. Krahn =E5=AF=AB=E9=81=93=EF=BC=9A


In my case, the contents are network packet. If I don't unpack it, is
there other way to analyze it (like parsing IP header) with the binary
string?

Don't unpack the whole thing, just the header.

Xho
 
J

John W. Krahn

cyl said:
John W. Krahn 寫é“:


In my case, the contents are network packet. If I don't unpack it, is
there other way to analyze it (like parsing IP header) with the binary
string?

It depends on how you want to analyse it.


John
 
C

cyl

At last, I wrote the parsing code in C and use swig to wrap it so I can
still use perl to write non-critical parts. The improvement is amazing.
The time is reduced to 1/5.
 

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

Forum statistics

Threads
474,197
Messages
2,571,040
Members
47,634
Latest member
RonnyBoelk

Latest Threads

Top