title: Slow swift code after dynamically calling class method
body: <p>I'm trying to write a Swift class (in Playground) that takes a dictionary of strings (such as <code>PixelGreyScale</code>) and applies each sequentially to each pixel in an image.</p>
  
  <p>I have it working with a dictionary of class methods, but it's very slow and I can't pass parameters.</p>
  
  <p>In the code below, my class is instantiated and then the <code>Process</code> method is run, taking in a dictionary of methods (<a href="https://stackoverflow.com/a/30286375/12280">a technique I saw on SO</a> - is there a better way to dynamically run a method from a string variable?):</p>
  
  <pre><code>// Create an instance of the class, passing in the raw image
  let p = ImageProcessor(src: rawImage);
  
  // use a dictionary of method calls to queue up the process
  let stack = [
      { p.PixelGreyScale() }
  ]
  
  // run the process
  p.Process(stack)
  </code></pre>
  
  <p>The <code>Process</code> method calls each method in the stack and sets the current pixel to the return value:</p>
  
  <pre><code>func Process(stack: [() -&gt; Pixel]) {
          for y in 0..&lt;rgba.height {
              for x in 0..&lt;rgba.width {
                  let index = y * rgba.width + x
                  currentIndex = index
                  rgba.pixels[index] = stack[0]()
              }
          }
  }
  </code></pre>
  
  <p>This code runs, but I'm clearly not doing it right! I'm having the following problems:</p>
  
  <ol>
  <li>It takes an <em>age</em> to run, unless the <code>stack</code> dictionary is empty. Removing the <code>stack[0]()</code> call doesn't speed things up - it seems that having something in the stack causes a slowdown when getting the pixel at a given index.</li>
  <li>Because of how I'm using the <code>stack</code> dictionary to call methods, I can't pass parameters and have to resort to moving things into and out of class variables. I've tried using <code>performSelector</code> but got exceptions every time I tried to use parameters.</li>
  </ol>
  
  <p>Is there a way of passing in a dictionary of strings and using this to control which methods are executed, <em>with</em> parameters and without speed issues?</p>