google/crc32

多种CRC32实现

v0.2.0 2023-04-16 22:44 UTC

This package is auto-updated.

Last update: 2024-08-25 20:19:21 UTC


README

Build Status

作者:Andrew Brampton

已弃用:从PHP 8.0开始,PHP中的crc32算法使用硬件加速指令,并且速度非常快。因此,此扩展不再需要!

CRC32实现,支持所有crc32多项式,以及(如果你安装了pecl扩展)CRC32C(Castagnoli)的硬件加速版本。

支持PHP 7.4至PHP 8.2。旧版本的PHP可以通过“v0.1.0”支持。

用法

require 'vendor/autoload.php';

use Google\CRC32\CRC32;

$crc = CRC32::create(CRC32::CASTAGNOLI);
$crc->update('hello');
echo $crc->hash();

根据环境和多项式,CRC32::create将选择最快的可用版本,并返回以下类之一

  • Google\CRC32\PHP - 纯PHP实现。
  • Google\CRC32\Builtin - PHP哈希框架实现。
  • Google\CRC32\Google - 硬件加速实现(使用google/crc32c)。

当以1M字节的块读取时,使用CRC32::CASTAGNOLI在2014年Macbook Pro上运行PHP 7.4,我们得到以下性能(值越高越好)

Google\CRC32\PHP           12.27 MB/s
Google\CRC32\Builtin       468.74 MB/s (available since PHP 7.4)
Google\CRC32\Google        24,684.46 MB/s (using crc32c.so)

安装

$ composer require google/crc32

crc32c.so

要使用硬件加速,必须安装自定义PHP扩展。这使用了google/crc32c,它提供了一个高度优化的CRC32C(Castagnoli)实现,使用Intel CPU的SSE 4.2指令集。

可以从pecl安装此扩展,或从头编译。

TODO pecl install crc32c

安装或编译后,您需要在php.ini文件中添加extension=crc32c.so

编译(Linux / Mac)

请确保已安装composer、构建工具(例如build-essentialcmake等)和php开发头文件(例如php-dev)。

简单(使用Makefile)

make test

或者(手动)

cd ext

# Install the google/crc32c library
./install_crc32c.sh # From source (recommended)

# or use your favorite package manager, e.g.
# brew install crc32c

# Prepare the build environment
phpize
./configure

# or if using a custom crc32c
# ./configure --with-crc32c=$(brew --prefix crc32c)

## Build and test
make test

现在扩展位于ext/modules/crc32c.so。此文件应复制到您的扩展目录,并在php.ini中引用。

# php.ini
extension=crc32c.so

测试

make test将使用当前PHP进行测试。make test_all将查找所有可用的PHP安装,并使用它们进行测试。

基准测试

要比较不同CRC32C实现的性能,请运行make benchmark

相关

待办事项

许可(Apache 2)

这不是一个官方的Google产品(实验性的或其他的),它只是Google拥有的代码。

Copyright 2023 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://apache.ac.cn/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.