Skip to content

Instantly share code, notes, and snippets.

@bert-w
Created May 20, 2024 12:25
Benchmark for Collection avg/average
<?php
namespace Illuminate\Tests\Support;
use Illuminate\Support\Benchmark;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
class BenchmarkTest extends TestCase
{
public function testAvg()
{
$sizes = [1e1, 1e2, 1e3, 1e4, 1e5, 1e6];
foreach($sizes as $size) {
$collection = Collection::range(1, $size)->map(fn ($i) => random_int(100, 1000));
dump("[NEW] size=$size " . Benchmark::measure( fn () => $collection->avg()) . 'ms');
}
foreach($sizes as $size) {
$collection = Collection::range(1, $size)->map(fn ($i) => random_int(100, 1000));
dump("[OLD] size=$size " . Benchmark::measure( fn () => $collection->avgOld()) . 'ms');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment