test.pl
#!/usr/bin/perl
use strict;
use IO::Socket;
use LWP::UserAgent;
my $host = "bbs.chinaunix.net";
my $html = "";
my $start = "";
my $end = "";
my $ua = LWP::UserAgent -> new;
$ua -> timeout( 30 );
$ua -> agent( 'Mozilla/4.76 [en] (Win98; U)' );
$ua -> default_header( 'Pragma' => 'no-cache', 'Accept' => '*/*');
$start = time;
print "LWP strat " . $start . "\n";
my $resp = $ua -> get( "http://$host/" );
if ( $resp -> is_success )
{
$html = $resp->status_line ."\n\n";
$html .= $resp->headers_as_string ."\n\n";
$html .= $resp -> content;
}
$end = time;
print "LWP end " . $end . "\n";
print "LWP time = " . ($end - $start) . "\n\n";
$html = "";
my $geturl=qq~GET / HTTP/1.1
Accept: */*
User-Agent: Mozilla/4.76 [en] (Win98; U)
Host: $host
Connection: Keep-Alive
~;
$start = time;
print "IO::Socket::INET strat " . $start . "\n";
my $socket = IO::Socket::INET->new(Proto=>"tcp", PeerAddr=>"$host", Pe
+erPort=>80, Timeout => 30);
$socket->autoflush(1);
my @html;
if($socket){
print $socket "$geturl";
print "IO::Socket::INET time1 " . time . "\n";
@html=<$socket>; # Attention here, why spent so much time?
print "IO::Socket::INET time2 " . time . "\n";
close($socket);
}else{
}
$html = join("",@html);
$end = time;
print "IO::Socket::INET end " . $end . "\n";
print "IO::Socket::INET time = " . ($end - $start) . "\n\n";
Results:
C:\>perl test.pl
LWP strat 1192833431
LWP end 1192833432
LWP time = 1
IO::Socket::INET strat 1192833432
IO::Socket::INET time1 1192833432
IO::Socket::INET time2 1192833448
IO::Socket::INET end 1192833448
IO::Socket::INET time = 16
Why so many differences?
@html=<$socket>; # Attention here, why spent so much time?
Solutions?
Thank you.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.