7, September 2008

Parsing a text file into an array - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Parsing a text file into an array
« previous next »
Pages: [1] Print

Author Topic: Parsing a text file into an array  (Read 736 times)
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« on: Feb 16, 2006, 09:57:15 AM »

Just wanted to check what was the best and fastestway to upload a file and have the php script read each line of a text file into an array.

I will post my code after a few samples have been posted so as to not taint answers...


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #1 on: Feb 16, 2006, 10:01:23 AM »

The fastest way to parse a file is using something like :

Code:
<?php
$handle 
fopen("path/to/file""rb");
$contents '';
while (!
feof($handle)) {
  
$contents .= fread($handle8192);
}
fclose($handle);
?>


The easiest is

Code:
<?php
$contents 
file_get_contents("/path/to/file");
?>


I suppose the decision on what to use is depending on how big the file you use is(for memory reasons)

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #2 on: Feb 16, 2006, 10:03:39 AM »

I wasn't right. file_get_contents is faster than fread

from php.net :
Quote
Note:  If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance than the code above.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #3 on: Feb 16, 2006, 10:42:06 AM »

Well, you have hit the nail on the head! It is exactly the same as what I have.

The part that parses the text file into an array (and in this case each line per element of the array) goes like this...

Code:
$array = explode("\n", $contents);

Nice!


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #4 on: Feb 16, 2006, 10:47:11 AM »

In this case I think the faster would be this :

Code:
<?php
$array 
= array();
$handle fopen("path/to/file""r");
while (!
feof($handle)) {
  
$array fgets($handle4096);
}
fclose($handle);
?>



Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #5 on: Feb 16, 2006, 10:54:14 AM »

fgets returns a string though, will this work?

I think that I will test both and time them!


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #6 on: Feb 16, 2006, 10:57:24 AM »

fgets($handle, 4096);

mean that will read one line from the $handle with maximum limit 4096 bytes/line.

It is much faster, because it will create the array while parsing, instead of explode("\n", file_get_contents($file)); which will first load the file in memory and then will parse it to create the array.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #7 on: Feb 16, 2006, 10:58:22 AM »

Ooops I made a small error in the code.

The right is $array [] = fgets($handle, 4096);

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #8 on: Feb 16, 2006, 11:01:44 AM »

Yes, I suspect you are right! I will time both and post results. It will be a nice test to show...


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #9 on: Feb 16, 2006, 11:03:14 AM »

Yeah. I wait for your results Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #10 on: Feb 16, 2006, 11:19:00 AM »

You may be pleasantly surprised here! Here are the results run five times...

explode: 0.043 ms
fgets:     0.131 ms

explode: 0.043 ms
fgets:     0.132 ms

explode: 0.041 ms
fgets:     0.134 ms

explode: 0.043 ms
fgets:     0.133 ms

explode: 0.040 ms
fgets:     0.132 ms

file size : 1.6Mb

...


Last blog : Site of the Month - August 2007
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #11 on: Feb 16, 2006, 11:20:12 AM »

On average it seems to be three times faster... in explodes favour!


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #12 on: Feb 16, 2006, 11:22:26 AM »

Strange huh?

You tried it in windows or linux?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #13 on: Feb 16, 2006, 11:29:17 AM »

at this stage it is still on my windows laptop, but I will upload it to our linux server and test again...


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #14 on: Feb 16, 2006, 11:33:03 AM »

You should do that.

Also I think  there is another catch in this. Even that explode is faster, it costs more in memory.

On a server environment where many clients can seek the same page at once maybe this could be a prob.

Of course all of these are just predictions, and are actually fitable for sites with hundrends of thousands daily visits.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1523
6847 credits
Members referred : 8


Gimme all your cookies!!!


« Reply #15 on: Feb 16, 2006, 11:44:23 AM »

I got very similar results on a linux server.

I was also thinking about the load on the server, but i think that it is too much effort to monitor that.

The files that are going to be uploaded shouldn't be more than 100K anyway, so either method will do fine anyway!

Anyway, well at least we learnt something today!


Last blog : Site of the Month - August 2007
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #16 on: Feb 16, 2006, 11:46:28 AM »

Quote
Anyway, well at least we learnt something today!

That's true, and as my ancient fathers said once "Girasko aei didaskomenos" which mean something like "I learn while I am getting older(forever)"

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=1514
Tags : php linux Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: Parsing a text file into an array
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 07, 2008, 11:49:40 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.302
Total Topics: 7.479
Total Members: 3.906
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: powerpoint

26 Guests, 3 Users online :

6 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.