News:

Wondering if this will always be free?  See why free is better.

Main Menu

Slow code, help plz ^.^

Started by Tyris, January 03, 2004, 09:50:19 PM

Previous topic - Next topic

Tyris

ok, this code is slow as ****** ^.^
   # remove ifs and ifnots
   
$oldcontent = "";
   while(
$oldcontent!=$tcontent)
   {
     
$oldcontent = $tcontent;
     
$tcontent = preg_replace(array('~(.*)<if exists:(.*?)>(.*?)</if>~eis','~(.*)<ifnot exists:(.*?)>(.*?)</ifnot>~eis'), array("parse_ifs('2','\$1',\$data[\$2],'\$3')","parse_ifs('3','\$1',\$data[\$2],'\$3')"), $tcontent);
     
$tcontent = str_replace("\\\"", "\"", $tcontent);
   }


basically it adds a full 0.1 second to my script (without this code it takes 0.02~ seconds)...
Basically its slow coz it has to pass about 20 news items thru there...
the preg_replace, replaces all my <if exists:somedata>blah</if> with nothing or blah depending on the if outcome...
how could I do something like this a lot faster... ie: the best way of writing an if system for lots of bits of data...

Thanx

Anguz

I don't understand what you want the while loop for, I'm not saying it's wrong, I don't get it
Cristián Lávaque http://cristianlavaque.com

Tyris

#2
thats all there to make it do embedded if statements...

basically it fins the last if statement on the page, analyses that, then it repeats with the second last and so on until there are none left (ie: $oldcontent == $tcontent)
... Without the while loop, the first (.*) would need to be (.*?)... by having it tho it allows embedded tags to work...
theres probably a better way to do this... but I use that same system somewhere else and it works quite fast and well (just doesnt get called with as much data and as many times as I need this one to do).


*edit*
in my other code part... of got a similar function that goes thru about 5 if statements...
this one however does the WHOLE loop for each of the 15 items of news it tries to display (basically I want a full news link to be displayed if the fullnews actually exists)

Anguz

let me see if I got this straight, you have nested if statements in your news articles like this?

if ... if ... if ... /if ... /if ... /if

or are they separate like this?

if ... /if ... if ... /if ... if ... /if
Cristián Lávaque http://cristianlavaque.com

Tyris

thats the the thing... I dont use if statements... its part of my CMS... I use special tags instead...
in this case:
<if exists:fullstory>link to full story here</if>

that preg replace is called every time a news item is loaded on the main page (so 15 times coz there are 15 'snippets' of news)... if that piece has a 'full story' section filled in in the database... I want a link displayed...

however, the while allows me to have multiple nested if statements...
ie:
<if exists:newssnippet>print snippet<br /><if exists:fullstory>print link to full story</if></if>
without the loop as far as I can tell taht would be impossible...

Tyris

also
heres some more info from when I asked a similar question before...

http://support.simplemachines.org/community/index.php?topic=2705
no one replied :(

just realised the location... support... huh?
cool :-\

Anguz

I see what your situation is... I just got back and am hungry and tired, so I don't think I can help much right now, but I'll look back into it tomorrow

I'm sure it can be done in some way that'll be faster... ttyl ;)
Cristián Lávaque http://cristianlavaque.com

writeto

The best way that I can think to handle this is using a recursive function. If I remember correctly (which I never do) if you call the function before returning a value it will create a stack. So what you do is if you come across another if statement that is correct you call the function again. Otherwise you continue to process until you hit the the end if statement.

That is how I would do it. I would imagine that would be faster then your current method since data will be handled in real time.

Tyris

aaa, thats sounds like a good idea... I'll see what I can do to recode it to do this :)
thanx :)

Shoeb Omar

Maybe I'm just tired - but recursive functions are hurrendously slow in comparison to normal loops... prettier but slower.

Tyris

so my while loop would be faster :-\ ??

its just the while loop has to go thru the same bit of code like EVERY time.., it doesnt get smaller... it just removes the if's... a recursive function would make the smaller smaller every time...

writeto

While a recursive function is generally slower due to the overhead of calling a function I believe it would still be faster. Of course there is no way to tell until this is done but either way I believe it will be equivalent not to mention much more slick.

Andrew

Tyris

well I'll check it out when I'm feeling less lazy :)

Tyris

ok... I've changed both of them....
the first instance increase it by a small ammount but caused anything using the above code (first post) to skyrocket to 20 seconds+ (no idea why)...
However once I modifed the above code as well it cut the time to .05seconds... still slower... but for about 30+ preg_replace calls... not too bad.. ;)

<?php
function ifsp1($content,$check)
{
  $content = preg_replace(array('~<if usergroup:(.*?)>(.*)~eis'),array("ifsp1('\$2','\$1')"),$content);
  $content = preg_replace('~(.*)</if>~eis',"ifsp2('0','\$1',$check)",$content);
  $content = str_replace("\\\"", "\"", $content);
  return $content;
}

function ifsp2($if,$stuff,$check)
{
  $pass = memcheck($check);
  if($if=='0')
    return $pass? $stuff: '';
  else
    return $pass? '': $stuff;
}
?>

Any ideas would still be appreciated... ideas to further increase speed...
what would be the best way if I didnt care about embedding...?
and what is a respectable page creation time for a CMS...?

writeto

Tyris:

Would you please run 30 preg_replace commands in code on your server and let us know the run time?

How many if statements are you embedding?

Obviously if you could make this one function you would greatly decrease the load time. I would imagine that alone would drop it about a quarter.

Andrew

Anguz

yeah, a recursive function more or less came to mind last night, but my consciousness was below the point were I could put it in words

could you post a copy of the text you want to pass through the function? with nested if's please, so we can do some testing if necessary, too :)
Cristián Lávaque http://cristianlavaque.com

Tyris

ok... I believe it runs 15 singular ones thru there (non embedded), and maybeabout 3 others...
takes about 0.05 seconds average on this page (sometimes peaks at 0.2), however my computer is pretty slow... and my space with Lewis Media tends to be quite a bit faster...

around 30 single non-embedded is taking just under 0.1 seconds...
And the same 30 with some embedding two levels deep at 2 points, and 3 levels deep at 1..
averages: about the same (actually it seems to average a little less... but only marginal maybe).

[Unknown]

Well, the problem is that using parsed preg_replace is very very slow.... very slow indeed.

-[Unknown]

Tyris

and a remedy for this would beeee....? (I use a lot of them... so if you could point me in the right direction I would be much obliged)...
I used to use str_ commands to locate the start and end of stuff before I knew about preg_replaces... basically a 10 line function that emulated preg_replace... :-\

Advertisement: