← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 12 of 24

PHP Performance Optimization

Performance optimization आपके code को तेज़ और हल्का बनाना है, एक suitcase को smartly pack करने जैसा ताकि आप कम trips करें। छोटे changes, जैसे database से कम questions पूछना, pages को जल्दी load होने में मदद करते हैं।

Database Queries Minimize करना

किसी loop के अंदर एक query चलाना database round-trips को unnecessarily multiply करता है; एक ही query में पहले से हर ज़रूरी चीज़ fetch करना (एक थोड़ी बड़ी भी) लगभग हमेशा कई छोटी queries से तेज़ है।

उदाहरण: Minimizing Database Queries

php
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$db->exec("INSERT INTO users VALUES (1,'Alice'),(2,'Bob')");
// One query instead of querying inside a loop per user
$result = $db->query("SELECT * FROM users");
while ($row = $result->fetchArray()) {
    echo $row['name'] . "\n";
}
?>

OPcache Integration

PHP आमतौर पर हर request पर fresh interpret होता है; OPcache scripts को एक बार compile करता है और compiled bytecode को shared memory में रखता है, हर बाद की request पर उस repeated parsing step को skip करते हुए एक बड़े speed boost के लिए।

उदाहरण: OPcache Integration

php
<?php
// Print `function_exists('opcache_get_status') ? "OPcache extension loaded" : "OPcache not available"` to the output
echo function_exists('opcache_get_status') ? "OPcache extension loaded" : "OPcache not available";
// Print "\nOPcache caches compiled bytecode, skipping re-parsing on every request" to the output
echo "\nOPcache caches compiled bytecode, skipping re-parsing on every request";
?>

Output Buffering

Output buffering browser को कुछ भी भेजने से पहले सारा generated HTML memory में collect करता है, जो server को कई छोटे chunks के बजाय एक complete response flush करने देता है, perceived network overhead कम करते हुए।

उदाहरण: Output Buffering

php
<?php
// Call `ob_start()`
ob_start();
// Print "<p>Part one</p>" to the output
echo "<p>Part one</p>";
// Print "<p>Part two</p>" to the output
echo "<p>Part two</p>";
// Declare `$html`, set to `ob_get_clean()`
$html = ob_get_clean();
// Print `"Buffered length: " . strlen($html)` to the output
echo "Buffered length: " . strlen($html);
?>

Loops और Strings Optimize करना

किसी loop की condition के अंदर count() जैसा एक expensive function call करना इसे हर single iteration पर re-evaluate करता है; इसे पहले से एक बार compute करके एक variable में store करना उस repeated cost को हटा देता है।

उदाहरण: Optimizing Loops and Strings

php
<?php
$items = range(1, 1000);
$count = count($items); // computed once, not on every iteration
for ($i = 0; $i < $count; $i++) {
    // work
}
echo "Loop ran $count times without recalculating count() each pass";
?>

Memory Profiling

memory_get_usage() report करता है कि आपकी script इस समय कितनी memory consume कर रही है, जो vague performance concerns को concrete numbers में बदल देता है जिन्हें आप किसी optimization से पहले और बाद में track कर सकें।

उदाहरण: Memory Profiling

php
<?php
// Declare `$before`, set to `memory_get_usage()`
$before = memory_get_usage();
// Declare `$data`, set to `range(1, 10000)`
$data = range(1, 10000);
// Declare `$after`, set to `memory_get_usage()`
$after = memory_get_usage();
// Print "Memory used: " . ($after - $before) . " bytes" to the output
echo "Memory used: " . ($after - $before) . " bytes";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी loop के अंदर database queries चलाना, जो queries की संख्या को multiply करता है।
  2. production server पर OPcache disabled छोड़ना।
  3. यह measure करने से पहले code optimize करना कि time actually कहाँ खर्च हो रहा है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.