Created
March 25, 2016 11:15
-
-
Save unera/f6cb43f57f958013e35f to your computer and use it in GitHub Desktop.
pipe vs socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
use utf8; | |
use open qw(:std :utf8); | |
use Benchmark qw(:all) ; | |
use Socket; | |
sub null_reader($) { | |
my ($fh) = @_; | |
my $cnt = 0; | |
my $str; | |
while(1) { | |
read $fh, $str, 1024 or exit; | |
$cnt++; | |
} | |
} | |
sub writer($) { | |
my ($fh) = @_; | |
print $fh "b" x 512; | |
} | |
my ($r, $w); | |
pipe $r, $w; | |
my ($rs, $ws); | |
socketpair $rs, $ws, AF_UNIX, SOCK_STREAM, PF_UNSPEC; | |
if (!fork) { | |
close $w; | |
null_reader $r; | |
} | |
if (!fork) { | |
close $ws; | |
null_reader $rs; | |
} | |
cmpthese( | |
10000000 => { | |
pipe => sub { writer $w }, | |
socket => sub { writer $ws } | |
} | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rate pipe socket | |
pipe 513084/s -- -5% | |
socket 540833/s 5% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment