Portál AbcLinuxu, 30. dubna 2025 12:40
<?php // ########################################################################## // Benchmark fopen, file_get_contents, require, require_once // Author Mario Chorvath - Bedna, License GPLv3+: GNU GPL version 3 or later // ########################################################################## // 3 reps for ($r = 1; $r <= 3; $r++) { echo $r.'. test
'; // Number of repetitions $loop = 100000; // Open 3 files with 2 lines $time = microtime(true); for ($i = 0; $i < $loop; $i++) { $handle = fopen('2_1.txt', r); $line = fgets($handle); $line = fgets($handle); fclose($handle); $handle = fopen('2_2.txt', r); $line = fgets($handle); $line = fgets($handle); fclose($handle); $handle = fopen('2_3.txt', r); $line = fgets($handle); $line = fgets($handle); fclose($handle); } echo 'Open 3 files with 2 lines '; echo microtime(true)-$time.'s
'; // Open 1 file with 6 lines $time = microtime(true); for ($i = 0; $i < $loop; $i++) { $handle = fopen('6.txt', r); $line = fgets($handle); $line = fgets($handle); $line = fgets($handle); $line = fgets($handle); $line = fgets($handle); $line = fgets($handle); fclose($handle); } echo 'Open 1 file with 6 lines '; echo microtime(true)-$time.'s
'; // Open 1 file with 6 lines (file_get_contents) $time = microtime(true); for ($i = 0; $i < $loop; $i++) { $content_file = file_get_contents('6.txt', false); $strings = explode("\n", $content_file); // Store lines in the array } echo 'Open 1 file with 6 lines (file_get_contents) '; echo microtime(true)-$time.'s
'; // Insert php file with 6 lines (require) $time = microtime(true); for ($i = 0; $i < $loop; $i++) { require ('6.php'); } echo 'Insert php file with 6 lines (require) '; echo microtime(true)-$time.'s
'; // Insert php file with 6 lines (require_once) $time = microtime(true); for ($i = 0; $i < $loop; $i++) { require_once ('6.php'); } echo 'Insert php file with 6 lines (require_once) '; echo microtime(true)-$time.'s
'; } ?>
1. test Open 3 files with 2 lines 2.2364869117737s Open 1 file with 6 lines 0.85146999359131s Open 1 file with 6 lines (file_get_contents) 0.95627999305725s Insert php file with 6 lines (require) 2.1108641624451s Insert php file with 6 lines (require_once) 0.12839698791504s 2. test Open 3 files with 2 lines 2.2577669620514s Open 1 file with 6 lines 0.85908102989197s Open 1 file with 6 lines (file_get_contents) 1.0589778423309s Insert php file with 6 lines (require) 2.1393258571625s Insert php file with 6 lines (require_once) 0.12791395187378s 3. test Open 3 files with 2 lines 2.3164110183716s Open 1 file with 6 lines 0.87628293037415s Open 1 file with 6 lines (file_get_contents) 1.0660879611969s Insert php file with 6 lines (require) 2.1195430755615s Insert php file with 6 lines (require_once) 0.12526202201843s
Tiskni
Sdílej:
Možno to niekomu pomôže keď sa bude rozhodovať údaje vložiť do jedného, alebo viacerých súborov.Otevírání/zavírání souboru je relativně drahá operace. Ale je otázka, jestli to člověk potřebuje dělat 100 000× a jestli se to nějak reálně projeví. Často je vhodné si načíst data jen jednou do RAM (při startu aplikace) a pak je pokaždé (třeba s každým HTTP) požadavkem číst odtamtud – z proměnné v rámci jednoho procesu. Jenže tohle jde bohužel v PHP špatně nebo spíš vůbec.
Jenže tohle jde bohužel v PHP špatně nebo spíš vůbec.Ale no tak...
do kterého si formou různých modulů nebo externích démonů (viz zmiňovaný memcached) doplním co potřebuji, než abych dělal v tom neskutečném molochu, jakým Java je.V Javě si zase napíšeš jednu třídu, která implementuje nějaké rozhraní nebo dědí abstraktního předka, případně přidáš nějakou tu anotaci… přijde mi to jednodušší než externí démoni a moduly do webserveru… Resp. takový ten klasický LAMP (alternativně Nginx a PostgreSQL) a standardní moduly, to je fajn a jednoduché, ale když potřebuješ doprogramovat nějakou vlastní specialitu, na to mi přijde jednodušší ta Java.
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.