Created
October 27, 2017 08:09
-
-
Save amochohan/6526ace78008e2ecdbb7c1395af9a1f6 to your computer and use it in GitHub Desktop.
Generating a timeseries - Carbon vs native PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$timeSeries = []; | |
// Using Carbon | |
$start = \Carbon\Carbon::create(1983, 9, 1, 0, 0, 0); | |
$end = \Carbon\Carbon::create(2050, 1, 1, 0, 0,0); | |
while ($start->format('Y-m-d') < $end->format('Y-m-d')) { | |
$start->addDay(); | |
$timeSeries[] = $start->format('Y-m-d'); | |
} | |
// Generating a daily timeseries average time taken: 0.355524039 | |
// Using native PHP | |
$begin = new DateTime('1983-09-01'); | |
$end = new DateTime('2050-01-01'); | |
$interval = new DateInterval('P1D'); | |
$daterange = new DatePeriod($begin, $interval ,$end); | |
foreach($daterange as $date){ | |
$timeSeries[] = $date->format("Y-m-d"); | |
} | |
// Generating a daily timerseries average time taken: 0.036749125 (10.3x faster) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment