Forum Moderators: coopster

Message Too Old, No Replies

str_replace search efficiency help

         

cfinlay

6:55 pm on May 25, 2004 (gmt 0)

10+ Year Member



Hi everyone,

I'm currently working on a theming setup that uses strings as placeholders for specific information.

The format of these strings are [v:varname], and they are replaced by a series of foreach loops as it loops through the content of the page to be displayed.

The page itself is built off of a general template in the format:

[v:header]
[v:content]
[v:footer]

header and footer are defined in the theme setup, and content is assigned by the php before the replacement is done. After this initial replacement, another foreach loop run must be done in order to ensure that all the variables have been replaced where they need to be.

This whole setup has proven to be very inefficient and very cpu intensive. I need a better way to do this, can anyone help?

Thanks,
C

JasonHamilton

7:07 pm on May 25, 2004 (gmt 0)

10+ Year Member



I guess it might be intensive depending on how your server is setup (how much ram, how fast your cpus), and how many variables you're scanning for. I do much the same for a template system I wrote, but I generally only have about 5-10 variables per page.

I don't have any cpu issues, but:

1) I run a php caching program (zend opt, phpa, etc)

2) Once a page is generated, I store the output for 5-10 minutes. If the page is loaded again during that time, it's issued from cache instead of being created from scratch.

3) My server has 2 gigs of ram and dual xeon cpus, so it deals pretty well with the 160k/day page views. Just one site on the server.

cfinlay

7:42 pm on May 25, 2004 (gmt 0)

10+ Year Member



That was my intent to fix things earlier, and is kind of the method I employed to keep from having to load the theme info from the database upon every page load.

Unfortunately though, I can't figure out a good way to cache the page because of the way cookies influence the layout.

I'm in the process of reducing the number of foreach() loops, I'm hoping that, in combination with some other things, will speed things along..

Netizen

2:01 pm on May 26, 2004 (gmt 0)

10+ Year Member



Have you thought about using some of the standard PHP templating systems? There are a few registered with PEAR - IT, Smarty, and there is PHPLIB. Also take a look at the PEAR Cache class as this can be used with cookies, if you include the cookie values in the id generator.