← Index
NYTProf Performance Profile   « line view »
For -e
  Run on Thu Jun 30 16:34:56 2016
Reported on Thu Jun 30 16:35:09 2016

Filename/home/s1/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Class/Singleton.pm
StatementsExecuted 15019 statements in 12.4ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
3002215.01ms5.02msClass::Singleton::::instanceClass::Singleton::instance
11123µs26µsClass::Singleton::::BEGIN@19Class::Singleton::BEGIN@19
1118µs14µsClass::Singleton::::BEGIN@20Class::Singleton::BEGIN@20
1113µs3µsClass::Singleton::::ENDClass::Singleton::END
0000s0sClass::Singleton::::_new_instanceClass::Singleton::_new_instance
0000s0sClass::Singleton::::has_instanceClass::Singleton::has_instance
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1#============================================================================
2#
3# Class::Singleton.pm
4#
5# Implementation of a "singleton" module which ensures that a class has
6# only one instance and provides global access to it. For a description
7# of the Singleton class, see "Design Patterns", Gamma et al, Addison-
8# Wesley, 1995, ISBN 0-201-63361-2
9#
10# Written by Andy Wardley <abw@wardley.org>
11#
12# Copyright (C) 1998-2008 Andy Wardley. All Rights Reserved.
13# Copyright (C) 1998 Canon Research Centre Europe Ltd.
14#
15#============================================================================
16
17package Class::Singleton;
1819µsrequire 5.004;
19228µs229µs
# spent 26µs (23+3) within Class::Singleton::BEGIN@19 which was called: # once (23µs+3µs) by parent::import at line 19
use strict;
# spent 26µs making 1 call to Class::Singleton::BEGIN@19 # spent 3µs making 1 call to strict::import
202196µs220µs
# spent 14µs (8+6) within Class::Singleton::BEGIN@20 which was called: # once (8µs+6µs) by parent::import at line 20
use warnings;
# spent 14µs making 1 call to Class::Singleton::BEGIN@20 # spent 6µs making 1 call to warnings::import
21
221200nsour $VERSION = 1.5;
231700nsmy %_INSTANCES = ();
24
25
26#========================================================================
27#
28# instance()
29#
30# Module constructor. Creates an Class::Singleton (or derived) instance
31# if one doesn't already exist. The instance reference is stored in the
32# %_INSTANCES hash of the Class::Singleton package. The impact of this is
33# that you can create any number of classes derived from Class::Singleton
34# and create a single instance of each one. If the instance reference
35# was stored in a scalar $_INSTANCE variable, you could only instantiate
36# *ONE* object of *ANY* class derived from Class::Singleton. The first
37# time the instance is created, the _new_instance() constructor is called
38# which simply returns a reference to a blessed hash. This can be
39# overloaded for custom constructors. Any addtional parameters passed to
40# instance() are forwarded to _new_instance().
41#
42# Returns a reference to the existing, or a newly created Class::Singleton
43# object. If the _new_instance() method returns an undefined value
44# then the constructer is deemed to have failed.
45#
46#========================================================================
47
48
# spent 5.02ms (5.01+7µs) within Class::Singleton::instance which was called 3002 times, avg 2µs/call: # 3000 times (5.00ms+3µs) by DateTime::TimeZone::new at line 57 of DateTime/TimeZone.pm, avg 2µs/call # 2 times (10µs+4µs) by DateTime::TimeZone::new at line 49 of DateTime/TimeZone.pm, avg 7µs/call
sub instance {
493002645µs my $class = shift;
50
51 # already got an object
523002322µs return $class if ref $class;
53
54 # we store the instance against the $class key of %_INSTANCES
553002761µs my $instance = $_INSTANCES{$class};
563002483µs27µs unless(defined $instance) {
# spent 4µs making 1 call to DateTime::TimeZone::Floating::_new_instance # spent 3µs making 1 call to DateTime::TimeZone::UTC::_new_instance
57 $_INSTANCES{$class} = $instance = $class->_new_instance(@_);
58 }
59300210.00ms return $instance;
60}
61
62
63#=======================================================================
64# has_instance()
65#
66# Public method to return the current instance if it exists.
67#=======================================================================
68
69sub has_instance {
70 my $class = shift;
71 $class = ref $class || $class;
72 return $_INSTANCES{$class};
73}
74
75
76#========================================================================
77# _new_instance(...)
78#
79# Simple constructor which returns a hash reference blessed into the
80# current class. May be overloaded to create non-hash objects or
81# handle any specific initialisation required.
82#========================================================================
83
84sub _new_instance {
85 my $class = shift;
86 my %args = @_ && ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
87 bless { %args }, $class;
88}
89
90
91#========================================================================
92# END()
93#
94# END block to explicitly destroy all Class::Singleton objects since
95# destruction order at program exit is not predictable. See CPAN RT
96# bugs #23568 and #68526 for examples of what can go wrong without this.
97#========================================================================
98
99
# spent 3µs within Class::Singleton::END which was called: # once (3µs+0s) by main::RUNTIME at line 0 of -e
END {
100 # dereferences and causes orderly destruction of all instances
10114µs undef(%_INSTANCES);
102}
103
104
10514µs1;
106
107__END__