Skip to content

Commit

Permalink
Merge pull request #258 from cakephp/diff-optimization
Browse files Browse the repository at this point in the history
Optimized diffInMonths() - 50% improvement
  • Loading branch information
markstory authored Apr 21, 2020
2 parents 01a9115 + ad3e0af commit 7e77b7d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Traits/DifferenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ trait DifferenceTrait
*/
public function diffInYears(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt ?? static::now($this->tz);
$diff = $this->diff($dt ?? static::now($this->tz), $abs);

return (int)$this->diff($dt, $abs)->format('%r%y');
return $diff->invert ? -$diff->y : $diff->y;
}

/**
Expand All @@ -63,11 +63,10 @@ public function diffInYears(?ChronosInterface $dt = null, bool $abs = true): int
*/
public function diffInMonths(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt ?? static::now($this->tz);
$diff = $this->diff($dt ?? static::now($this->tz), $abs);
$months = $diff->y * ChronosInterface::MONTHS_PER_YEAR + $diff->m;

return $this->diffInYears($dt, $abs)
* ChronosInterface::MONTHS_PER_YEAR
+ (int)$this->diff($dt, $abs)->format('%r%m');
return $diff->invert ? -$months : $months;
}

/**
Expand All @@ -91,9 +90,9 @@ public function diffInWeeks(?ChronosInterface $dt = null, bool $abs = true): int
*/
public function diffInDays(?ChronosInterface $dt = null, bool $abs = true): int
{
$dt = $dt ?? static::now($this->tz);
$diff = $this->diff($dt ?? static::now($this->tz), $abs);

return (int)$this->diff($dt, $abs)->format('%r%a');
return $diff->invert ? -$diff->days : $diff->days;
}

/**
Expand Down
60 changes: 60 additions & 0 deletions tests/Benchmark/DiffBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

/**
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Chronos\Test\Benchmark;

use Cake\Chronos\Chronos;

/**
* @BeforeMethods({"init"})
* @AfterMethods({"shutdown"})
*/
class DiffBench
{
public function init()
{
$this->from = new Chronos('2019-01-01 00:00:00');
$this->to = new Chronos('2020-01-01 00:00:00');
}

public function shutdown()
{
}

/**
* @Revs(1000)
* @Iterations(5)
*/
public function benchDiffYears()
{
$this->from->diffInYears($this->to);
}

/**
* @Revs(1000)
* @Iterations(5)
*/
public function benchDiffMonths()
{
$this->from->diffInMonths($this->to);
}

/**
* @Revs(1000)
* @Iterations(5)
*/
public function benchDiffDays()
{
$this->from->diffInDays($this->to);
}
}

0 comments on commit 7e77b7d

Please sign in to comment.