[SOLVED] Is this a suitable 'search CSV' function?

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 17th, 2007, 17:41
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,011
Blog Entries: 2
Thanks: 5
Thanked 23 Times in 20 Posts
Send a message via Skype™ to Aso
Lightbulb [SOLVED] Is this a suitable 'search CSV' function?

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?

PHP: Select all

<?php
function searchPostcode($areaCode) {
$file fopen("postcodes.csv""rb");
    while (!
feof($file) ) {
    
$line fgets($file);
    
$parts explode(','$line);
        if (
in_array($areaCode,$parts)) {
        print 
'Area Code: '.$parts[0].' Name: '.$parts[1];
        }
    }
fclose($file);
}
?>
Many thanks
Last Blog Entry: The Google Misconception (Feb 3rd, 2008)
Reply With Quote

  #2 (permalink)  
Old Dec 17th, 2007, 19:02
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Is this a suitable 'search CSV' function?

Quote:
Originally Posted by aso186 View Post

So is this 'efficient' enough to search such a file, or is there a more effective (or quicker) way?
This clearly depends on your application.

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.
Reply With Quote
  #3 (permalink)  
Old Dec 17th, 2007, 20:57
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
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....
Reply With Quote
  #4 (permalink)  
Old Dec 17th, 2007, 21:56
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Is this a suitable 'search CSV' function?

Quote:
Originally Posted by c010depunkk View Post
I think line by line is faster and better programming practice. Let PHP handle reading the file with a stream and pointer.
In terms of cleaner programming, I agree that reading the file line by line is better practice.

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.
Reply With Quote
  #5 (permalink)  
Old Dec 17th, 2007, 23:31
Aso's Avatar
Aso Aso is offline
Chief Moderator

SuperMember
Join Date: Oct 2007
Location: UK
Posts: 1,011
Blog Entries: 2
Thanks: 5
Thanked 23 Times in 20 Posts
Send a message via Skype™ to Aso
Re: Is this a suitable 'search CSV' function?

Hey All

Thanks for the feedback . I ran a few primitive tests and the times weren't bad for line by line, plus fair point c010depunkk regarding 10th line etc., so I stuck with .

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)
Reply With Quote
  #6 (permalink)  
Old Dec 18th, 2007, 06:19
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: Is this a suitable 'search CSV' function?

Quote:
Originally Posted by hschmitz View Post
I'll let you know my results.
Please do. I'd be interested....
Reply With Quote
  #7 (permalink)  
Old Dec 18th, 2007, 12:37
Up'n'Coming Member
Join Date: Jun 2007
Location: Birmingham, UK
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
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):

Code: Select all
First Read:

        line         file
short:    0.014614     0.001286
medium: 0.049765     0.011119
long:   0.121069     0.109896


FollowingReads (avg over 9 runs):

        line         file
short:  0.00161      0.00171    
medium: 0.015044     0.029462
long:   0.211312     0.195516
Judging from these results, I have to admit that is doesn't really matter
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.
Reply With Quote
  #8 (permalink)  
Old Dec 18th, 2007, 13:53
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: [SOLVED] Is this a suitable 'search CSV' function?

Hey, kool!!!

Thanks a lot. Really interesting!!!
Reply With Quote
  #9 (permalink)  
Old Dec 18th, 2007, 15:19
alexgeek's Avatar
Technical Administrator

SuperMember
Join Date: Jul 2007
Location: Webforumz 24/7
Age: 15
Posts: 3,770
Blog Entries: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to alexgeek
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)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 02:15.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43