File: freeze_thaw_timing.pl

package info (click to toggle)
libsereal-encoder-perl 4.018%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,284 kB
  • sloc: ansic: 11,838; perl: 6,004; sh: 25; makefile: 9
file content (82 lines) | stat: -rw-r--r-- 1,980 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env perl
use strict;
use warnings;
use Sereal::Encoder;
use Sereal::Decoder;

use Benchmark::Dumb qw(cmpthese);

my $enc_nocb= Sereal::Encoder->new();
my $enc_cb= Sereal::Encoder->new( { freeze_callbacks => 1 } );
my $dec= Sereal::Decoder->new();

package Foo;

sub new {
    my $class= shift;
    return bless( {@_} => $class );
}

sub FREEZE {
    my ( $self, $serializer )= @_;
    return $self->{name};    # performance
}

sub THAW {
    my ( $class, $serializer, $data )= @_;
    return Foo->new( name => $data );
}

package main;

my $data= Foo->new( name => "blargh" );
my $data_big= [];
for ( 1 .. 100 ) {
    push @$data_big, Foo->new( name => "blargh" );
}
my $data_big_nocb= [];
for ( 1 .. 100 ) {
    push @$data_big_nocb, bless( { name => "blargh" } => "Bar" );
}

my $frozen_nocb= $enc_nocb->encode($data);
my $frozen_cb= $enc_cb->encode($data);

my $frozen_big_nocb= $enc_nocb->encode($data_big);
my $frozen_big_cb= $enc_cb->encode($data_big);

my $timing= "1000.01";

print "Comparing small serialization with/out callbacks...\n";
cmpthese(
    $timing,
    {
        cb    => sub { $enc_cb->encode($data) },
        no_cb => sub { $enc_nocb->encode($data) },
    } );

print "Comparing big serialization with/out callbacks...\n";
cmpthese(
    $timing,
    {
        cb             => sub { $enc_cb->encode($data_big) },
        no_cb          => sub { $enc_nocb->encode($data_big) },
        cb_nocbdata    => sub { $enc_cb->encode($data_big_nocb) },
        no_cb_nocbdata => sub { $enc_nocb->encode($data_big_nocb) },
    } );

print "Comparing small deserialization with/out callbacks...\n";
cmpthese(
    $timing,
    {
        cb    => sub { $dec->decode($frozen_cb) },
        no_cb => sub { $dec->decode($frozen_nocb) },
    } );

print "Comparing big deserialization with/out callbacks...\n";
cmpthese(
    $timing,
    {
        cb    => sub { $dec->decode($frozen_big_cb) },
        no_cb => sub { $dec->decode($frozen_big_nocb) },
    } );