PHP - If no results, wait and try again - php

I'm calling a web service via GET protocol and the response will either be echoed out on the page as true or false.
I'm trying to write logic that will retry up to 3 times if the web service returns false. However, the first time I want to wait 1 second, the second time I want to wait 10 seconds, and the third time I want to wait 60 seconds.
This is what I currently have. Is there a better way to achieve this?
if ($wsReturn == 'false') {
sleep(1);
$wsReturn = strip_tags(file_get_contents($link));
if ($wsReturn == 'false') {
sleep(10);
$wsReturn = strip_tags(file_get_contents($link));
if ($wsReturn == 'false') {
sleep(60);
$wsReturn = strip_tags(file_get_contents($link));
}
}
}

Just use an array and a loop to make any number of iterations with any pause times
$i = 0;
$sleep = [1, 10, 60];
while( $wsReturn == 'false' )
{
sleep( $sleep[$i] );
// your logic goes here
$wsReturn = strip_tags(file_get_contents($link));
if( ++$i >= count($sleep) )
break;
}

This is what I ended up with after #Pavel Lint sparked the idea for me:
$i = 0;
$waitTimes = array();
$waitTimes = [1, 10, 30]
while ($wsReturn == 'false') {
sleep( $waitTimes[$i] );
$wsReturn = strip_tags(file_get_contents($link));
$++i
if ($i >= count($waitTimes)-1) { break; }
}

Related

Sleep function fires twice

I have 31 rows in my DB and I want to load them with 2 seconds interval. But when sleep function is live for 60 seconds, main function repeats and $count resets.
My code:
$count = 0;
$files_count = count($files);
foreach($files as $file) {
$count++;
$bot_file_name = $file['bot_file_name'];
$bot_file_tg_id = $file['bot_file_tg_id'];
$bot_file_format = $file['bot_file_format'];
if ($bot_file_format == "pdf") {
// do something
} else if ($bot_file_format == "video") {
// do something
} else if ($bot_file_format == "audio") {
// do something
}
if ($count == $files_count) {
break;
}
sleep(2);
}
I can't understand what is wrong here.
php max_execution_time is 120.

Why does this code cause high load average on the server?

The code below sends a msg when a value, obtained from a not so stable API, is within a certain range.
This code causes the load average, but not the CPU usage, to go up, likely due to high I/O wait.
CentOS 6.5
Kernel 2.6.32-431.11.2.el6.x86_64
Apache 2.2.15
PHP 5.3.3 (mod_fcgid/2.3.7)
> cat /sys/block/sda/queue/scheduler
noop anticipatory deadline [cfq]
The code had the same problematic effect on both dedicated hardware and on the cloud (both cases as a VM on KVM/Virtio).
What could be done to keep this code from *causing the processor to wait on instruction completion before processing new instructions**? I understand that lowering the timeout doesn't really solve the problem, only diminishes its impact.
*this is my understanding why this code causes load average to go up.
<?php
$min = '1';
$last_min = '1';
if (!empty($_GET['min'])) {
$min = $_GET['min'];
} else {
$min = false;
}
$ctx=stream_context_create(array('http'=>
array(
'timeout' => 10 // seconds timeout
)
));
$json = file_get_contents('https://www.domain.com/api/ticker/',false,$ctx);
if (!empty($json )) {
echo($json);
if (#file_get_contents('log.txt')) {
if (quote_changed($json)) {
file_put_contents('log.txt', $json, FILE_APPEND);
}
} else {
file_put_contents('log.txt', $json);
}
$obj = json_decode($json,true);
$last = $obj['ticker']['last'];
if (is_numeric($last)) {
$last = (int)$last;
$last_min = #file_get_contents('last_min.txt');
$notified = #file_get_contents('notified.txt');
if ($notified === false) {
$notified = 'false';
#echo "no notify file\n";
}
if (($last_min === false) || (($min) && ($last_min <> $min))) {
$last_min = 1;
$notified = 'false';
file_put_contents('last_min.txt', $min);
#echo "no min file or diff min\n";
}
#echo ('notified='.$notified.'\n');
if (($last >= $min) && ($notified=='false')) {
#$url = ('http://otherdomain.com/nexmo/sendmsg.php' . '?name=blah' . $last);
#file_get_contents($url);
#switch to SMS when going abroad and plugin new number when available
mail("8885551212#mail.net","Blah at".$last,"","From: gaia#domain.com\n");
file_put_contents('notified.txt', 'true');
#echo "msg sent\n";
} elseif (($last < $min) && ($notified=='true')) {
file_put_contents('notified.txt', 'false');
#echo "not sent\n";
}
}
}
function quote_changed($current) {
$previous = tailCustom('log.txt');
#echo ('previous='.$previous);
if ($previous === (trim($current))) {
return 0;
} else {
return 1;
}
}
function tailCustom($filepath, $lines = 1, $adaptive = true) {
// Open file
$f = #fopen($filepath, "rb");
if ($f === false) return false;
// Sets buffer size
if (!$adaptive) $buffer = 4096;
else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
// Jump to last character
fseek($f, -1, SEEK_END);
// Read it and adjust line number if necessary
// (Otherwise the result would be wrong if file doesn't end with a blank line)
if (fread($f, 1) != "\n") $lines -= 1;
// Start reading
$output = '';
$chunk = '';
// While we would like more
while (ftell($f) > 0 && $lines >= 0) {
// Figure out how far back we should jump
$seek = min(ftell($f), $buffer);
// Do the jump (backwards, relative to where we are)
fseek($f, -$seek, SEEK_CUR);
// Read a chunk and prepend it to our output
$output = ($chunk = fread($f, $seek)) . $output;
// Jump back to where we started reading
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
// Decrease our line counter
$lines -= substr_count($chunk, "\n");
}
// While we have too many lines
// (Because of buffer size we might have read too many)
while ($lines++ < 0) {
// Find first newline and remove all text before that
$output = substr($output, strpos($output, "\n") + 1);
}
// Close file and return
fclose($f);
return trim($output);
}
?>
Move the data into a database or if that's not an option, cache the unstable file into your server locally so you don't have to hit the external provider every time.
If neither of those are an option, it seems to me that the people providing the API need to improve their performance, you can verify this by just benchmarking how many milliseconds each hit takes through Firebug.

Improve performance of php on local server

I have a XAMPP install, with pretty much the default config.
Performance isn't much of a problem in general as I use PHP mostly to run web pages and small web apps. Waiting a couple seconds for a page is not unusual.
However, I have recently taken up the problems from Project Euler and decided to do them in PHP.
Try as I may, I couldn't get my code to run in less than 1 minute 1 second (optimized down from almost 3 min) and I was getting pretty embarrassed, especially considering most posters on Pjt Euler reported times of 1-3 seconds. (#7, find the 10001th prime)
I ported my code to C#, and the same task completed in a blink. 0.4 seconds. Same algorithm, the only notable difference in the code is that I used a List in C# to replace the array I was using in PHP.
While I did expect C# to outperform php, this difference leads me to suspect a gross configuration problem, but I have no idea where to look.
What could be the cause of this poor performance?
Edit: Here is the code:
In PHP:
/*
* Project Euler #7:
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
* What is the 10001st prime number?
*/
ini_set('max_execution_time', 300);
echo "start time:" . date("i:s:u") . "<br />";
function isPrime($number, $prevPrimes)
{
foreach ($prevPrimes as $key =>$prime)
{
if ($prime == 1)
{
continue;
}
elseif ($number % $prime == 0)
{
return 0;
}
}
// If we get to here, $number is prime
return $number;
}
$primes = array();
$i = 0;
$nbPrimes = 0;
while ($nbPrimes <10001)
{
$i++;
if ($i % 2 != 0)
{
$result = isPrime($i, $primes);
if ($result != 0)
{
$primes[] = $i;
$nbPrimes++;
}
}
}
echo "#$nbPrimes: $result<br>";
echo "End time:" . date("i:s:u") . "<br />";
In C#:
public static void RunSnippet()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
List<int> primes = new List<int>();
int i = 0;
int nbPrimes = 0;
int result =0;
while (nbPrimes <10001)
{
i++;
if (i % 2 != 0)
{
result = isPrime(i, primes);
if (result != 0)
{
primes.Add(i);
nbPrimes++;
}
}
}
stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}",
stopwatch.Elapsed);
Console.WriteLine ("#" + nbPrimes + ": " + result.ToString());
}
public static int isPrime(int number, List<int> prevPrimes)
{
foreach (int prime in prevPrimes)
{
if (prime == 1)
{
continue;
}
else if (number % prime == 0)
{
return 0;
}
}
// If we get to here, number is prime
return number;
}
"Use the force ..." of math! Just throwing some code pointless. Here are just a few points that can boost the performance.
why you are using array to match the number against?
the foreach function is thus ineffective - the cycle should end at floor(sqrt(number))
example: sqrt(64) = 8 -> all prime dividers will be from 1 to 8. The others will be product of them( 32 = 4 x 8 = 2x2x2x2x2 )
use formulas to jump to the next possibly prime number
math:
numbers divisable by 2 - 2, 4, 6, 8, 10, 12 -> 2k+1 = 2x1+1 = 3, 5, .....
numbers divisable by 3 - 3, 6, 9, 12 -> we already have 6 and 12, so 3, 9, 15, 21 -> 3(2k-1) = 3(2x1-1) = 3, 9, ...
here is some pseudo code from hk admin at project euler
isPrime ( number )
{
if ( number == 1 ) return false
elseif ( number < 4 ) return true
elseif ( number % 2 == 0 ) return false
elseif ( number < 9 ) return true
elseif ( number % 3 == 0 ) return false
else
r = floor ( sqrt ( number ) )
f = 5
while ( f <= r )
{
if ( number % f == 0 ) return false
if ( number % ( f + 2 ) == 0 ) return false
f = f + 6
}
return true
}
PS
About the difference in the speed of the execution - PHP is interpreted language, to view the result in browser you have 3 programs running - browser, server, php interpreter. You make a http request, the server calls php (and probably a bunch of other stuff, logging for example),php reads the script and executes it. There are much more steps than in C#.
In C# the compiled code is executed.
FINAL EDIT
Here is the PHP code from Bakudan's logic, which returns this result:
start time:44:25:000000
#10001: 104759
End time:44:26:000000
The Code:
<?php
echo "start time:" . date("i:s:u") . "\n";
function isPrime($number, &$primes)
{
if ($number === 1) return false;
elseif ($number %2 === 0) return false;
elseif ($number < 4) return true;
elseif ($number < 9) return true;
elseif ($number %3 === 0) return false;
else $r = floor(sqrt($number));
$f = 5;
while ($f <= $r) {
if ($number % $f ===0) return false;
if ($number % ($f+2) === 0) return false;
$f = $f + 6;
}
return true;
}
$primes = array();
$nbPrimes = $i = 0;
while ($nbPrimes < 10001)
{
$i++;
if (isPrime($i, $primes) !== false)
{
$primes[] = $i;
$nbPrimes++;
}
}
echo "#$nbPrimes: " . end($primes) . "\n";
echo "End time:" . date("i:s:u") . "\n";
Bakudan gave me the pseudo code, I Just translated and wrote it out for the OP's script above.
EDIT 2
I cleaned up the code a bit, didn't improve anything, may enhance "readability". But yea, I think this is the best you will get with PHP, which on an i7 without apache yields 5 seconds.
<?php
echo "start time:" . date("i:s:u") . "\n";
function isPrime($number, &$primes)
{
foreach($primes as $prime) {
if ($number % $prime === 0 && $prime > 1)
return false;
}
}
$primes = array();
$nbPrimes = $i = 1;
while ($nbPrimes <= 10001)
{
if ($i % 2 !== 0 && isPrime($i, $primes) !== false)
{
$primes[] = $i;
$nbPrimes++;
}
$i++;
}
echo "#$nbPrimes: " . end($primes) . "\n";
echo "End time:" . date("i:s:u") . "\n";
EDIT
Knocked another second off by moving the $prime === 1 to be after the $number % $prime check in the same if statement.
start time:29:40:000000
#10001: 104743
End time:29:45:000000
Taking Hannes suggestion of strict checking and passing the array as reference plus adding a few tweaks of my own (modifying the array inside the function):
ini_set('max_execution_time', 300);
echo "start time:" . date("i:s:u") . "\n";
function isPrime($number, &$prevPrimes)
{
foreach ($prevPrimes as $prime) {
if ($number % $prime === 0 && $prime !== 1)
{
return false;
}
}
// If we get to here, $number is prime
$prevPrimes[] = $number;
return $number;
}
$primes = array();
$i = 0;
$nbPrimes = 0;
while ($nbPrimes < 10001)
{
$i++;
if ($i % 2 !== 0)
{
$result = isPrime($i, $primes);
if ($result !== 0)
{
$nbPrimes++;
}
}
}
echo "#$nbPrimes: $result\n";
echo "End time:" . date("i:s:u") . "\n";
Which ended up being:
start time:52:08:000000
#10001: 104743
End time:52:15:000000
VS your code:
start time:50:44:000000
#10001: 104743
End time:51:17:000000
A good improvement there, but nothing like C#, just goes to show the power of a compiled language :)
While I did expect C# to outperform
php, this difference leads me to
suspect a gross configuration problem,
but I have no idea where to look.
Firing the PHP engine creates a little overhead for the webserver. The way PHP is loaded (e.g. loaded as a module on server startup or loaded on demand for every .php request) determines how much overhead is involved. Then on windows there are two variants of PHP available: thread-safe and non thread-safe, the latter one is claimed to be faster.
If its a XAMPP configuration problem, I think you can isolate it by running the test 3 times on your webserver and note down the average time. Then run the same script via PHP CLI 3 times and note down the average. If the difference is noticeable then you might blame XAMPP. You should be able to locate the PHP CLI binary somewhere inside the XAMPP installation folder.
On my system I get these results:
PHP-CLI: #10001: 104743 -- Time taken: 30.25 second(s)
PHP on IIS/FastCGI: #10001: 104743 -- Time taken: 29.89 second(s)
PHP on Apache/CGI: #10001: 104743 -- Time taken: 29.93 second(s)
Not much of a difference -- I would rather optimize the code.
EDIT
Same machine and everything but execution time brought down from ~30 seconds to ~5.85 seconds with this revised code. The only thing worth mentioning is that that I used a global array instead of passing it by value every time the isPrime function is called (104743 times to be precise). Passing the array by reference also results in similar execution time, give or take 1 second. The comparison operators shave off just a second or two but not much.
/*
* Project Euler #7:
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
* What is the 10001st prime number?
*/
ini_set('max_execution_time', 300);
$t0 = microtime(true);
$primes = array();
function isPrime($number)
{
global $primes;
foreach ($primes as $prime)
{
if ($prime === 1)
{
continue;
}
elseif ($number % $prime === 0)
{
return 0;
}
}
return $number;
}
$i = 0;
$nbPrimes = 0;
while ($nbPrimes < 10001)
{
$i++;
if ($i % 2 !== 0)
{
$result = isPrime($i);
if ($result !== 0)
{
$primes[] = $i;
$nbPrimes++;
}
}
}
$t1 = microtime(true);
echo sprintf('#%d: %d -- Time taken: %.2f second(s)', $nbPrimes, $result, $t1 - $t0);

PHP - Project Euler #2

I got the answer fine, but when I run the following code,
$total = 0;
$x = 0;
for ($i = 1;; $i++)
{
$x = fib($i);
if ($x >= 4000000)
break;
else if ($x % 2 == 0)
$total += $x;
print("fib($i) = ");
print($x);
print(", total = $total");
}
function fib($n)
{
if ($n == 0)
return 0;
else if ($n == 1)
return 1;
else
return fib($n-1) + fib($n-2);
}
I get the warning that I have exceeded the maximum execution time of 30 seconds. Could you give me some pointers on how to improve this algorithm, or pointers on the code itself? The problem is presented here, by the way.
Let's say $i equal to 13. Then $x = fib(13)
Now in the next iteration, $i is equal to 14, and $x = fib(14)
Now, in the next iteration, $i = 15, so we must calculate $x. And $x must be equal to fib(15). Now, wat would be the cheapest way to calculate $x?
(I'm trying not to give the answer away, since that would ruin the puzzle)
Try this, add caching in fib
<?
$total = 0;
$x = 0;
for ($i = 1;; $i++) {
$x = fib($i);
if ($x >= 4000000) break;
else if ($x % 2 == 0) $total += $x;
print("fib($i) = ");
print($x);
print(", total = $total\n");
}
function fib($n) {
static $cache = array();
if (isset($cache[$n])) return $cache[$n];
if ($n == 0) return 0;
else if ($n == 1) return 1;
else {
$ret = fib($n-1) + fib($n-2);
$cache[$n] = $ret;
return $ret;
}
}
Time:
real 0m0.049s
user 0m0.027s
sys 0m0.013s
You'd be better served storing the running total and printing it at the end of your algorithm.
You could also streamline your fib($n) function like this:
function fib($n)
{
if($n>1)
return fib($n-1) + fib($n-2);
else
return 0;
}
That would reduce the number of conditions you'd need to go through considerably.
** Edited now that I re-read the question **
If you really want to print as you go, use the output buffer. at the start use:
ob_start();
and after all execution, use
ob_flush();
flush();
also you can increase your timeout with
set_time_limit(300); //the value is seconds... so this is 5 minutes.

PHP creating a while loop that is dependent on multiple variables that are independent of one another?

I'm stumped on this and my searches aren't turning up anything relevant.. I need to do a while loop that will continue if either of 2 variables are true... as far as I can tell you can't do a "while ($var = '' and $var2 = ''); so I tried this, basically I figured I could just set 2 different if statements so that it would change the variable "continue" if it went past 4 iterations (if $i >= 4), however this just gives an infinite loop:
function whiletest () {
$i = 1;
do {
echo 'output';
if ($status != 'true') {
$continue = 1 ;
}
if ($i >= 4) {
$continue = 2 ;
}
$i++ ;
} while ($continue = 1 );
}
Are you looking for a construct like this:
while($var1 == 'value1' OR $var2 == 'value2') {
...
}
That will continue to run while either condition is true.
Why wouldn't the following work?
while (($condition1) || ($condition2)) {
// loop stuff
}
As long as the expression within the while parens is true, the loop will execute.
The while statement evaluates a boolean expression. You should be able to write out:
while( ($status != true) && ($continue == 1) ) {}
Also in your code (if its a c/p), you have $continue = 1. This will always evaluate to true.
EDIT:
while (($status) && ($i < 4))
As for the last while, it just looks like an infinite loop to me.
You shouldn't need the $continue variable. This should do the trick:
$i = 1;
do {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
} while ($status != 'true' && $i < 4);
Keep in mind that this will always run the loop at least once. If $status might start out as 'true' and you want the loop to run zero times if it is, you want:
$i = 1;
while ($status != 'true' && $i < 4) {
//do other stuff here (possibly changing the value of $status)
echo 'output';
$i++;
}

Categories