-
Notifications
You must be signed in to change notification settings - Fork 8
03 – Optimization & Caching
This section is great for both making the library run faster and understand how it works under-the-hood. However, it is not required in order to use the library, so feel free to skip this section if you feel it won't apply to your situation.
There are plenty of times where going through the trouble of optimizing how you use a library can pay off. For this library, I would recommend going through the effort only if it is possible for your application to work with more than 500 instances of the main
class at once.
Generally speaking, this library can perform most of the main
class methods over 10,000 times in a second. However, when it comes to schemes and gradients it takes considerably longer. Caching was introduced specifically to help combat this issue, but there are still plenty of cases where even that can't help you.
You should only generate schemes and gradients when you absolutely have to. Also generating standard schemes will always be faster than YIQ schemes.
For some situations, it may be better to use some of the underlying classes rather than routing through the main
class. The result will be faster calculations and fewer resources used. This comes at the cost of some additional development time.
If you are only converting a color between spectrums and don't care about the rest of the features, it is always better to use the convert
class instead of going through the main
class. However unlike the main
class, the convert
class won't magically handle the in-between conversions for you.
The below code shows an example of one of the in-between conversions that the main
class will handle for you that the convert
class can't handle.
$color = \projectcleverweb\color\main(array('h' => 0, 's' => 0, 'l' => 100));
// output 'FFFFFF'
echo $color->hex();
use \projectcleverweb\color\convert;
$color = array('h' => 0, 's' => 0, 'l' => 100);
$rgb = convert::hsl_to_rgb($color['h'], $color['s'], $color['l']);
// output 'FFFFFF'
echo convert::($rgb['r'], $rgb['g'], $rgb['b']);
Caching within PHP Color is used to temporarily store recently generated values, at the cost of some additional ram. The vast majority of the time the benefits are well worth the cost. However, if you have more time than RAM or you know for sure you will never need a value more than once from the same object, you may benefit from turning off caching. You can turn off caching with the below code:
$color = \projectcleverweb\color\main('FFFFFF');
$color->cache(FALSE);
You should always avoid making different instances for the same color. But if you need to, you should use PHP's clone keyword instead of recreating the instance through the constructor.
Copyright © 2016 Nicholas Jordon
This documentation is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.