MVC, file_get_contents, and speed
Moderator: General Moderators
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
MVC, file_get_contents, and speed
So I'm extremely proud of myself as of right now, as I feel I have finally programmed a website in an actual MVC format. When I put PHP into HTML, I didn't like it. Then, I tried putting HTML into PHP, but it was still too cluttered. Now, I've separated form from function completely (aside from a form handling class that I made which handles it's own display) and it feels a LOT neater and much more organized and object-oriented.
The only problem is that there's an extra overhead of loading my templates. I use file_get_contents() and offline, the speed decrease is not noticeable, but online it most certainly is. I'm curious if anyone is aware of some methods I could implement to decrease the overhead. I've considered cache control, but on pages that require no-cache, I'd still need to call file_get_contents (and they tend to be the most content-heavy pages).
The only problem is that there's an extra overhead of loading my templates. I use file_get_contents() and offline, the speed decrease is not noticeable, but online it most certainly is. I'm curious if anyone is aware of some methods I could implement to decrease the overhead. I've considered cache control, but on pages that require no-cache, I'd still need to call file_get_contents (and they tend to be the most content-heavy pages).
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Well I have a lot of tricks up my sleeve and really approached it from a different angle than most programmers would. I would guess that most of the difference in rendering speed that your seeing is not related to the speed of the PHP code, but rather the latency of the connection. You could always test that by throwing a chronometer on the pages to verify that.
If I'm wrong, and that isn't the issue, or your just trying to squeeze every ounce of speed you can out of the code, I think the next step would be start caching pages. You can't really increase the speed of file_get_contents.
There are also other things to look at. Are you on shared hosting on a server under high load? What type of internet connection do you have and what is the ping time to your server? Is your remote server compressing the pages, adding to the time it takes to render in your browser? Are you running preg_replace a lot and doing a lot of string processing? Is the framework? Is it including tons of files you don't need? Loading tons of classes? Is the page using tables? etc.. etc..
There's a bunch of stuff that could cause that. Nevertheless I think that most frameworks are similar to aolware in that they are bloated. I prefer not to use them.
If I'm wrong, and that isn't the issue, or your just trying to squeeze every ounce of speed you can out of the code, I think the next step would be start caching pages. You can't really increase the speed of file_get_contents.
There are also other things to look at. Are you on shared hosting on a server under high load? What type of internet connection do you have and what is the ping time to your server? Is your remote server compressing the pages, adding to the time it takes to render in your browser? Are you running preg_replace a lot and doing a lot of string processing? Is the framework? Is it including tons of files you don't need? Loading tons of classes? Is the page using tables? etc.. etc..
There's a bunch of stuff that could cause that. Nevertheless I think that most frameworks are similar to aolware in that they are bloated. I prefer not to use them.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Right, I *am* on a 56k connection, and the page doesn't load slowly, but there is a pause between clicks and anything new being displayed to the browser. I was quick to blame that on file_get_contents(), but then I started thinking about it and all of my operations are performed before anything is displayed to the screen, And then after all of the processing, it is echoed. So, on second thought, I guess there is no real problem.astions wrote:There are also other things to look at. Are you on shared hosting on a server under high load? What type of internet connection do you have and what is the ping time to your server? Is your remote server compressing the pages, adding to the time it takes to render in your browser? Are you running preg_replace a lot and doing a lot of string processing? Is the framework? Is it including tons of files you don't need? Loading tons of classes? Is the page using tables? etc.. etc..
Yeah. Basically, on localhost, it's instant, but on the server, there's about 5 seconds from one page load to another (after images are cached) on my 56k. I'm so used to DevNetwork loading in like 2 seconds, 5 seconds seemed like a long time, but after browsing more websites, it's actually very fast. I guess I was just begging for some sort of deficiency for me to fix.arborint wrote:I don't see how loading two files rather than one has caused such a speed decrease. And I really don't understand "offline, the speed decrease is not noticeable, but online it most certainly is"? What does "offline" and "online" mean? I am guessing localhost vs a server somewhere?
Reply
Hi guys,
I have a similar framework and I think that it works pretty fine. I use a standart way of obtaining values. I have a class which parses html and .html template files. What I do is when a request to a page is sent my class checks the passed values and get the source of the page in which there are dynamic vars which are replaced with their values from a database. If your site is slow then use PEAR package and check every page's loading time. Then you can create a script which simulates a real situation. You should define a number of users(for example seventy or any number you want) and test your script on your localhost. You should do that with a smaller number than the number you expect to have on your server because I suppose that your own computer is not a server. If your server behaves good during the test then you have no problems with your Php and can keep using that class else you should fix it.
BTW:It is a tendency for many people not to pay the needed attention on some very small bugs in their code and that's why there are so many slow scripts on the Internet.
For example a bad programmer would write:
While the good one would write:
I know that this is pretty basic code but it is a common way of creating a loop.
I have a similar framework and I think that it works pretty fine. I use a standart way of obtaining values. I have a class which parses html and .html template files. What I do is when a request to a page is sent my class checks the passed values and get the source of the page in which there are dynamic vars which are replaced with their values from a database. If your site is slow then use PEAR package and check every page's loading time. Then you can create a script which simulates a real situation. You should define a number of users(for example seventy or any number you want) and test your script on your localhost. You should do that with a smaller number than the number you expect to have on your server because I suppose that your own computer is not a server. If your server behaves good during the test then you have no problems with your Php and can keep using that class else you should fix it.
BTW:It is a tendency for many people not to pay the needed attention on some very small bugs in their code and that's why there are so many slow scripts on the Internet.
For example a bad programmer would write:
Code: Select all
for($i =0 ; $i< count($users_arr); $i++) {
}Code: Select all
$num = count($users_arr);
for($i = 0; $i < $num; $i++) {
}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Reply
That is very good advice. As I was writing this website, it started out sloppy and became neater as time went by, since I don't really plan my structure ahead of time... I just make it. Now that I have the majority of the classes produced, I've been re-factoring the entire website and have noticed a few deficiencies here and there. Most of these were MySQL-related, where I'd either break something into two queries where one would suffice, or I'd do too much in one query where two would be faster.
I hadn't really considered clocking everything (other than my search queries)... That could prove very useful.
I hadn't really considered clocking everything (other than my search queries)... That could prove very useful.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US