Skip to content

Instantly share code, notes, and snippets.

@aero
Created July 22, 2013 02:15
Show Gist options
  • Select an option

  • Save aero/6050921 to your computer and use it in GitHub Desktop.

Select an option

Save aero/6050921 to your computer and use it in GitHub Desktop.

Revisions

  1. aero created this gist Jul 22, 2013.
    83 changes: 83 additions & 0 deletions bench.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    #!/usr/bin/env perl
    use 5.012;
    use warnings;
    use blib;
    use Benchmark qw/cmpthese/;
    use mop; # 2013-07-22


    {
    package Raw;
    sub new {
    return bless { a => 1 }, shift;
    }
    sub get_a {
    my $self = shift;
    return $self->{a};
    }
    sub set_a {
    my $self = shift;
    my $a = shift;
    $self->{a} = $a;
    }
    }

    class MOP {
    has $a is rw = 1;

    method get_a {
    return $a;
    }
    method set_a($new_a) {
    $a = $new_a;
    }
    }


    cmpthese(100000, {
    Raw_create => sub {
    Raw->new();
    },
    MOP_create => sub {
    MOP->new();
    },
    }
    );

    my $R = Raw->new();
    my $M = MOP->new();

    cmpthese(100000, {
    Raw_get => sub {
    $R->get_a;
    },
    MOP_get => sub {
    $M->get_a;
    },
    }
    );

    cmpthese(100000, {
    Raw_set => sub {
    $R->set_a(3);
    },
    MOP_set => sub {
    $M->set_a(3);
    },
    }
    );

    __END__
    RESULT:
    Rate MOP_create Raw_create
    MOP_create 3127/s -- -100%
    Raw_create 833333/s 26550% --
    Rate MOP_get Raw_get
    MOP_get 14749/s -- -100%
    Raw_get 3333333/s 22500% --
    Rate MOP_set Raw_set
    MOP_set 13021/s -- -99%
    Raw_set 2000000/s 15260% --