This goes out to Leigh Purdie (5 years ago) and also Dade Brandon (4 months ago)
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'
real 0m1.482s
user 0m1.616s
sys 0m0.152s
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=fgets($fp,65535)) { 1; } fclose($fp);'
real 0m7.281s
user 0m7.392s
sys 0m0.136s
My Results:
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'
real 0m0.341s
user 0m0.352s
sys 0m0.148s
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=fgets($fp,65535)) { 1; } fclose($fp);'
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_0
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_1
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_2
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_3
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_4
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_5
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_6
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_7
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_8
So i say Leigh posting and though omg i need to change all my fgets to stream_get_line. Then i ran the tests as shown in Leigh Purdie comment His results:_9
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'0
$ time yes "This is a test line" | head -1000000 | php -r '$fp=fopen("php://stdin","r"); while($line=stream_get_line($fp,65535,"\n")) { 1; } fclose($fp);'1
Diberi referensi file, temukan jumlah baris dalam file ini menggunakan PHP. Ada total 3 pendekatan untuk menyelesaikan ini
tes. txt. File ini digunakan untuk menguji semua kode PHP berikut
Geeks For GeeksPendekatan 1. Muat seluruh file ke dalam memori dan kemudian gunakan fungsi count() untuk mengembalikan jumlah baris dalam file ini
Contoh
PHP
<?php
$filePath ="test.txt";
31 =3334$filePath36
38 31;
3_1
Keluaran
3_Pendekatan ini memuat seluruh file ke dalam memori yang dapat menyebabkan masalah kelebihan memori
Pendekatan 2. Di sini kita akan memuat hanya satu baris file pada satu waktu yang lebih baik daripada pendekatan pertama. Fungsi PHP fopen() digunakan untuk membuka file atau URL. Fungsi PHP fgets() digunakan untuk mengembalikan baris dari file terbuka. Fungsi fclose() digunakan untuk menutup pointer file
Contoh
PHP
<?php
$filePath="test.txt";
39 30
32 =343536373839
<?php1<?php2<?php33532<?php6
________78______7<?php8 = 0353239
________78______739 6
________79______8
_9
$filePath13239
_9
38 39;
3_1
Keluaran
3_Bagaimana jika file 1GB hanya memiliki satu baris di dalamnya. Kode ini akan memuat seluruh 1GB data yang dapat menyebabkan masalah kelebihan memori
Pendekatan 3. Kami hanya akan memuat ukuran data tertentu ke dalam memori. Kami tidak peduli dengan jeda baris pada saat memuat. Kami akan mengulangi data yang dimuat untuk menghitung jumlah jeda baris di dalamnya
Contoh
PHP
<?php
$filePath="test.txt";
39 30
32 =3435$filePath373839
_9
<?php1<?php2<?php33532<?php6
<?php7;7
________78______7<?php8 = 03532304
3_05
<?php7307
<?php7309
________78______739 =39 314<?php8316
________79______8
_9
$filePath13239
_9
38 39;
3_1
Keluaran
3_Ini adalah pendekatan yang lebih baik dalam hal ruang tetapi, perhatikan bahwa kami mengulangi data kami dua kali Jadi, waktu yang dibutuhkan akan menjadi dua kali lipat