ext
3 years ago
src
3 years ago
tests
3 years ago
.travis.yml
3 years ago
LICENSE
3 years ago
Makefile
3 years ago
composer.json
3 years ago
crc32_benchmark.php
3 years ago
crc32_benchmark.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Copyright 2019 Google LLC |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | include __DIR__ . '/vendor/autoload.php'; |
| 19 | |
| 20 | use Google\CRC32\Builtin; |
| 21 | use Google\CRC32\CRC32; |
| 22 | use Google\CRC32\Google; |
| 23 | use Google\CRC32\PHP; |
| 24 | use Google\CRC32\PHPSlicedBy4; |
| 25 | |
| 26 | define('min_duration', 5); // Min duration of test in seconds. |
| 27 | define('max_duration', 30); // Max duration of test in seconds. |
| 28 | define('min_iterations', 10000); // Min number of iterations. |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | Tested on my mid-2014 MacBook Pro (with SSE4.2) |
| 33 | |
| 34 | Google CRC Benchmarks |
| 35 | CRC32CBenchmark/Public/256 214 ns 213 ns 3152131 bytes_per_second=1.11688G/s |
| 36 | CRC32CBenchmark/Public/4096 1975 ns 1974 ns 346883 bytes_per_second=1.9328G/s |
| 37 | CRC32CBenchmark/Public/65536 31805 ns 31782 ns 21701 bytes_per_second=1.92044G/s |
| 38 | CRC32CBenchmark/Public/1048576 508704 ns 508373 ns 1312 bytes_per_second=1.92096G/s |
| 39 | CRC32CBenchmark/Public/16777216 8526064 ns 8516872 ns 78 bytes_per_second=1.83459G/s |
| 40 | CRC32CBenchmark/Portable/256 363 ns 363 ns 1900480 bytes_per_second=672.139M/s |
| 41 | CRC32CBenchmark/Portable/4096 4610 ns 4607 ns 150221 bytes_per_second=847.9M/s |
| 42 | CRC32CBenchmark/Portable/65536 72886 ns 72815 ns 9168 bytes_per_second=858.341M/s |
| 43 | CRC32CBenchmark/Portable/1048576 1151197 ns 1150417 ns 585 bytes_per_second=869.25M/s |
| 44 | CRC32CBenchmark/Portable/16777216 18655381 ns 18640083 ns 36 bytes_per_second=858.365M/s |
| 45 | CRC32CBenchmark/Sse42/256 211 ns 211 ns 3245158 bytes_per_second=1.13004G/s |
| 46 | CRC32CBenchmark/Sse42/4096 1959 ns 1958 ns 347583 bytes_per_second=1.94816G/s |
| 47 | CRC32CBenchmark/Sse42/65536 32041 ns 32013 ns 21616 bytes_per_second=1.90658G/s |
| 48 | CRC32CBenchmark/Sse42/1048576 514282 ns 514035 ns 1296 bytes_per_second=1.8998G/s |
| 49 | CRC32CBenchmark/Sse42/16777216 8437749 ns 8433051 ns 78 bytes_per_second=1.85283G/s |
| 50 | |
| 51 | CRC32_PHP 256 1500 11.48 MB/s |
| 52 | CRC32_Builtin 256 1500 315.81 MB/s |
| 53 | CRC32C_Google 256 1500 1078.78 MB/s |
| 54 | CRC32_PHP 4096 1500 11.65 MB/s |
| 55 | CRC32_Builtin 4096 1500 457.41 MB/s |
| 56 | CRC32C_Google 4096 1500 10836.76 MB/s |
| 57 | CRC32_PHP 1048576 118 12.27 MB/s |
| 58 | CRC32_Builtin 1048576 1500 468.74 MB/s |
| 59 | CRC32C_Google 1048576 1500 24684.46 MB/s |
| 60 | CRC32_PHP 16777216 8 12.24 MB/s |
| 61 | CRC32_Builtin 16777216 276 461.51 MB/s |
| 62 | CRC32C_Google 16777216 1500 20221.71 MB/s |
| 63 | |
| 64 | */ |
| 65 | |
| 66 | function test($crc, $chunk_size) |
| 67 | { |
| 68 | //xdebug_start_trace(); |
| 69 | $name = get_class($crc); |
| 70 | $chunk = random_bytes($chunk_size); // TODO for php 5 use https://github.com/paragonie/random_compat |
| 71 | |
| 72 | $i = 0; |
| 73 | $now = microtime(true); |
| 74 | $start = $now; |
| 75 | $duration = 0; |
| 76 | |
| 77 | while (true) { |
| 78 | $crc->update($chunk); |
| 79 | |
| 80 | $i++; |
| 81 | $now = microtime(true); |
| 82 | $duration = ($now - $start); |
| 83 | |
| 84 | if ($duration >= max_duration) { |
| 85 | break; |
| 86 | } |
| 87 | if ($duration >= min_duration && $i >= min_iterations) { |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Very quick sanity check |
| 93 | if ($crc->hash() == '00000000') { |
| 94 | exit($name . ' crc check failed'); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | $bytes = $i * $chunk_size; |
| 99 | |
| 100 | echo sprintf("%s\t%10d\t%5d\t%8.2f MB/s\n", $name, $chunk_size, $i, $bytes / ($now - $start) / 1000000); |
| 101 | } |
| 102 | |
| 103 | foreach (array(256, 4096, 1048576, 16777216) as $chunk_size) { |
| 104 | test(new PHP(CRC32::CASTAGNOLI), $chunk_size); |
| 105 | test(new PHPSlicedBy4(CRC32::CASTAGNOLI), $chunk_size); |
| 106 | |
| 107 | // Using IEEE, avoiding the CASTAGNOLI version crc32c.so adds. |
| 108 | test(new Builtin(CRC32::IEEE), $chunk_size); |
| 109 | test(new Google(), $chunk_size); |
| 110 | } |
| 111 |