title: Objective-C graphics drawing speed
body: <p>I'm looking to draw to the screen, pixel by pixel. I've got this code working but its slow... I find this very very odd as the same kind of code written in javascript is super fast. fast enough that I has to slow down my interval that calling this...</p>
  
  <p>What do I need to do to be able to speed this up by at least 2 or 3 times?</p>
  
  <p><strong>objective-c</strong></p>
  
  <pre><code>- (void)drawPixelAtX:(int)X atY:(int)Y
  {
      [NSBezierPath fillRect:NSMakeRect(X, self.frame.size.height - Y, 1, 1)];
  }
  
  
  - (void)drawCharater:(int)charCode atX:(int)charX atY:(int)charY color:(NSColor*)color;
  {
      if (charCode &gt;= [self.font count])
          return;
  
      [color set];
      NSArray *template = [self.font objectAtIndex:charCode];
      for (int y = 0; y &lt; 16; y++) {
          for (int x = 0; x &lt; 8; x++) {
              if ([[template objectAtIndex:y*8+x] intValue] == 1)
                  [self drawPixelAtX:(charX * 8 + x) atY:(charY * 16 + y)+1];
          }
      }
  }
  
  
  - (void)drawRect:(NSRect)dirtyRect
  {
      [NSGraphicsContext saveGraphicsState];
      for (int x=0; x &lt; 80; x++) {
          for (int y=0; y &lt; 25; y++) {
              AnsiScreenChar *c = [self.screen objectAtIndex:(y*80)+x];
              [c.bgColor set];
              [NSBezierPath fillRect:NSMakeRect(x*8, self.frame.size.height - ((y+1)*16), 8, 16)];
              [self drawCharater:c.data atX:x atY:y color:c.fgColor];
          }
      }
      [NSGraphicsContext restoreGraphicsState];
  }
  </code></pre>
  
  <p><strong>javascript</strong></p>
  
  <pre><code>function updateDisplay() {
      var $this = $(this);
      var data = $this.data('ansi');
  
      for (var i = 0, length1 = data.screen.length; i &lt; length1; ++i) {
          var a = data.screen[i]; // cache object
          for (var j = 0, length2 = a.length; j &lt; length2; ++j) {
              if (a[j][4]) {
                  data.ctx.save();
                  data.ctx.beginPath();
                  data.ctx.rect(data.fontWidth * j, data.fontHeight * i, data.fontWidth, data.fontHeight);
                  data.ctx.clip();
  
                  data.ctx.fillStyle = a[j][0];
                  data.ctx.fillRect(data.fontWidth * j, data.fontHeight * i, data.fontWidth, data.fontHeight);
  
                  data.ctx.fillStyle = a[j][1];
                  drawCharater.call(this, a[j][2], [j, i], 1);
                  data.ctx.restore();
  
                  a[j][4] = false;
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Profileing info</strong></p>
  
  <pre><code>Running Time    Self        Symbol Name
  12371.0ms   21.8%   12371.0     objc_msgSend
  2612.0ms    4.6%    2612.0      CFNumberGetValue
  2446.0ms    4.3%    2446.0      _class_getInstanceSize
  1910.0ms    3.3%    1910.0      -[__NSArrayI objectAtIndex:]
  1482.0ms    2.6%    1482.0      _CFExecutableLinkedOnOrAfter
  1384.0ms    2.4%    1384.0      object_getIndexedIvars
  1350.0ms    2.3%    1350.0      -[AnsiView drawCharater:atX:atY:color:]
  1277.0ms    2.2%    1277.0      OSAtomicCompareAndSwap64Barrier$VARIANT$mp
  1270.0ms    2.2%    1270.0      ripl_BltShape
  1261.0ms    2.2%    1261.0      -[__NSCFNumber intValue]
  1185.0ms    2.0%    1185.0      CFRelease
  1047.0ms    1.8%    1047.0      CGSShmemGuardUnlock
  1005.0ms    1.7%    1005.0      ripr_Rectangles
  </code></pre>
  
  <p>I take it CFNumberGetValue is dealing with the <code>intValue</code> call?</p>