dump-yaml-json.txt

=head1 YAML vs JSON vs Data::Dumper/eval

 * Test case:  Which is fastest? eval/Data::Dumper, YAML or YAML::XS?
 * Conclusion: YAML::XS is pretty fast :)
 * Tester:     Jan Henning Thorsen
 * Test app:   See below

Benchmark: timing 10 iterations of dumper, json_xs, yaml_xs...

=head2 Dump

  dumper: 107 wallclock secs
         (103.35 usr +  1.73 sys = 105.08 CPU) @  0.10/s (n=10)
 json_xs:  7 wallclock secs
         ( 4.62 usr +  1.97 sys =  6.59 CPU) @  1.52/s (n=10)
    yaml: 1682 wallclock secs
         (1604.92 usr +  3.04 sys = 1607.96 CPU) @  0.01/s (n=10)
 yaml_xs: 66 wallclock secs
         (65.20 usr +  0.86 sys = 66.06 CPU) @  0.15/s (n=10)

=head2 Read

  dumper: 175 wallclock secs
         (79.37 usr + 95.10 sys = 174.47 CPU) @  0.06/s (n=10)
 json_xs: 19 wallclock secs
         (18.68 usr +  0.00 sys = 18.68 CPU) @  0.54/s (n=10)
    yaml: 3278 wallclock secs
         (3157.31 usr + 23.58 sys = 3180.89 CPU) @  0.00/s (n=10)
 yaml_xs: 50 wallclock secs
         (48.53 usr +  1.30 sys = 49.83 CPU) @  0.20/s (n=10)

=cut

#==============================================================================
use strict;
use warnings;
use Benchmark;
use Data::Dumper;
use YAML ();
use YAML::XS ();
use JSON::XS;

my $struct = {};
my($dumper, $yaml, $yaml_xs, $json_xs);

for my $i (0..2_000) {
    my $struct2 = {};
    for my $i2 (0..500) {
        $struct2->{$i2} = $i2;
    }
    $struct->{$i} = $struct2;
}

timethese(10, {
    dumper => sub { $dumper = Dumper($struct) },
    yaml => sub { $yaml = YAML::Dump($struct) },
    yaml_xs => sub { $yaml_xs = YAML::XS::Dump($struct) },
    json_xs => sub { $json_xs = encode_json($struct) },
});

timethese(10, {
    dumper => sub { eval $dumper },
    yaml => sub { YAML::Load($yaml) },
    yaml_xs => sub { YAML::XS::Load($yaml_xs) },
    json_xs => sub { decode_json($json_xs) },
});