Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: AppConfig variables and OO

by Caillte (Friar)
on Jan 10, 2001 at 20:34 UTC ( [id://50931]=note: print w/replies, xml ) Need Help??


in reply to AppConfig variables and OO

Consider the following:

use Benchmark; my $foo = "bar"; my $test = new tester(); my ($buffer, $i); my $a1 = new Benchmark; for( $i=1; $i<1_000_000; $i++ ) { $buffer = $foo; } my $a2 = new Benchmark; my $b1 = new Benchmark; for( $i=1; $i<1_000_000; $i++ ) { $buffer = $test->report(); } my $b2 = new Benchmark; my $diff1 = timediff($a1, $a2); my $diff2 = timediff($b1, $b2); print "Variable copying: " . timestr($diff1) . "\n"; print "Object copying: " . timestr($diff2) . "\n"; package tester; sub new { my $class = shift; my $self = { 'foo' => "bar" }; bless $self, $class; return $self; } sub report { my $self = shift; return $self->{'foo'}; }

Running this gives (on a 1ghz pc hobbled by Win2k):

Variable copying: -1 wallclock secs (-0.81 usr + 0.00 sys = -0.81 CPU +) Object copying: -5 wallclock secs (-5.15 usr + 0.00 sys = -5.15 CPU)

Considering there are a million copies made of each the time difference isnt all that great. However, if all we wanted to do was code fast programs none of us would ever write OO :)

In other words, use a variable if calling the object is too much work :)

However there are many other reasons to code this way... have a look at This file and especially this file for some basic guidelines and explanation of OO programming.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://50931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-04-24 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found