Skip to content

Uses more memory than native PHP stream filters when call stream_copy_too_stream() #54

@guanguans

Description

@guanguans

File size: 106.38 MB, Time: 00:00.175, Memory: 333.17 MB

<?php

declare(strict_types=1);

use SebastianBergmann\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Timer;

require __DIR__.'/vendor/autoload.php';

$timer = new Timer;
$timer->start();
$resourceRead = fopen($file = '/.../laravel-2024-09-12.log', 'rb');
$resourceWrite = fopen('/.../laravel-2024-09-12_copy.log', 'wb');
Clue\StreamFilter\append($resourceWrite, strtoupper(...));
// stream_filter_append($resourceWrite, 'string.toupper');
stream_copy_to_stream($resourceRead, $resourceWrite);
echo sprintf('File size: %.2f MB, ',
    filesize($file) / 1024 / 1024), (new ResourceUsageFormatter)->resourceUsage($timer->stop());

// Output:
// File size: 106.38 MB, Time: 00:00.175, Memory: 333.17 MB

File size: 106.38 MB, Time: 00:00.089, Memory: 120.39 MB(using string.toupper)

<?php

declare(strict_types=1);

use SebastianBergmann\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Timer;

require __DIR__.'/vendor/autoload.php';

$timer = new Timer;
$timer->start();
$resourceRead = fopen($file = '/.../laravel-2024-09-12.log', 'rb');
$resourceWrite = fopen('/.../laravel-2024-09-12_copy.log', 'wb');
// Clue\StreamFilter\append($resourceWrite, strtoupper(...));
stream_filter_append($resourceWrite, 'string.toupper');
stream_copy_to_stream($resourceRead, $resourceWrite);
echo sprintf('File size: %.2f MB, ',
    filesize($file) / 1024 / 1024), (new ResourceUsageFormatter)->resourceUsage($timer->stop());

// Output:
// File size: 106.38 MB, Time: 00:00.089, Memory: 120.39 MB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions