3

I am using many include to show small sections of my site. Is it fine to use many include or I should just reduce them (as much possible). How much more time does a include function cost?

My home page loads very slowly. What is the way to make it load faster. (My homepage shows almost same content on home page for an hour daily (and it shows some different data in some sections only). Can I cache it..what is the best solution available for caching or some other way with which i can make things faster.)

kb0000
  • 444
  • 3
  • 6
  • 18
  • Number of includes is not a problem. Problem is with your source code maybe your querys are slow, print them on the screen and cope to PMA to check how they work. – kskaradzinski May 30 '11 at 06:25
  • 1
    u can use [xdebug](http://xdebug.org/) to find what is slowing your code – Ibu May 30 '11 at 06:30

3 Answers3

1

If the content is updated on an hourly basis, why don't you create a static html (cam easily be done by php) upon an hourly basis, so that, only that static html is read and loaded to users instead of being generated upon web requests.


EDIT:

You create a php script that will generate a file like index.html and fill it with html code. Then you execute that php script every hour. This can be achieved by using CRON jobs. If you want more information on either of those then please ask another question specified on that subject.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
Gary Tsui
  • 1,755
  • 14
  • 18
  • Probably because "*it shows some different data in some sections*". – binaryLV May 30 '11 at 06:32
  • If it shows some different data in some sections, and in the big pictures, everything's collected and updated on an hourly basis, it is advised that you generate a static page by the hour. It saves a lot of overhead according to my experience. – Gary Tsui May 30 '11 at 06:34
  • I think he has 2 types of contents - one is that changes on hourly basis, another is fully dynamic (instead of second type being fully static). – binaryLV May 30 '11 at 06:55
  • Then, you can statically generate parts that are not as dynamic and attach only the dynamically generated. Fusion works the same in this case. Your final file would have parts in .php (i assume ur dynamically generated pages are done in php) and parts that are in html (but included by php). This way, you are saving overheads from those queries from the hourly pool. – Gary Tsui May 30 '11 at 07:22
  • @SynerCoder, Thanks for edits. Couldn't have put that together better than that myself. – Gary Tsui May 30 '11 at 07:23
  • Thanks for all the replies. On my homepage I have few sections which changes (minor changes) daily only after a cron job runs once a day. Some sections shows random data picked on every load. I would like to implement cache for those sections which are changed only once a day. Like Gary Tsui mentioned: final file would have parts in .php and .html how I can do this? – kb0000 May 30 '11 at 07:43
  • @Kunal, top of my head, 1) it is the method that you have been doing, you include .html file in the sections you want 2) this is something uncommon, but i'd done it before. You use another cronjob to write to your php scripts (what you are writing will be the static html codes) and leave the php codes untouched. This way, you will have a standard .php file without or with less includes but contain BOTH .php queries(dynamic) and html codes(static) – Gary Tsui May 30 '11 at 07:47
  • @Gary Tsui Thanks. I will try it out now. – kb0000 May 30 '11 at 07:56
  • in case you need to know which part you need to write to, you can always create custom texts/tags for your eregi_replace or ieregi_replace to function well. (ie. eregi_replace("## section for html ##", $HTML_CONTENT) Something along that line. Hope this helps. – Gary Tsui May 30 '11 at 07:58
  • @Kunal, in addition, if you like my answers, i sincerely appreciate your kindness and acceptance/up-vote. thx. – Gary Tsui May 30 '11 at 08:15
  • @Gary I would have..but I am not able to do so..requires 15 reputation. – kb0000 Jun 01 '11 at 05:49
  • @Kunal Thx, you should be able to now. sincerely appreciate. thx. – Gary Tsui Jun 01 '11 at 06:02
1

Maybe the answer to this question helps you:

PHP include(): File size & performance

Community
  • 1
  • 1
SynerCoder
  • 12,493
  • 4
  • 47
  • 78
1

If the information only lasts for one hour and will be changed, then it's no reason using cache for that section of information, because the next time people visit, they will get another information and the cached one goes waste.

And, I don't think there is much difference between including a file and including a file's content in the current page, since they will all be executed similarly. The use of include() just makes your code look cleaner, easier to control and maintain.

Turning now to the question why your homepage loads too slow, I think it's not a problem with your include()'s, but could be a problem with your way of processing data. As somebody commented in your post, use Xdebug to find what makes your homepage slow.

Good luck.

  • if information lasts for one hour, there IS a reason for caching, if we are talking about *server-side* cache. If talking about including files, including file will parse it (and potentially perform some heavy manipulations like doing DB queries), while outputting file's content will just read file's content and output it - that's a huge difference. – binaryLV May 30 '11 at 07:40
  • @binaryLV: why do you think the information should be cached when it only appears for an hour? And, due to what I know, include() statement includes and evaluates the specified file, not doing any execution unless there are any calls for the methods. You should take a look at this page http://stackoverflow.com/questions/2298196/php-include-file-size-performance –  May 30 '11 at 08:46