Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(optimize): getMultipleRunDates method #75

Merged

Conversation

oleksiikhr
Copy link

Hi, it seems to me here CronExpression@getMultipleRunDates we can speed up the results. From the last received date, calculate the next one without using $nth.

public function testPerformance(): void
{
    $start = microtime(true);

    for ($i = 0; $i < 10; $i++) {
        $cron = CronExpression::factory('*/2 * * * *');
        $cron->getMultipleRunDates(200, 'now', false, true);
    }

    $this->assertTrue(true);

    // Before: ~1.3. After: ~0.025
    die(microtime(true) - $start);
}

For verification, I added the changes to the new method and checked this way:

public function testCompare(): void
{
    $cron = CronExpression::factory('* * * * *');
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2008-11-09 00:05:00', true, true),
        $cron->getMultipleRunDates2(50, '2008-11-09 00:05:00', true, true)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2008-11-09 00:05:00', false, false),
        $cron->getMultipleRunDates2(50, '2008-11-09 00:05:00', false, false)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2008-11-09 00:05:00', false, true),
        $cron->getMultipleRunDates2(50, '2008-11-09 00:05:00', false, true)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2008-11-09 00:05:00', true, false),
        $cron->getMultipleRunDates2(50, '2008-11-09 00:05:00', true, false)
    );

    $cron = CronExpression::factory('1 1 * 3 1');
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2009-05-03 13:15:25', true, true),
        $cron->getMultipleRunDates2(50, '2009-05-03 13:15:25', true, true)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2009-05-03 13:15:25', false, false),
        $cron->getMultipleRunDates2(50, '2009-05-03 13:15:25', false, false)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2009-05-03 13:15:25', false, true),
        $cron->getMultipleRunDates2(50, '2009-05-03 13:15:25', false, true)
    );
    $this->assertEquals(
        $cron->getMultipleRunDates(50, '2009-05-03 13:15:25', true, false),
        $cron->getMultipleRunDates2(50, '2009-05-03 13:15:25', true, false)
    );
}

And, it seems working :)

@dragonmantank dragonmantank merged commit 49b8832 into dragonmantank:master Jan 5, 2022
@dragonmantank
Copy link
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants