Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > speed up linecache.getline()

Reply
Thread Tools

speed up linecache.getline()

 
 
bbarbero@inescporto.pt
Guest
Posts: n/a
 
      10-13-2009
Hi Everyone!!


I am using linecache.getline, to access to a line in a long file. It s
really fast, appx 4seconds, but I was just wandering if any of you,
know either another way, or there is something that I can do to speed
it up... thank you very much for your help!!

Regards,
Bea











Quoting Chris Rebert <(E-Mail Removed)>:

> On Wed, Oct 7, 2009 at 10:21 AM, <(E-Mail Removed)> wrote:
>> Hi again!
>>
>> After testing the whole day, I have got my goals from the last email, but as
>> always, another issues came up! and now that Ive been able to save a list of
>> list (or multi-arrays) as below :
>>
>> ['100.mp3\n' '10008.mp3\n' '10005.mp3\n' '10001.mp3\n' '10006.mp3\n']
>> ['10001.mp3\n' '10005.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>> ['10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n' '10006.mp3\n']
>> ['10006.mp3\n' '10005.mp3\n' '10001.mp3\n' '100.mp3\n' '10008.mp3\n']
>> ['10008.mp3\n' '100.mp3\n' '10001.mp3\n' '10005.mp3\n' '10006.mp3\n']
>>
>> I am not able to manipulate it again! I read it with:
>> Myfile.read() and all what I get is a str type data, what make my aim very
>> difficult to reach! *What I want, is just to read one line(one specific
>> line, so *I wouldnt have to read the whole file) and to get the numbers of
>> the songs from that line. Maybe I should save the information in another
>> way... But I just get those lines as lists, and write them in a file. Is
>> there a better way? I am very receptive to suggestions! Thanks again for
>> your help!

>
> Have you considered using the `json` module
> (http://docs.python.org/library/json.html) to serialize and
> deserialize the lists to/from a file in JSON format?
> The `pickle` module is another option:
> http://docs.python.org/library/pickle.html
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>




----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-13-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I am using linecache.getline, to access to a line in a long file. It s
> really fast, appx 4seconds, but I was just wandering if any of you,
> know either another way, or there is something that I can do to speed
> it up... thank you very much for your help!!


If it is a single file that never changes just read it into a list:

with open(filename) as f:
cached_lines = list(f)

If that alone takes about 4 seconds the runtime of your script is spent
reading the file from disk. An SSD might help then

In some situations you could delay reading line n until that line is
actually needed. This may speed up your script for cases where all requested
lines are near the beginning of the cached file.

Peter

 
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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reported Wireless speed w/ repeater 7-9x Measured Speed Lance Wireless Networking 0 10-31-2004 09:31 PM
I need speed Mr .Net....speed Ham ASP .Net 6 10-29-2004 08:04 AM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM
java tool to test disk i/o, processor speed, and network speed efiedler Java 1 10-09-2003 03:36 PM
USB High Speed against USB Non High Speed DannyD1355 Computer Support 1 09-07-2003 02:59 AM



Advertisments