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:
|
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)
|
|
|