<?xml version="1.0" encoding="windows-1252"?>
<node id="1004211" title="Re^4: How to concatenate N binary buffers?" created="2012-11-16 09:45:40" updated="2012-11-16 09:45:40">
<type id="11">
note</type>
<author id="947751">
mantager</author>
<data>
<field name="doctext">
&lt;p&gt;So I took the time to benchmark:&lt;/p&gt;

&lt;code&gt;$ ./syswrite_or_concat.pl 1000000
Double syswrite: 
timethis 1000000:  2 wallclock secs ( 0.43 usr +  0.94 sys =  1.37 CPU) @ 729927.01/s (n=1000000)
Data concat:
timethis 1000000:  1 wallclock secs ( 0.32 usr +  0.45 sys =  0.77 CPU) @ 1298701.30/s (n=1000000)
Data join:
timethis 1000000:  1 wallclock secs ( 0.37 usr +  0.46 sys =  0.83 CPU) @ 1204819.28/s (n=1000000)
&lt;/code&gt;

&lt;code&gt;
#!/usr/bin/env perl
# ex: set tabstop=4 et syn=perl:

use strict;
use warnings;
use 5.010.001;

use Benchmark qw(timethis);
use Fcntl;

my $count = shift || 1_000_000;
my $file = '/dev/shm/outfile';

my $bufsize = 256;
my $data1 = chr(1) x $bufsize;
my $data2 = chr(2) x $bufsize;


say "Double syswrite: ";
my $out;
sysopen($out, $file, O_WRONLY|O_CREAT) 
    or die "unable to write on $file";
timethis($count, sub {
    syswrite ($out, $data1, $bufsize) == $bufsize 
        or die "unable to write whole data1 buffer";
    syswrite ($out, $data2, $bufsize) == $bufsize 
        or die "unable to write whole data2 buffer";
});
close $out;

say "Data concat:";
sysopen($out, $file, O_WRONLY|O_CREAT) 
    or die "unable to write on $file";
my $doublebuf = 2 * $bufsize;
timethis($count, sub {
    syswrite ($out, $data1.$data2, $doublebuf) == $doublebuf 
        or die "unable to write all data";
});
close $out;

say "Data join:";
sysopen($out, $file, O_WRONLY|O_CREAT) 
    or die "unable to write on $file";
timethis($count, sub {
    syswrite ($out, join('', $data1, $data2), $doublebuf) == $doublebuf 
        or die "unable to write all data";
});
close $out;

unlink $file;

&lt;/code&gt;</field>
<field name="root_node">
1003666</field>
<field name="parent_node">
1003761</field>
</data>
</node>
