PHP Parser, worthwhile?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

PHP Parser, worthwhile?

Post by mikusan »

Hi, i would like to ask, would it be worthwile for me to make a quick PHP code parser, how much will i gain? Here is my idea, the core of it really.

I have an encode and decode program, that takes a php file, strips off all comments and all spaces and formats it into a long block of code with no spaces no comments, very hard to read indeed. (but shouldn't it be faster?)

This process encode, will save a text file with the encoding information, that is it will contain the comments and the spaces previously stripped off, in case i want to take my code and edit it. I then use the decode and it will do that for me.

This is purely an idea, my aim is to hopefully to cut some of my overhead. Should my time be better spent trying to figure out how to use caching instead?

Thanks a bunch.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Re: PHP Parser, worthwhile?

Post by BDKR »

mikusan wrote: This is purely an idea, my aim is to hopefully to cut some of my overhead. Should my time be better spent trying to figure out how to use caching instead?

Thanks a bunch.
Yes, you're time would be better using caching. As a matter of fact, caching AND an accellerator. Something like APC, or Turk McCache comes to mind.

Now if you're like me and you use a lot of comments and you just can't get over being worried about the drain this has on performance, just put copies of your code without comments into production. Just keep a "commented" copy of your site for development and stuff.

Cheers,
BDKR
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Where do i start? :)
Any suggestions where can i find info on caching?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

I'd be surprised if there would be any noticable difference.

Try some timing tests: one file a couple of hundred lines long, and one with a couple of thousand additional lines of comments.

Computers are pretty quick these days ;)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

mikusan wrote:Where do i start? :)
Any suggestions where can i find info on caching?
Google, AllTheWeb, Feedster. There are also sites like PHPBuilder.com. It has tons of tutorials on PHP and doing various things.

Also, what exactly are you doing? Caching isn't what I would do first. The first thing I would do is install an accellerator. I allready mentioned a couple.

However, worrying about that kind of thing is normally something that comes a little later in the development process if you know what I mean.

Just to say something to McGruff: I wouldn't have thought the comments made much of a difference either until Selkirk over at sitepoint did some benching and found that there is a penalty associated with parsing out comments as well as anything else. That's why an accellerator makes so much sense. The parsing phase of running a script is taken care and all that needs to be done is actual execution.

Cheers,
BDKR
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

mikusan wrote:Where do i start? :)
Any suggestions where can i find info on caching?
Caching makes a lot of sense for site areas which aren't constantly updating - not something you'd use for a discussion forum however.

Use output buffering to grab a string which you can write to file. For example, if you normally finish off a script by including an html template where a bunch of page vars get echo'd out:

Code: Select all

<?php

ob_start();
include('html/template.htm');
$html = ob_get_contents();
ob_end_clean();

?>
When to cache? Some scripts I've seen check at each page request to see if a newer version of a page is available, refreshing the file if so. Better to cache as part of the site update routine and so speed up the routine page views.
Last edited by McGruff on Wed Aug 10, 2005 12:16 pm, edited 1 time in total.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

All of my scripts are designed to concatenate the output and echo it only at the very end, i also use my own custom made parser class. Though i found that reloading the same page was kind of pointless, i wanted to start tackiling the problem by making what i described above, but then realized that perhpas caching was the best thing to do. Though i don't know where to start, but basically, say, when you go to my website for the first time, all the images will take away time at loading, but that should not happen at any subsequent loads. So if i can cache the images and that sorta stuff into the client's computer i think i can more than compensate for SECONDS of time!!

Correct me if i am wrong, and how exactly would i go ahead and start on that.

Thanks
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Browsers will normally cache images automatically.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

BDKR wrote: Just to say something to McGruff: I wouldn't have thought the comments made much of a difference either until Selkirk over at sitepoint did some benching and found that there is a penalty associated with parsing out comments as well as anything else. That's why an accellerator makes so much sense. The parsing phase of running a script is taken care and all that needs to be done is actual execution.

Cheers,
BDKR
Interesting.
Post Reply