PHP Performance Optimization
In this page:
Database Queries Minimize करना
किसी loop के अंदर एक query चलाना database round-trips को unnecessarily multiply करता है; एक ही query में पहले से हर ज़रूरी चीज़ fetch करना (एक थोड़ी बड़ी भी) लगभग हमेशा कई छोटी queries से तेज़ है।
उदाहरण: Minimizing Database Queries
<?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";
}
?>
Login to try C/C++/Java/PHP code in the editor
OPcache Integration
PHP आमतौर पर हर request पर fresh interpret होता है; OPcache scripts को एक बार compile करता है और compiled bytecode को shared memory में रखता है, हर बाद की request पर उस repeated parsing step को skip करते हुए एक बड़े speed boost के लिए।
उदाहरण: OPcache Integration
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Output Buffering
Output buffering browser को कुछ भी भेजने से पहले सारा generated HTML memory में collect करता है, जो server को कई छोटे chunks के बजाय एक complete response flush करने देता है, perceived network overhead कम करते हुए।
उदाहरण: Output Buffering
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
Loops और Strings Optimize करना
किसी loop की condition के अंदर count() जैसा एक expensive function call करना इसे हर single iteration पर re-evaluate करता है; इसे पहले से एक बार compute करके एक variable में store करना उस repeated cost को हटा देता है।
उदाहरण: Optimizing Loops and Strings
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
Memory Profiling
memory_get_usage() report करता है कि आपकी script इस समय कितनी memory consume कर रही है, जो vague performance concerns को concrete numbers में बदल देता है जिन्हें आप किसी optimization से पहले और बाद में track कर सकें।
उदाहरण: Memory Profiling
<?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";
?>
Login to try C/C++/Java/PHP code in the editor
- किसी loop के अंदर database queries चलाना, जो queries की संख्या को multiply करता है।
- production server पर OPcache disabled छोड़ना।
- यह measure करने से पहले code optimize करना कि time actually कहाँ खर्च हो रहा है।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser