What other factors maybe causing my php script to timeout? - php

I have been creating a php script to analyse some data. At around 320 seconds the script just stops, no error messages.
I have created a separate simple php script to test and ensure its nothing in my code causing the problem. The simple php script does the same. I have set the max execution time to 25000 (again only for testing) and it still stops at 300 - 360 secs. What other factors maybe causing my script to timeout?
echo ini_get('max_execution_time');
for ($x = 0; $x <= 600; $x++) {
echo $x." - ";
sleep(1);
}
I would expect the script to continue running for 600 seconds, however its stops at 308.
OUTPUT:-
250000 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18 - 19 - 20 - 21 ....... 299 - 300 - 301 - 302 - 303 - 304 - 305 - 306 - 307 - 308 -

Try this, you will have it continue working:
set_time_limit(0);
for ($x = 0; $x <= 600; $x++) {
ob_flush();
flush();
echo $x." - ";
sleep(1);
}

Related

PHP - Why floor() give me another result of sams calculation [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
for($i=0;$i<=15120;$i+=504){
echo ($i/60/8.4) . " - " .floor($i/60/8.4)."<br>";
}
Result (i make ** at problem) :
0 - 0
1 - 1
2 - 2
3 - 3
4 - 4
5 - 5
6 - 6
7 - 6
8 - 8
9 - 8
10 - 10
11 - 11
12 - 12
13 - 13
14 - 13
15 - 15
16 - 16
17 - 17
18 - 17
etc...
At start i think i apply "Floo" at all calculation but (for line "7-6") also $i = 3528 :
3528 / 60 = 58,8 == Floor ==> 58 / 8 = 7.25
floor of 7.25 =/= 6
it because of PHPs floating point precision
so your 8th calculation for example will not return 7, but something like 6.9999999999999999999999999999
as this is a double, it will be rounded to 7 on output, try this out:
$x = 6.9999999999999999999999999999999999999999999999999999999;
echo $x;
when you use floor() it will (correctly) round it down to 6

Reset value before reach ceiling inside Loop

i have a number of minutes as ceiling and tasks that demand time in minutes, im doing a while loop to sum the time required by the tasks until reach the ceiling, but im having a problem with one step in the loop, the first number is the loop, the second the task required time, the third is my ceiling. I must reset the the task_required_time variable before or once it reach the ceiling. Using
if ($task_required_time > $ceiling) {task_required_time = 0}
it reset after its over the ceiling:
1 - 120 - 2400
2 - 304 - 2400
3 - 424 - 2400
4 - 2092 - 2400
5 - 2212 - 2400
6 - 3580 - 2400 <---- here is when it reset as expected but should be in the step before.
7 - 120 - 2400
8 - 330 - 2400
9 - 450 - 2400
10 - 570 - 2400
I will appreciate some light on it.
You need to make the compare at the begining of the code. this may help:
while ($rows = mysql_fetch_array($duedates)) {
if ($tiempo_para_mix + (($rows['tiempo_reproduccion'])*2)+($tiempo_tqc_final*60) > $tiempo_efectivo) {
$tiempo_para_mix = 0;
};
$tiempo_para_mix += (($rows['tiempo_reproduccion'])*2)+($tiempo_tqc_final*60);
echo $x . " - " . $tiempo_para_mix . " - " . $tiempo_efectivo . "</br>";
$x++;
}

How to determine next level using a formula?

Thank you for any help you can provide on this.
If we have:
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
For instance if:
$c_xp = 1200
Then:
$level = 4
Then I am asking if:
$level = 4
How much xp to get to level 5?
How can I get the amount of xp required to achieve the next level?
The formula to get the minimum XP required to reach a level $level is given by:
$minXP = 12.5 * (pow((2 * $level + 3), 2) - 41);
So you can set $level = 5 and $level = 4 and find the difference between them.
Analyse:
I ran a script to view the XP needed for each level:
$current_level = 0;
for($c_xp = 1;$c_xp < 7000;$c_xp++){
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1; //GET LEVEL
if($level > $current_level){
$current_level++;
echo "c_xp $c_xp level $level<br>";
}
}
The output is:
c_xp 1 level 1
c_xp 100 level 2
c_xp 500 level 3
c_xp 1000 level 4
c_xp 1600 level 5
c_xp 2300 level 6
c_xp 3100 level 7
c_xp 4000 level 8
c_xp 5000 level 9
c_xp 6100 level 10
We can see that level 1 is an exception, then the difference of XP from level 2 to level 3 is 400, from level 3 to level 4: 500 and level 4 to level 5: 600 and so on...
The pattern is now clear,
Level 1 -> 1 XP
Level 2 -> Start a 100 XP
Level 3 -> Increase by 400 (500 XP)
Level 4 -> Increase by (400 + 100) 1000XP
Level 5 to Level X -> Keep increasing by 100
The code:
With the above analyse, I could come up with a loop in a function:
$c_xp = 1200;
$level = floor((-1 + sqrt(1 + 4 * ($c_xp + 500) / 100 * 2)) / 2) - 1;
echo 'Next level: '.current_level_xp($level+1).'<br>';
echo 'XP needed for next Level: '. (current_level_xp($level+1) - $c_xp);
function current_level_xp($current_level){
if($current_level <= 1){
return(1);
}else{
$increase = 400;
$needed = 100;
for($i=0;$i<$current_level-2;$i++){
$needed += $increase;
$increase += 100;
}
return $needed;
}
}
Output:
Next level: 1600
XP needed for next Level: 400
Use a math / formular plotting tool of your choosing to get an idea of the results of the used equation (which presumably isn't yours):
Now, there's probably even a way to reverse your formula for translating y into x. But I'm unsure if there's a floor reverse.

Which iteration rules apply on crypt() using CRYPT_EXT_DES?

My testcase as follows:
echo crypt('string', '_....salt');//error
echo crypt('string', '_A...salt');//fast
echo crypt('string', '_AAAAsalt');//slow
Explanation as stated at http://www.php.net/manual/en/function.crypt.php:
CRYPT_EXT_DES - Extended DES-based hash. The "salt" is a 9-character
string consisting of an underscore followed by 4 bytes of iteration
count and 4 bytes of salt. These are encoded as printable characters,
6 bits per character, least significant character first. The values 0
to 63 are encoded as "./0-9A-Za-z". Using invalid characters in the
salt will cause crypt() to fail.
A dot is a printable character so why does it return an error? And which "order" applies on the used characters resulting "AAAA" more iterations than "A..."?
It says all in the quoted paragraph:
- least significant character first
- The values 0 to 63 are encoded as "./0-9A-Za-z"
So in your example "_....salt" would mean 0 rounds which obviously can't work.
and "_A...salt" is less than "_AAAAsalt" considering the least significant character comes first.
"_...Asalt" would also be more than "_A...salt"
This question is a bit old, however i found this when trying to wrap my head around how to create a hashing class for internal use here, and i came up with this little function which will base64 encode an integer with the appropriate characters/significance to be used as the 4 character 'iteration count'. Possible values are from 1 to 16,777,215
private function base64_int_encode($num){
$alphabet_raw = "./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$alphabet = str_split($alphabet_raw);
$arr = array();
$base = sizeof($alphabet);
while($num){
$rem = $num % $base;
$num = (int)($num / $base);
$arr[]=$alphabet[$rem];
}
$arr = array_reverse($arr);
$string = implode($arr);
return str_pad($string, 4, '.', STR_PAD_LEFT);
}
Hope it helps someone!
The code of Klathmon is nice but has some mistakes:
First - alphabet
It is:
./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
Should be:
./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Second - order of characters/digits
It generates for example: ...z
But it should generate: z...
The improved code:
function base64_int_encode($num) {
$alphabet_raw='./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$alphabet=str_split($alphabet_raw);
$arr=array();
$base=sizeof($alphabet);
while($num) {
$rem=$num % $base;
$num=(int)($num / $base);
$arr[]=$alphabet[$rem];
}
$string=implode($arr);
return str_pad($string, 4, '.', STR_PAD_RIGHT);
}
A number system used in Extended DES:
.... - 0 (Extended DES error)
/... - 1
0... - 2
1... - 3
2... - 4
3... - 5
4... - 6
5... - 7
6... - 8
7... - 9
8... - 10
z... - 63
./.. - 64
//.. - 65
0/.. - 66
1/.. - 67
Y/.. - 100
61.. - 200
g2.. - 300
E4.. - 400
o5.. - 500
M7.. - 600
w8.. - 700
UA.. - 800
2C.. - 900
cD.. - 1000
zz.. - 4095
../. - 4096
/./. - 4097
0./. - 4098
1./. - 4099
xzzz - 16 777 213
yzzz - 16 777 214
zzzz - 16 777 215
And in connection with salt:
_/...salt - 1
_0...salt - 2
_1...salt - 3
_2...salt - 4
_3...salt - 5
_4...salt - 6
_5...salt - 7
_6...salt - 8
_7...salt - 9
_8...salt - 10
_z...salt - 63
_./..salt - 64
_//..salt - 65
_0/..salt - 66
_1/..salt - 67
_Y/..salt - 100
_61..salt - 200
_g2..salt - 300
_E4..salt - 400
_o5..salt - 500
_M7..salt - 600
_w8..salt - 700
_UA..salt - 800
_2C..salt - 900
_cD..salt - 1000
_zz..salt - 4095
_../.salt - 4096
_/./.salt - 4097
_0./.salt - 4098
_1./.salt - 4099
_xzzzsalt - 16 777 213
_yzzzsalt - 16 777 214
_zzzzsalt - 16 777 215

Working with percentages in PHP [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm trying to create a simple split tester in PHP but I'm having some trouble getting the percentages all figured out.
So, I have a settings page where I want to put a percentage, something like 20, so that 20% of the traffic to my page will be redirected.
This will obviously need to work with rand, but I'm not exactly sure how.
This if statement returns 20% true:
if (rand(1,5) % 5 == 0)
if (rand(1, 100) <= 20){
// show page 1
} else {
// show page 2
}
Some testing and comparison:
Results for round 0
Method1 redirect rate: 0.200302%
Method2 redirect rate: 0.200318%
Results for round 1
Method1 redirect rate: 0.199277%
Method2 redirect rate: 0.199479%
Results for round 2
Method1 redirect rate: 0.200262%
Method2 redirect rate: 0.19995%
Results for round 3
Method1 redirect rate: 0.200254%
Method2 redirect rate: 0.200315%
Results for round 4
Method1 redirect rate: 0.199943%
Method2 redirect rate: 0.19977%
Results for round 5
Method1 redirect rate: 0.20006%
Method2 redirect rate: 0.20024%
Summary:
Method1 deviation: -9.7999999999931E-5
Method2 deviation: -7.1999999999905E-5
and code for that testing:
<?php
function method1(){
return (rand(1,5) % 5 == 0)?true:false;
}
function method2(){
return (rand(1,100) <= 20)?true:false;
}
$iterations = 1000000;
for ($j = 0; $j <= 5; $j++){
$m1 = 0;
$m2 = 0;
for ($i = 0; $i < $iterations; $i++){
if (method1()){
$m1++;
}
if (method2()){
$m2++;
}
}
$dx1 = $m1/$iterations;
$dx2 = $m2/$iterations;
$dev1 += 0.2 - $dx1;
$dev2 += 0.2 - $dx2;
echo "Results for round $j\n";
echo "Method1 redirect rate: " . $dx1 . "%\n";
echo "Method2 redirect rate: " . $dx2 . "%\n";
}
echo "Summary:\n";
echo "Method1 deviation: $dev1\n";
echo "Method2 deviation: $dev2\n";
Also < would take less cpu power than % :) First in timing is the % and then <
$ /usr/bin/time -l php -q test.php
5.25 real 5.19 user 0.01 sys
8224768 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
2090 page reclaims
0 page faults
0 swaps
17 block input operations
4 block output operations
0 messages sent
0 messages received
0 signals received
17 voluntary context switches
592 involuntary context switches
$ /usr/bin/time -l php -q test.php
4.75 real 4.73 user 0.01 sys
8216576 maximum resident set size
0 average shared memory size
0 average unshared data size
0 average unshared stack size
2088 page reclaims
0 page faults
0 swaps
0 block input operations
0 block output operations
0 messages sent
0 messages received
0 signals received
0 voluntary context switches
100 involuntary context switches

Categories