Code loops optimization in PHP - php

Is there any way to optimize this piece of code to work faster? I'd appreciate any suggestions!
This piece of code processes the transferring of edges during graph creation.
foreach($times_arrival as $city_id => $time_points) {
// if city is not prohibited for transfers and there is and exists any departure times for this city
if (isset($times_departure[$city_id]) && isset($cities[$city_id]))
{
foreach($times_arrival[$city_id] as $t1_info)
{
foreach($times_departure[$city_id] as $t2_info)
{
if ($t1_info[0] != $t2_info[0]) //transfers are allowed only for different passages
{
$t1 = $t1_info[1];
$t2 = $t2_info[1];
$vertex_key = new Vertex($city_id, $t1, 1);
$vertex_key = $vertex_key->toString();
//minimum transfer time is 10 min.
if (date('H:i', strtotime($t2)) > date('H:i', strtotime('+ 10 minutes', strtotime($t1))))
{
$this->graph[$vertex_key][] = new Edge(
NULL,
$vertex_key,
new Vertex($city_id, $t2, 0),
(float) 0,
$f((strtotime($t2) - strtotime($t1)) / 60, 0, 1) //edge weight
);
}
//if transfer is on the bound of the twenty-four hours
else if (date('H:i', strtotime('+ 24 hours', strtotime($t2))) > date('H:i', strtotime('+ 10 minutes', strtotime($t1))))
{
$this->graph[$vertex_key][] = new Edge(
NULL,
$vertex_key,
new Vertex($city_id, $t2, 0),
(float) 0,
$f(strtotime('+ 24 hours', strtotime($t2)) - strtotime($t1) / 60, 0, 1) //edge weight
);
}
}
}
}
}
}
example of variables:
var_dump($times_arrival); //$times_departure have the same structure
array
3 =>
array
0 =>
array
0 => string '1' (length=1)
1 => string '08:12' (length=5)
1 =>
array
0 => string '2' (length=1)
1 => string '08:40' (length=5)
41 =>
array
0 =>
array
0 => string '21' (length=2)
1 => string '12:40' (length=5)

Thank you all!
The reason of slow speed was coz of using functions strtotime() and date().

In that case only you can say whether you chose a good or bad algorithm. In my point of view your code not has no extra computations.
Only one recommendation - use Xdebug to profile your code and find out where the bottleneck is, if possible.

Related

Algorithm to find best combination of numbers - Bin Packing Prob lem

Say I have the following measures:
80
180
200
240
410
50
110
I can store each combination of numbers to a maximum of 480 per unit. How can I calculate the least number units required so all measures are spread in the most efficient way?
I've tagged PHP but it can be in JS too, or even pseudo language.
I know I'm supposed to tell what I did already but I'm quite stuck on how to approach this. The first thing that comes to mind is recursion but I'm no math expert to see how this can be done efficient...
Any help is greatly appreciated.
To further elaborate: I'm trying to calculate the number of skirtings I have to order, based on the different lengths I need for the walls. Each skirting has a length of 480cm and I want to know the best way to spread them so I have to buy the least number of skirtings. It's not so much about ordering a skirting extra, but the puzzle to figure it out is an interesting one (at least to me)
Update with solution
Despite people trying to close the question I've started fiddling with the Bin Packing Problem description and following the idea of sorting all items from largest to smallest and then fit them in the best possible way I created this small class that might help others in the future:
<?php
class BinPacker {
private $binSize;
public function __construct($binSize) {
$this->binSize = $binSize;
}
public function pack($elements) {
arsort($elements);
$bins = [];
$handled = [];
while(count($handled) < count($elements)) {
$bin = [];
foreach($elements as $label => $size) {
if(!in_array($label, $handled)) {
if(array_sum($bin) + $size < $this->binSize) {
$bin[$label] = $size;
$handled[] = $label;
}
}
}
$bins[] = $bin;
}
return $bins;
}
public function getMeta($bins) {
$meta = [
'totalValue' => 0,
'totalWaste' => 0,
'totalBins' => count($bins),
'efficiency' => 0,
'valuePerBin' => [],
'wastePerBin' => []
];
foreach($bins as $bin) {
$value = array_sum($bin);
$binWaste = $this->binSize - $value;
$meta['totalValue'] += $value;
$meta['totalWaste'] += $binWaste;
$meta['wastePerBin'][] = $binWaste;
$meta['valuePerBin'][] = $value;
}
$meta['efficiency'] = round((1 - $meta['totalWaste'] / $meta['totalValue']) * 100, 3);
return $meta;
}
}
$test = [
'Wall A' => 420,
'Wall B' => 120,
'Wall C' => 80,
'Wall D' => 114,
'Wall E' => 375,
'Wall F' => 90
];
$binPacker = new BinPacker(488);
$bins = $binPacker->pack($test);
echo '<h2>Meta:</h2>';
var_dump($binPacker->getMeta($bins));
echo '<h2>Bin Configuration</h2>';
var_dump($bins);
Which gives an output:
Meta:
array (size=6)
'totalValue' => int 1199
'totalWaste' => int 265
'totalBins' => int 3
'efficiency' => float 77.898
'valuePerBin' =>
array (size=3)
0 => int 420
1 => int 465
2 => int 314
'wastePerBin' =>
array (size=3)
0 => int 68
1 => int 23
2 => int 174
Bin Configuration
array (size=3)
0 =>
array (size=1)
'Wall A' => int 420
1 =>
array (size=2)
'Wall E' => int 375
'Wall F' => int 90
2 =>
array (size=3)
'Wall B' => int 120
'Wall D' => int 114
'Wall C' => int 80
While the data set is relatively small a rather high inefficiency rate is met. But in my own configuration where I entered all wall and ceiling measures I've reached an efficiency of 94.212% (n=129 measures).
(Note: the class does not check for ambigious labels, so if for example you define Wall A twice the result will be incorrect.)
Conclusion: for both the ceiling and the wall skirtings I can order one less skirting than my manual attempt to spread them efficiently.
Looks to me like a variation on the Bin Packing Problem where you're trying to pick the combination of elements that make up 480 (or just under). This is a fairly computationally hard problem and depending on how efficient/accurate it needs to be, might be overkill trying to get it exact.
A rough heuristic could be just to sort the measures, keep adding the smallest ones into a unit until the next one makes you go over, then add to a new unit and repeat.

Importancy algorithm based on time

I have an array of users, as follows;
<?php
$users = array(
array(
"id" => 1,
"last_updated" => 1398933140,
"weight" => 2.0
),
array(
"id" => 2,
"last_updated" => 1398933130,
"weight" => 0
),
array(
"id" => 3,
"last_updated" => 1398933120,
"weight" => 1.0
),
array(
"id" => 4,
"last_updated" => 1398933110,
"weight" => 0
)
);
?>
I want to (asynchronously) refresh some stats on users (for which I'm using a crobjob) ordered on last_updated, in essence the user with the most stale stats.
However, I want to add weights to users and calculate that into the equation. Is it good enough to just convert the weight to some amount of seconds and substract that from the last_updated timestamp?
I know my example array has time differences of 10 seconds, but I only want to start adding the weight criteria after 3600 seconds.
// Start substracting weight
if ($timediff > 3600) {
// The longer the timediff, the heavier the weight becomes
$weight_severity = (($timediff/1000) * $weight) * SOME_CONSTANT;
// Putting the 'last_updated' back farther in the past
$substract = $timestamp - $weight_severity;
}
Is this a good 'algorithm' or will this go horribly wrong when the differences become pretty large?
At the moment I have nearly 2000 users (expected will become 10.000), so theoretically a full loop takes 2000 minutes. My concern is, will a user with a weight of 2.0 be buried under 500 'insignificant' users?
Update: I have enhanced my code a bit.
<?php
$users = array(
array(
"id" => 1,
"last_updated" => 1399281955,
"weight" => 2.0
),
array(
"id" => 2,
"last_updated" => 1399281955 - 15000,
"weight" => 0
),
array(
"id" => 3,
"last_updated" => 1399281955 - 30000,
"weight" => 1.0
),
array(
"id" => 4,
"last_updated" => 1399281955 - 45000,
"weight" => 0
)
);
$results = array();
foreach ($users as $index => $user) {
$factor = 3;
$timestamp = $user['last_updated'];
$substract = $timestamp;
// Start substracting weight
$timediff = time() - $timestamp;
if ($timediff > 3600) {
// The longer the timediff, the heavier the weight becomes
$weight_severity = pow((($timediff/1000) * $user['weight']), $factor);
// Putting the 'last_updated' back farther in the past
$substract = $timestamp - $weight_severity;
}
$users[$index]['weight_updated'] = floor($substract);
$users[$index]['timediff'] = $timediff;
$users[$index]['diff'] = $users[$index]['last_updated'] -
$users[$index]['weight_updated'];
}
echo '<pre>';
print_r($users);
usort($users, function($a, $b) {
return (($a['weight_updated'] == $b['weight_updated'])) ?
0 : ($a['weight_updated'] < $b['weight_updated']) ? -1 : 1;
});
print_r($users);
So without weights, the user IDs would be: 4,3,2,1. But with my 'algorithm' it's now 3,4,2,1. User ID 3 because of it's weight, is getting done before 4. This is with a time difference of 15000 seconds (a little over 4 hours).

How to implement a performant search of dates intervals in an array of dates?

Having this array :
array (size=1)
24 =>
array (size=7)
'user_id' => int 24
'date_clicked' =>
array (size=3)
0 => int 1382867319
1 => int 1382867419
2 => int 1382940698
'ip' => string '127.0.0.1' (length=9)
'email' => string 'test' (length=8)
'name' => string 'test' (length=7)
'request' => string 'test content' (length=12)
'faked_clicks' =>
array (size=3)
0 => int 1382867319
1 => int 1382867419
2 => int 1382940698
Here is my implementation that adds the faked_clicks array based on the date clicked array :
foreach($parsedUserClicks as $k => $v) {
foreach($v['date_clicked'] as $kk => $vv) {
$rangeHigh = range($vv, $vv+(60*60*24));
$checkHigh = array_intersect($v['date_clicked'], $rangeHigh );
if(count($checkHigh) >= 3) {
$parsedUserClicks[$k]['faked_clicks'] = $checkHigh;
}
}
}
The thing is that , by using array_intersect , it's taking quite a long time make a search only for 3 timestamps .
What I want to achieve is to get all 3 dates that are in an interval of 1 day . But my search is too slow (5 seconds for this simple search) . Any algorith i could use for this type of search ?
P.S. : I know i should not use such a big range to intersect arrays (60*60*24) . But i can't seem to find another solution . Also the range might get bigger so this method eventually will drop .
how about simply checking the values?
$dc_copy = $v['date_clicked'];
foreach($parsedUserClicks as $k => $v) {
$checkHigh = array();
foreach($v['date_clicked'] as $kk => $vv) {
$rangeHigh = $vv+(60*60*24);
foreach($dc_copy as $v2){
if($v2 >= $vv && $v2 <= $rangeHigh){
$checkHigh[] = $v2;
}
}
if(count($checkHigh) >= 3) {
$parsedUserClicks[$k]['faked_clicks'] = $checkHigh;
}
}
}
The only solution i could think of right now was to minimize the search to be made on days not on seconds . This is not a final answere , maybe someone else can give a proper search algorith for this type of search .

PHP class array

Hey all i am new at classes and arrays in php but need to find out how to go about getting the correct value from this class function array
class theCart {
public static $DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
}
My output i am trying to match looks like this: 76 - 125
Do i just call it like
$distanceNum = '76 - 125';
$tmpDistanceTotal = $DISTANCE($distanceNum);
Should $tmpDistanceTotal then have a value of 10? I'm thinking that the array only has the values 0,10,20,30,40,50 in it?
I have another array:
public static $STEPS = array(
'0' => 0,
'1' => 0,
'2' => 0,
'3' => 25,
'4' => 50,
'5' => 75,
'6' => 100,
'7' => 125
);
My output i am trying to match with that above is 3 I'm not sure if its looking for a string or not?
This should clear the point:
foreach (theCart::$DISTANCE as $k => $v) {
if ($v == '76 - 125') {
echo $k;
break;
}
}
For $tmpDistanceTotal to get the value 10, you could do the following:
$tmpDistanceTotal = array_search($distanceNum, theCart::DISTANCE);
or you may wish to end up with something like this:
class theCart {
public static $DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
public function getTotalDistance($distanceNum)
{
return array_search($distanceNum, self::DISTANCE);
}
}
Your question is actually just about arrays, and you should remove the classes from here to make things easier to understand:
$DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
$variable = $DISTANCE[10];
In the above example Variable will be equal to 76-125. You're working with Associative Arrays, so you need to go read up on them a little bit as your questions shows you don't really understand how arrays work. Once you have that down, go ahead and move into a class context like you mentioned above.
You can check out the PHP Manual here: http://php.net/manual/en/language.types.array.php
For a short and quick answer you can use
$tempVar = 10;
$tmpDistance = $this->DISTANCE[$tempVar];
Not sure what you are trying to do, but you could use array_search:
$distanceNum = '76 - 125';
$key = array_search($distanceNum, theCart::$DISTANCE);
$key is now 10.

Zend_Date::sub() and ISO_8601 calculating hour difference wrong

I want to calculate time difference between two Zend_Date objects (for countdown calculator):
$now = new Zend_Date($now_datetime, Zend_Date::ISO_8601);
$end= new Zend_Date($end_datetime, Zend_Date::ISO_8601);
echo $now->getIso();
echo $end->getIso();
$expires=array();
$expires['expired']=false;
if($end->isEarlier($now)){
$expires['expired']=true;
return $expires;
}
$dif=$end->sub($now);
$expires['days']=($dif->getDay()->toValue()/(60*60*24));
$expires['hours']=($dif->getHour()->toValue()/(60*60));
$expires['minutes'] = $dif->getMinute()->toValue()/60;
$expires['seconds'] = $dif->getSecond()->toValue();
var_dump($expires);
For $now_datetime ='2012-06-30 01:01:01' and $end_datetime='2012-06-30 23:59:59', the result is
2012-06-30T01:01:01+02:00
2012-06-30T23:59:59+02:00
//array
'expired' => boolean false
'days' => int 0
'hours' => int 22
'minutes' => int 58
'seconds' => int 58
and it is OK.
But for For $now_datetime ='2012-06-30 00:01:01' and $end_datetime='2012-06-30 23:59:59', the result is
2012-06-30T00:01:01+02:00
2012-06-30T23:59:59+02:00
//array
'expired' => boolean false
'days' => int 1
'hours' => int -1
'minutes' => int 58
'seconds' => int 58
and it is NOT OK. I expect 'hours' to be 23, not -1 ?!
I am running MAMP with php 5.3, Zend_Framework 1.10.
What is wrong with that? ISO_8601 is used for MySQL 'datetime' data and I don't wanto to change to mktime()...
try to set
date_default_timezone_set('UTC');
in your index.php.. it will work fine..

Categories