Skip to content

Instantly share code, notes, and snippets.

@soldair
Created June 28, 2011 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soldair/1052268 to your computer and use it in GitHub Desktop.
Save soldair/1052268 to your computer and use it in GitHub Desktop.
fastest way to get a hash based on the githash in php
<?php
/*
~>php bench.php
md5_file 634.3932 ms
hash_file/md5 714.4802 ms
parse contents 911.6130 ms
parse contents substr 852.8361 ms
contents first chunk 590.0660 ms
all ran with 30000 itterations
*/
$t = microtime(true);
for($i = 0;$i<30000;$i++) {
md5_file('api/.git/FETCH_HEAD');
}
echo sprintf('md5_file %0.4f ms',(microtime(true)-$t)*1000)."\n";
$t = microtime(true);
for($i = 0;$i<30000;$i++) {
hash_file('md5','api/.git/FETCH_HEAD');
}
echo sprintf('hash_file/md5 %0.4f ms',(microtime(true)-$t)*1000)."\n";
$t = microtime(true);
for($i = 0;$i<30000;$i++) {
current(explode("\t",file_get_contents('api/.git/FETCH_HEAD')));
}
echo sprintf('explode contents %0.4f ms',(microtime(true)-$t)*1000)."\n";
$t = microtime(true);
for($i = 0;$i<30000;$i++) {
$str = file_get_contents('api/.git/FETCH_HEAD');
substr($str,0,strpos($str,"\t"));
}
echo sprintf('parse contents substr %0.4f ms',(microtime(true)-$t)*1000)."\n";
$t = microtime(true);
for($i = 0;$i<30000;$i++) {
file_get_contents('api/.git/FETCH_HEAD',false,null,-1,40);
}
echo sprintf('contents first chunk %0.4f ms',(microtime(true)-$t)*1000)."\n";
@moledet
Copy link

moledet commented Feb 21, 2016

$t = microtime(true);
for($i = 0;$i<30000;$i++) {

$versionNumber = filemtime(__DIR__.'/../.git/');
}
echo sprintf('md5_file %0.4f ms',(microtime(true)-$t)*1000)."\n";

on my system 28.3351 ms

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