FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » 500. Turbo Sort
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: 500. Turbo Sort [message #172640 is a reply to message #172607] Wed, 23 February 2011 01:15 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Kevin Wells wrote:

> n00m <n00m(at)narod(dot)ru> wrote:
>> http://www.spoj.pl/problems/TSORT/
>> My php code gets TimeLimitExceeded. Can it be accelerated somehow?
>> Just curious. No problem here with C, C++, Pascal etc.
>>
>> <?php
>>
>> $a = array_fill(0, 1000001, 0);
>>
>> fscanf(STDIN, "%d\n", &$n);
>>
>> for ($i = 0; $i < $n; ++$i) {
>> fscanf(STDIN, "%d\n", &$v);
>> ++$a[$v];
>> }
>>
>> //sort($a, SORT_NUMERIC);
>>
>> for ($i = 0; $i < 1000001; ++$i) {
>> for ($j = 0; $j < $a[$i]; ++$j) {
>> echo $i."\n";
>> }
>> }
>>
>> ?>
>
> Shouldn't it be $i++ not ++$i on the for loop?

Yes, it should not. First of all, the two expressions are semantically
equivalent *there*: the variable value is increased by one *after* one
cycle. Second, the algorithm for $i++ has to back up the original value of
$i since it is that which this expression evaluates to; ++$i does not have
to back up anything, saving one step. But since the variable value is not
immediately used here, optimization by the compiler might smooth out the
difference. Personally, I have used post-increment before on such occasions
but since a while I am using pre-increment just in case compilers are not as
advanced as I wish them to be.

On a funny side note, Bjarne Stroustrup said in an interview that there was
general agreement that his C++ (formerly, "C with Classes") should have been
named ++C, but he thought "that would [have created] too many problems for
non-geeks"¹ ;-)


PointedEars
___________
¹ <http://www.computerworld.com.au/article/250514/
a-z_programming_languages_c_/>
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to read/to download attachment on an URI?
Next Topic: Proxy to open blocked sites
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 03:24:47 GMT 2024

Total time taken to generate the page: 0.04309 seconds