package Tiny; # Filename Tiny.pm # The Worlds Smallest Perl Object use strict; sub new { return bless { }, shift; } 1; ------------------------------------------------------- package NotSoTiny; # Filename NotTiny.pm use strict; sub new { return bless { value => 'A value' }, shift; } sub get_value { return shift->{value}; } 1; ------------------------------------------------------ #! /usr/bin/perl -w #Filname: bench use Benchmark qw(timethese); # perl 5.6 has 'cmpthese' # which is very cool, and # much more informative # If you have 5.6 just # s/timethese/cmpthese/g use strict; use lib ('./'); use Tiny; use NotSoTiny; my ( $roll, @roll ); my $loop = 250000; # You will want this var up here to # Adjust itineratations to avoid # warnings ###### Preload Objs ################################### # Benchmark two ways one with pre constructed objs and # another without considering construction costs. my $TinyObjBase = Tiny->new(); my $NotTinyObj = NotSoTiny->new(); ###### SubRoutine Vars ################################# # To be completely fair do the same with the Vars for # the subroutines. my $TinySub; my $NotSoTinySub; ###### End Init ########### system("clear"); my $loops; for $loops ($loop) { timethese $loops, { ##### Subroutine Comparisons TinySubPreVar => sub { $TinySub = Tiny(); }, TinySubNewVar => sub { my $NewTinySub = Tiny(); }, NotSoTinySubPreVar => sub { $NotSoTinySub = NotSoTinySub(); }, NotSoTinySubNewVar => sub { my $NewNotSoTinySub = NotSoTinySub(); }, ##### Object Comparisons TinyObjReused => sub { $TinyObjBase; }, TinyObjNew => sub { my $NewTinyObj = Tiny->new(); }, NotSoTinyObjReused => sub { $TinySub = $NotTinyObj->get_value(); }, NotSoTinyObjNew => sub { my $NotSoTinyObjNew = NotSoTiny->new(); $TinySub = $NotSoTinyObjNew->get_value(); }, }; } sub Tiny { return; } sub NotSoTinySub { return 'A value'; }

In reply to Beginners OO Workbench by coreolyn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.