title: Possibilities to speed up PHP-CLI script?
body: <p>I wrote a PHP-CLI script that mixes two audio (.WAV PCM) files (with some math involved) so PHP needs to crunch through thousands (if not even millions) of samples with <code>unpack()</code>, do math on them and save them with <code>pack()</code>.</p>
  
  <p>Now, I dont need actual info on how to do the mixing or anything, as the title says, I'm looking for possibilites to speed this process up since the script needs 30 seconds of processing time to produce 10 seconds of audio output.</p>
  
  <p>Things that I tried:</p>
  
  <ul>
  <li>Cache the audiofiles to memory and crunch through with <code>substr()</code> instead of <code>fseek()</code>/<code>fread()</code>. Performance gain: 3 seconds.</li>
  <li>Write the output file in 5000-samples chunks. Performance gain: 10 seconds.</li>
  </ul>
  
  <p>After those optimizations I ended up at approximately 17 seconds processing time for 10 seconds audio output. What bugs me, is that other tools can do simple audio operations like mixing two files in realtime or even much faster.</p>
  
  <p>Another idea I had was paralellization, but I refrained from that due to the extra problems that would occur (like calculating correct seek positions for the forks/threads and other related things).</p>
  
  <p>So am I missing stuff out or is this actually good performance for a PHP-CLI script?</p>