Skip to content

Instantly share code, notes, and snippets.

@ostark
Last active July 30, 2018 08:31
Show Gist options
  • Save ostark/a7060b48e8d4ebfac9c4240a4bd248fd to your computer and use it in GitHub Desktop.
Save ostark/a7060b48e8d4ebfac9c4240a4bd248fd to your computer and use it in GitHub Desktop.
Craft micro optimization
<?php
/*
* Context:
* https://github.com/craftcms/cms/blob/3f055237e314362b079cbbf63a9be46b9cdcd7c7/src/helpers/Image.php#L65
*/
class ArrayHelper
{
public static function unique($array)
{
return array_keys(array_flip($array));
}
}
$formats = ["3fr","a","aai","ai","art","arw","avi","avs","b","bgr","bgra","bie","bmp","bmp2","bmp3","brf","c","cal","cals","canvas","caption","cin","cip","clip","cmyk","cmyka","cr2","crw","cur","cut","dcm","dcr","dcx","dds","dfont","djvu","dng","dot","dpx","epdf","epi","eps","eps2","eps3","epsf","epsi","ept","ept2","ept3","erf","exr","fax","fits","fractal","fts","g","g3","gif","gif87","gradient","gray","group4","hald","hdr","histogram","hrz","htm","html","icb","ico","icon","info","inline","ipl","isobrl","j2c","j2k","jbg","jbig","jng","jp2","jpc","jpeg","jpg","jpx","k","k25","kdc","label","m","m2v","m4v","mac","map","mat","matte","mef","miff","mng","mono","mov","mp4","mpc","mpeg","mpg","mrw","msl","msvg","mtv","mvg","nef","nrw","null","o","orf","otb","otf","pal","palm","pam","pango","pattern","pbm","pcd","pcds","pcl","pct","pcx","pdb","pdf","pdfa","pef","pes","pfa","pfb","pfm","pgm","pgx","picon","pict","pix","pjpeg","plasma","png","png24","png32","png8","pnm","ppm","preview","ps","ps2","ps3","psb","psd","ptif","pwp","r","radial-gradient","raf","ras","rgb","rgba","rgbo","rla","rle","scr","sct","sfw","sgi","shtml","sr2","srf","stegano","sun","svg","svgz","text","tga","thumbnail","tiff","tiff64","tile","tim","ttc","ttf","txt","ubrl","uil","uyvy","vda","vicar","vid","viff","vst","wbmp","wmf","wmv","wmz","wpg","x","x3f","xbm","xc","xcf","xpm","xps","xv","xwd","y","ycbcr","ycbcra","yuv"];
$time = microtime(true);
$formats = array_unique(array_merge($formats, ['svg']));
$time=microtime(true)-$time;
echo "\n merge + array_unique() to " . count($formats) . " in " . sprintf('%f', $time);
$time = microtime(true);
$formats = ArrayHelper::unique(array_merge($formats, ['svg']));
$time=microtime(true)-$time;
echo "\n merge + ArrayHelper::unique() to " . count($formats) . " in " . sprintf('%f', $time);
$time = microtime(true);
array_push($formats, 'svg');
$formats = ArrayHelper::unique($formats);
$time=microtime(true)-$time;
echo "\n push + ArrayHelper::unique() to " . count($formats) . " in " . sprintf('%f', $time);
@ostark
Copy link
Author

ostark commented Sep 7, 2017

Result (on macbook pro 2017, similar on AWS EC2 R4)

merge + array_unique()                        to 213 in 0.000032
merge + ArrayHelper::unique()                 to 213 in 0.000018
push + ArrayHelper::unique()                  to 213 in 0.000008

https://blackfire.io/profiles/compare/189f7d6d-2a7a-4065-bf94-a266be405e2a/graph

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment