note 75061 deleted from function.crc32 by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Oct 2011 21:30:54 +0000 |
| Subject: | note 75061 deleted from function.crc32 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183687@lists.php.net to get a copy of this message | ||
Note Submitter: manuel at sadism-softworks dot de
----
In response to quix at free dot fr (http://www.php.net/manual/de/function.crc32.php#31832):
Through buffering his CRC32 calculation is about 2-4 times faster:
<?php
function __crc32_file($name)
{
// Creates a CRC from a file
// Info: look at __crc32_string
global $__crc32_table;
// Start out with all bits set high.
$crc = 0xffffffff;
// Open the file
$fp = fopen($name, "rb");
// Read the file using a 1MB buffer
// This will speed up CRC calculation at least 2-4x!
// (5MB file: 15sec instead of 50sec)
if ($fp != false)
{
$buffer = '';
while (!feof($fp))
{
$buffer = fread($fp, 1048576); // Buffer 1MByte of file into memory
$len = strlen($buffer); // Cache buffer length (just this saves about 10sec on a 5MB
file!)
for ($i = 0; $i < $len; $i++)
{
$crc = (($crc >> 8) & 0x00ffffff) ^ $__crc32_table[($crc & 0xFF) ^
ord($buffer[$i])];
}
}
fclose($fp);
}
// Exclusive OR the result with the beginning value.
return $crc ^ 0xffffffff;
}
?>