Hi expert,

I was googling for the PHP code optimization. I got one article at
http://phplens.com/lens/php-book/optimizing-debugging-php.php

Article is saying that code should be read by memory line by line, not all the code should be read at once by memory.

How can i recognize that my code should be read line by line or what is method to write the PHP so that code should read by line by line.

Please suggest

Thanks in advance.

    It's not talking about reading your code line-by-line, but having your code read a text file line-by-line instead of loading the entire text file into memory. This would likely mean a choice between using a loop on the fgets() function instead of using a function such as file() or file_get_contents(). Also note that it's not saying one or the other will necessarily be "faster", but that depending on server load and the size of the text file to be read, one may method may perform better than the other depending on the specific circumstances.

      If you are concerned about the performance of your application, e.g. you're using ApacheBench on your test network and finding performance issues which are measurable, then you should install a profiler to determine where the bottleneck(s) lie.

      If you do not have quantitative data about the performance of your application, but it's still a problem, get some first.

      There is no point in optimising unless:
      A) It is required AND
      😎 You can measure your level of success

      Otherwise you're just effectively firing shots in the dark against something which might not even be there.

      You should be sure that there is a problem, be able to measure it, and define a goal which is reasonably attainable. Hardware upgrades may be one option in attaining this goal (which might actually turn out to be cheaper than paying developers to stare at profiler data into the small hours)

      Mark

        Thanks for your (NogDo, MarkR) response.

        Yes, i am running website on very good server configuration. But still, i am facing serious server load problem (paricularly from mysql side). I have optimized my code at the maximum of my knowledge.

        As MarkR is suggesting, it is difficult to find the exact bottleneck of the website.

        Is there anyway to find exact problem ?

        Thanks.

          Read MarkR's reply again. I think you missed the point.

            Ok, you've established that optimisation is a desirable thing to do; now you must be able to measure quantitiatively how well it's doing.

            You can of course benchmark things on your development server, which is good for measuring the effectiveness of specific optimisation attempts, but putting instrumentation on the production server is good to know where to start.

            Normally I'd configure Apache to record the time taken for each (PHP) request- you can do this easily by using a custom log (which I normally call time_log). This will record the date/time of each request, what was requested and the time taken in microseconds. It is possible to have this log record only .php requests (which makes things easier for analysis and makes it smaller; I assume you have a high traffic site so your normal logs will be many megabytes per day).

            You can then analyse these data to find the typical / average / maximum time taken by individual pages, and decide to target those ones specifically.

            Back on your development server, you can then work on those specific pages to improve execution time.

            Of course to know what to do, you may find it handy to have a profiler.

            If there are some very long queries (which your PHP profiler may show up), you may want to put instrumentation into mysql, which also has the ability to log the time taken for slow queries, or all queries.

            If a given query is taking a long time, it may be because you've not got appropriate indexes on the queried columns, or because it is doing a full table scan on a large table.

            Sadly, it is often required to load some production data into your development server to measure performance. I personally hate this, and I would probably rather take a copy of the production DB, wipe out all personal / sensitive info, then dump that copy into the local dev / performance test database. That way you can have a realistic sized data set without having real user data in dev (which I really, really try to avoid).

            Mark

              Thanks Mark for such a valuable information.

                Write a Reply...