PHP Classes

Suggested speed update

Recommend this page to a friend!

      Get Image Color  >  All threads  >  Suggested speed update  >  (Un) Subscribe thread alerts  
Subject:Suggested speed update
Summary:Speed up the script by removing round() - 20% faster
Messages:1
Author:Fredrik Eriksson
Date:2012-09-03 18:14:39
 

  1. Suggested speed update   Reply   Report abuse  
Picture of Fredrik Eriksson Fredrik Eriksson - 2012-09-03 18:14:39
Speed and optmization comes into mind when you are using nested for-loops that goes through all pixels in an image.

A quick look and I found the following:
$red = round(round(($rgb['red'] / 0x33)) * 0x33);
$green = round(round(($rgb['green'] / 0x33)) * 0x33);
$blue = round(round(($rgb['blue'] / 0x33)) * 0x33);

The inner round is need because it might end up as a float. But then the rounded number (Integer) is multiplied by 0x33 (51) and two integers multiplied can't result in a float but only a Integer.

Faster code would be:
$red = round($rgb['red'] / 0x33) * 0x33;
$green = round($rgb['green'] / 0x33) * 0x33;
$blue = round($rgb['blue'] / 0x33) * 0x33;

This small change gave me about 20% faster execution.