This is a discussion on "[SOLVED] Is this a suitable 'search CSV' function?" within the PHP Forum section. This forum, and the thread "[SOLVED] Is this a suitable 'search CSV' function? are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
[SOLVED] Is this a suitable 'search CSV' function?
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
||||
|
||||
|
I've written a very basic search CSV function that finds the line with the matching 'searched' area code and prints out the corresponding area name (Brighton, Tonbridge etc.)
The CSV file is a downloaded list of postcodes in the format Area Code, Area Name on each line (and it's quite large with 2,821 rows) So is this 'efficient' enough to search such a file, or is there a more effective (or quicker) way?
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
|
|
|
|
#2
|
|||
|
|||
|
Re: Is this a suitable 'search CSV' function?
Quote:
Reading the file line by line is probably a bit slow. You could try reading the entire file into an array using the file function and then iterating through the array. If you really need to speed things up, you should consider storing the postcodes in a database instead of in a file. |
|
#3
|
|||
|
|||
|
Re: Is this a suitable 'search CSV' function?
Yup, like hschmitz said, the most efficient way would be to import the post codes into a database.
If you want to stick with files, then I have to disagree with hschmitz. I think line by line is faster and better programming practice. Let PHP handle reading the file with a stream and pointer. This way, when you find the post code you are searching for on the 10th line, you can just delete the file resource and continue. It's unnecessary to load the whole file into an array ... you're just filling up your server RAM and re-inventing the wheel - fopen, fgets, feof and fclose are optimized for doing exactly this....
Last Blog Entry: 10 Reasons Why My Laptop Is Better Than Your Girlfriend (Dec 15th, 2007)
|
|
#4
|
|||
|
|||
|
Re: Is this a suitable 'search CSV' function?
Quote:
Concerning speed, my recollection is that IO functions normally slow down the code. It is normally a lot faster to read a big block of data into memory once and then performing operations on it. Multiple calls to fgets will tend to slow the system down. This is my experience with a number of different programming languages. I haven't tested this with PHP, but I will do this first thing tomorrow. I'll let you know my results. |
|
#5
|
||||
|
||||
|
Re: Is this a suitable 'search CSV' function?
Hey All
Thanks for the feedback I agree about the db but I just don't have that facility with this hosting package, so it's the next best thing really. Cheers Alex
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
|
|
#6
|
|||
|
|||
|
Re: Is this a suitable 'search CSV' function?
Last Blog Entry: 10 Reasons Why My Laptop Is Better Than Your Girlfriend (Dec 15th, 2007)
|
|
#7
|
|||
|
|||
|
Re: [SOLVED] Is this a suitable 'search CSV' function?
Ok, I've run some benchmarks and here are my results.
I've tested three different file sizes: The small file contains 300 records, the medium file contains 3000 records and the large file contains 30000 records. The data in the files was generated randomly. Each line consisted of a three letter "postcode"followed by a comma followed by a 15 letter name. Each file was searched 10 times to get some kind of average value. I have taken the time used by the function call using the microtime function. I found that, especially for the line by line reading, the time needed for the first run was significantly longer than for the following runs. This is probably because the system caches the file after the first read. So here are the numbers (times are in seconds):
which method is chosen in terms of speed. If the search is performed often the data will be cached. If the search is performed only once in a while, it doesn't matter if it is a bit slower. Let the system judge which data to buffer in the memory. |
|
#8
|
|||
|
|||
|
Re: [SOLVED] Is this a suitable 'search CSV' function?
Hey, kool!!!
Thanks a lot. Really interesting!!!
Last Blog Entry: 10 Reasons Why My Laptop Is Better Than Your Girlfriend (Dec 15th, 2007)
|
|
#9
|
||||
|
||||
|
Re: [SOLVED] Is this a suitable 'search CSV' function?
That is quite interesting, I didn't know about the microtime function. Thanks!
Last Blog Entry: 3D Chess in your browser! (Mar 14th, 2008)
|
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help with echo function on PHP [SOLVED] | sing2trees | PHP Forum | 15 | Jun 7th, 2008 19:14 |
| [SOLVED] Calc function | Pugger | JavaScript Forum | 15 | Nov 6th, 2007 20:08 |
| Adding a search function | bob_visualefx | Web Page Design | 10 | Apr 17th, 2006 12:25 |
| criteria search function | akdiver | Other Programming Languages | 2 | Aug 27th, 2005 00:10 |
| search function in ASP! | Monie | Classic ASP | 29 | Aug 12th, 2004 08:23 |