Graphics Programs Reference
In-Depth Information
timeofday
, it will be replaced with its caller,
time
. If you charge
time
, it will be re-
placed with its caller,
drawRect:
. The
drawRect:
method will move to near the top
of the list; it now is now charged with
time
,
gettimeofday
, and
mach_absolute_time
.
Some common function calls always use a lot of CPU time. Most of the time, these are
harmless and unavoidable. For example, the
objc_msgSend
function is the central dis-
patch function for any Objective-C message. It occasionally creeps to the top of the list
when you are sending lots of messages to objects. Usually, it's nothing to worry about.
However, if you are spending more time dispatching messages than actually doing work
in the triggered methods
and
your application isn't performing well, you have a problem
that needs solving.
As a real world example, an overzealous Objective-C developer might be tempted to cre-
ate classes for things like vectors, points, and rectangles. These classes would likely have
methods to add, subtract, or multiply instances as well as accessor methods to get and set
instance variables. When these classes are used for drawing, the code has to send a lot of
messages to do something simple, like creating two vectors and adding them together. The
messages add excessive overhead considering the simplicity of the operation. Therefore,
the better alternative is to create data types like these as structures and access their
memory directly. (This is why
CGRect
and
CGPoint
are structures and not Objective-C
classes.)
Don't forget to remove the CPU cycle-wasting code in
drawRect:
!
Leaks Instrument
Another useful instrument is
Leaks
. Although this instrument is less useful now that ARC
handles memory management, we know that there is still a possibility of leaking memory
with a retain cycle.
Leaks
can help us find retain cycles.
First, we need to introduce a retain cycle into our application. Let's pretend that every
Line
needs to know what array of lines it belongs to. Add a new property to
Line.h
.
@property (nonatomic, strong) NSMutableArray *containingArray;
Synthesize this property in
Line.m
.
@implementation Line
@synthesize begin, end;
@synthesize containingArray;