Fatal error! [message #180876] |
Sun, 24 March 2013 10:43 |
gb
Messages: 1 Registered: March 2013
Karma: 0
|
Junior Member |
|
|
Hi,
I'm a newbye of PHP, why it gives me this error:
Fatal error: Maximum execution time of 60 seconds exceeded in
C:\xampp\htdocs\ord.php on line 30
Is there a time limits?
This is the script that run locally on xampp:
<?php
$nmin_rand=1;
$nmax_rand=100000;
$l_arr=100000;
$n=0;
$num_estr=array();
$num_copia=array();
//inizializza array lungo $l_arr
echo "<br>";
for ($i=0; $i<$l_arr; $i++){
$num_estr[$i]=rand($nmin_rand,$nmax_rand);
echo " ".$num_estr[$i]." ";
}
// Ordinamento
$flag=true;
do{
$flag=false;
for ($i=0; $i<$l_arr-1; $i++)
if ($num_estr[$i]>$num_estr[$i+1]){
$temp=$num_estr[$i];
$num_estr[$i]=$num_estr[$i+1];
$num_estr[$i+1]=$temp;
$flag=true;
$n++;
}
}while($flag);
//stampa array ordinato lungo $l_arr
echo "<br>";
for ($i=0; $i<$l_arr; $i++){
echo " ".$num_estr[$i]." ";
}
echo "<br>Numero scambi : ".$n;
?>
|
|
|
Re: Fatal error! [message #180877 is a reply to message #180876] |
Sun, 24 March 2013 12:04 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/24/2013 6:43 AM, gb wrote:
>
> Hi,
> I'm a newbye of PHP, why it gives me this error:
>
> Fatal error: Maximum execution time of 60 seconds exceeded in
> C:\xampp\htdocs\ord.php on line 30
>
> Is there a time limits?
>
> This is the script that run locally on xampp:
>
> <?php
>
> $nmin_rand=1;
> $nmax_rand=100000;
> $l_arr=100000;
> $n=0;
>
> $num_estr=array();
> $num_copia=array();
>
> //inizializza array lungo $l_arr
>
> echo "<br>";
> for ($i=0; $i<$l_arr; $i++){
>
> $num_estr[$i]=rand($nmin_rand,$nmax_rand);
>
> echo " ".$num_estr[$i]." ";
>
> }
>
>
> // Ordinamento
>
> $flag=true;
>
> do{
> $flag=false;
>
> for ($i=0; $i<$l_arr-1; $i++)
> if ($num_estr[$i]>$num_estr[$i+1]){
>
> $temp=$num_estr[$i];
> $num_estr[$i]=$num_estr[$i+1];
> $num_estr[$i+1]=$temp;
>
> $flag=true;
>
> $n++;
>
> }
>
> }while($flag);
>
> //stampa array ordinato lungo $l_arr
>
> echo "<br>";
> for ($i=0; $i<$l_arr; $i++){
>
> echo " ".$num_estr[$i]." ";
>
> }
>
> echo "<br>Numero scambi : ".$n;
>
> ?>
>
http://www.lmgtfy.com/?q=%22Fatal+error%3A+Maximum+execution+time%22+php
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Fatal error! [message #180878 is a reply to message #180876] |
Sun, 24 March 2013 13:00 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
gb schrieb:
> Fatal error: Maximum execution time of 60 seconds exceeded in
> C:\xampp\htdocs\ord.php on line 30
> Is there a time limits?
Yes there is. But you can change that:
http://www.php.net/set-time-limit
> This is the script that run locally on xampp:
[...]
So you are basically creating an array of 100000 elements, each being a
random number in the range 1 to 100000. That means, statistically, each
number in the given range would appear once. There's a simpler way to do
that:
$data = range( 1, 100000 );
shuffle( $data );
echo implode( ', ', $data );
Then you want to sort the array. I assume this is some kind of exercise
and you do this for learning purposes. You have chosen a sorting
algorithm which is quite time-consuming, i.e. not very efficient, so
it's no wonder that your script times out, especially when you use such
a large array.
You can improve the algorithm a bit: After the first run of the for-loop
the largest number will have moved to the end of the array, in other
words, it will already be where it's supposed to be. Therefore, in the
next round, you don't need to go all the way to the end of the array:
you can skip the last entry. Likewise, after the second run of the
for-loop, the second largest number will also be at its final place and
consequently you can skip the last two elements in the third run. And so
on. This way, you don't have to pass through the whole array each time
and your script will run a bit faster. You can achieve this by replacing
the do-while-loop with another for-loop and you also can get rid of the
$flag.
But there are many other sorting algorithms -- and much more efficient
ones. It will be instructive to learn about them and try to implement
them. And then you can compare them to see which is the fastest.
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|
Re: Fatal error! [message #180879 is a reply to message #180878] |
Sun, 24 March 2013 13:05 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 3/24/2013 9:00 AM, Thomas Mlynarczyk wrote:
> gb schrieb:
>
>> Fatal error: Maximum execution time of 60 seconds exceeded in
>> C:\xampp\htdocs\ord.php on line 30
>> Is there a time limits?
>
> Yes there is. But you can change that:
> http://www.php.net/set-time-limit
>
>> This is the script that run locally on xampp:
> [...]
>
> So you are basically creating an array of 100000 elements, each being a
> random number in the range 1 to 100000. That means, statistically, each
> number in the given range would appear once. There's a simpler way to do
> that:
>
> $data = range( 1, 100000 );
> shuffle( $data );
> echo implode( ', ', $data );
>
This would note create an array of random numbers. It will create a
randomized array of numbers - two entirely different things.
In a random array, duplicates are expected to happen. That's the nature
of random numbers.
> Then you want to sort the array. I assume this is some kind of exercise
> and you do this for learning purposes. You have chosen a sorting
> algorithm which is quite time-consuming, i.e. not very efficient, so
> it's no wonder that your script times out, especially when you use such
> a large array.
>
True, quick sort is quite to write - but not so quick in execution.
> You can improve the algorithm a bit: After the first run of the for-loop
> the largest number will have moved to the end of the array, in other
> words, it will already be where it's supposed to be. Therefore, in the
> next round, you don't need to go all the way to the end of the array:
> you can skip the last entry. Likewise, after the second run of the
> for-loop, the second largest number will also be at its final place and
> consequently you can skip the last two elements in the third run. And so
> on. This way, you don't have to pass through the whole array each time
> and your script will run a bit faster. You can achieve this by replacing
> the do-while-loop with another for-loop and you also can get rid of the
> $flag.
>
A minor improvement, but anything helps.
> But there are many other sorting algorithms -- and much more efficient
> ones. It will be instructive to learn about them and try to implement
> them. And then you can compare them to see which is the fastest.
>
Agreed. And learning about different sorting algorithms is a great
learning experience in itself.
> Greetings,
> Thomas
>
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|
Re: Fatal error! [message #180880 is a reply to message #180879] |
Sun, 24 March 2013 13:34 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
Jerry Stuckle schrieb:
>> $data = range( 1, 100000 );
>> shuffle( $data );
> This would note create an array of random numbers. It will create a
> randomized array of numbers - two entirely different things.
> In a random array, duplicates are expected to happen. That's the nature
> of random numbers.
This is, of course, true, but I still consider my suggestion to be
sufficient for the OP's sorting exercise [1]. On the other hand: a few
duplicates might be useful to prove that the sorting algorithm can deal
with them.
Greetings,
Thomas
[1] The rand() function is -- like any deterministic algorithm -- not
truly random either.
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|