- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想提前为这篇冗长的帖子道歉,但我想让你清楚地了解我的问题。感谢您的帮助。
我有一个名为 $data 的数组,格式如下:
......
给定一个任意的开始日期,我需要在数组中搜索匹配的日期:
//$holidays is an array with dates of public holidays which are not considered business days
$search_results = array();
$minDate = -10;
$maxDate = 100;
$start_date = "2015-02-25";
echo "Before loop: " . xdebug_time_index() . "<br>";
for ($i=$minDate; $i<=$maxDate; $i++) {
if (in_array_r(getBusinessDay(new DateTime($start_date), $holidays, $i), $data)){
$a_date = getBusinessDay(new DateTime($start_date), $holidays, $i);
$a_key = array_search($a_date, array_column($data, "date"));
$search_results[]=array($i, $data[$a_key]["data"]);
}
}
echo "After loop: " . xdebug_time_index() . "<br>";
var_dump($search_results);
但是,此代码片段可能会在页面加载时运行 10-15 次,每次执行都需要很长时间(在较大的数组上至少需要 6 秒):
能否请您帮助我了解是哪部分代码导致了这种延迟,我怎样才能加快这个过程?
预先感谢您的帮助。
以下是代码片段中使用的函数:
function getBusinessDay($startdate, $holidays, $days) {
$calculator = new BusinessDaysCalculator($startdate, $holidays, [BusinessDaysCalculator::SATURDAY, BusinessDaysCalculator::SUNDAY]);
$calculator->addBusinessDays($days);
$result = $calculator->getDate()->format('Y-m-d');
unset($calculator);
return $result;
}
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
返回下一个工作日的计算器(跳过周末和 $holidays 数组中的任何日期):
class BusinessDaysCalculator {
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
/**
* @param DateTime $startDate Date to start calculations from
* @param DateTime[] $holidays Array of holidays, holidays are no considered business days.
* @param int[] $nonBusinessDays Array of days of the week which are not business days.
*/
public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
$this->date = $startDate;
$this->holidays = $holidays;
$this->nonBusinessDays = $nonBusinessDays;
}
public function addBusinessDays($howManyDays) {
$i = 0;
while ($i < abs($howManyDays)) {
if ($howManyDays < 0) {
$this->date->modify("-1 day");
} else {
$this->date->modify("+1 day");
}
if ($this->isBusinessDay($this->date)) {
$i++;
}
}
}
public function getDate() {
return $this->date;
}
private function isBusinessDay(DateTime $date) {
if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
return false; //Date is a nonBusinessDay.
}
foreach ($this->holidays as $day) {
if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
return false; //Date is a holiday.
}
}
return true; //Date is a business day.
}
}
更新 1:我将 $data 数组的结构更新为
循环到:
for ($i=$minDate; $i <= $maxDate; $i++) {
$day = getBusinessDay(new DateTime($start_date), $holidays, $i);
if (array_key_exists($day, $data)) {
$search_results[]=array($i, $data[$day]);
}
}
时间略有改善:
array_key_exists 是延迟的原因吗?
更新 2:这是 $holidays 数组(它是静态的,始终相同):
最佳答案
根据您的评论,日期列包含唯一值。由于您仅在日期列上进行过滤,因此通过日期列为数组编制索引效率更高,因此您应该像这样重组数据:
$data = array(
'2015-02-19' => 1.35625,
'2015-02-20' => 1.4015,
'2015-02-23' => 0.9095,
'2015-02-24' => 1.0635,
'2015-02-25' => 1.08775,
'2015-02-26' => 0.947,
/* ... */
)
in_array
需要在整个数组中循环一个对应于一个日期的元素,这对于大型数组来说可能很慢。使用此结构,您可以通过使用 $data[$date]
访问数据来立即获取数据。
关于PHP 搜索数组速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28983988/
我在想出一个算法时遇到了麻烦... 我有一系列 GPS 数据,以 1 秒为间隔记录时间、速度、距离。假设距离是米,速度是米/秒。可能有超过 2 小时的数据,或 7200 个点。这里的“时间”字段主要是
使用java排序器,即: Collections.sort(myArrayList, new Comparator() { @Override public int c
有什么区别吗 SELECT * FROM my_table 和 SELECT my_column_id FROM my_table 地点: my_table 有百万行 网站上有大量并发用户进行sql查
有2个样本。 在第一个示例中,使用 orderby 可以更快地获得结果。 (根据 phpmyadmin 速度报告) 在另一个例子中,我没有使用 order by,它给出的结果较慢。 (根据 phpmy
我注意到,如果我将训练数据加载到内存中并将其作为 numpy 数组提供到图中,与使用相同大小的 shuffle 批次相比,速度会有很大差异,我的数据有大约 1000 个实例。 使用内存 1000 次迭
我在 python 中使用破折号。我正在绘制记录到 SQLite 数据库中的实时数据,目前,我正在绘制单个值与时间线图。我计划再添加 20 个图表,但目前,随着时间的增加, plotly 变慢,我认为
我试图调用 hasNext Velocity 模板中的方法,以便根据 foreach 循环中的位置影响行为 - 仅 hasNext没有按照文档工作。 这是 Velocity 用户指南的片段,关于 ha
在我正在制作的游戏中,我有两个点,pt1 和 pt2,我想计算出它们之间的角度。我已经在较早的计算中计算出距离。显而易见的方法是对垂直距离上的水平距离进行反正切 (tan(theta) = opp/a
我经常遇到字符串值不存在和/或为空的情况。这是测试这种情况的最佳方法吗? #if( $incentive.disclaimer && $!incentive.disclaimer != '' )
我想将一个模板nested包含在其他模板cont1,cont2和cont3中。 并且嵌套模板应仅对cont1隐藏一个特定控件。 在包含在cont1中之前,我想为一些标志变量$hideMyControl
是否可以更改从“Windows Azure Media Encoder”输出的音频的播放速度? 我正在使用配置为“WMA High Quality Audio”的“Windows Azure Medi
我使用速度将String(template)与字段合并 hi there I'am ${name}, And I'am ${age} old. velocity将字段${name}和${age}与一种
我使用的是 LockedBitmap 类,它简化了 C# 中位图数据的处理。目前它正在将数据复制到本地 byte[] 数组中,然后通过其类方法访问该数组以获取/设置像素颜色值。 这比直接通过指针访问锁
我尝试在 VM_global_library.vm 文件中添加一堆 #set($x=abc) 语句,但这些变量在我的 VM 模板中不可用。 我想为图像的基本路径等设置一个全局变量。这可能吗? 最佳答案
我的项目结构: -src --main ---java ----makers -----SomeClass ---resources ----htmlPattern.vm 如何告诉 SomeClass
我正在尝试从 Velocity 中的字符串中删除不需要的字符(换行符可以,但不能像 EM 和 CAN ASCII 控制字符那样)。 #set($cleanScreen = $cleanScreen.r
我想在日.月.年之间的点处分割日期。例如:2015 年 1 月 14 日至 {14, 01, 2015}这是我使用的代码:dates3.get(0) 包含我从页面的文本字段获取的字符串“14.01.2
之后,从 1.5 升级到速度引擎 1.7 出现了 1.5 没有的问题。为了解释这个问题,我必须展示一个代码片段: #foreach($someVariable in $someCollection)
我想知道从表中选择所有字段是否更快: SELECT * 或只选择您真正需要的: SELECT field1, field2, field3, field4, field5... 假设表有大约 10 个
我正在尝试模仿照片应用程序的行为,在该应用程序中,用户用手指平移照片并且照片具有一定的速度。由于我不会深入的原因,我不能将 UIScrollView 与它的缩放 UIImageView 一起使用,而是
我是一名优秀的程序员,十分优秀!