Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8321523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:03:03+00:00 2026-06-08T23:03:03+00:00

I have found a few similar questions here in Stack Overflow, but I believe

  • 0

I have found a few similar questions here in Stack Overflow, but I believe I could benefit from advice specific for my case.

I must store around 80 thousand lists of real valued numbers in a file and read them back later.

First, I tried cPickle, but the reading time wasn’t appealing:

>>> stmt = """
with open('pickled-data.dat') as f:
    data = cPickle.load(f)
"""
>>> timeit.timeit(stmt, 'import cPickle', number=1)
3.8195440769195557

Then I found out that storing the numbers as plain text allows faster reading (makes sense, since cPickle must worry about a lot of things):

>>> stmt = """
data = []
with open('text-data.dat') as f:
    for line in f:
        data.append([float(x) for x in line.split()])
"""
>>> timeit.timeit(stmt, number=1)
1.712096929550171

This is a good improvement, but I think I could still optimize it somehow, since programs written in other languages can read similar data from files considerably faster.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:03:04+00:00Added an answer on June 8, 2026 at 11:03 pm

    If numpy arrays are workable, numpy.fromfile will likely be the fastest option to read the files (here’s a somewhat related question I asked just a couple days ago)

    Alternatively, it seems like you could do a little better with struct, though I haven’t tested it:

    import struct
    def write_data(f,data):
        f.write(struct.pack('i',len()))
        for lst in data:
            f.write(struct.pack('i%df'%len(lst),len(lst),*lst))
    
    def read_data(f):
        def read_record(f):
            nelem = struct.unpack('i',f.read(4))[0]
            return list(struct.unpack('%df'%nelem,f.read(nelem*4))) #if tuples are Ok, remove the `list`.
    
        nrec = struct.unpack('i',f.read(4))[0]
        return [ read_record(f) for i in range(nrec) ]
    

    This assumes that storing the data as 4-byte floats is good enough. If you want a real double precision number, change the format statements from f to d and change nelem*4 to nelem*8. There might be some minor portability issues here (endianness and sizeof datatypes for example).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i found few similar questions here but from answers i didn't get the whole
I have found a few good replies to similar content so far, but never
There were already a few similar questions on stackoverflow, but I haven't found the
I've seen a few questions that were similar but not specific enough. This one
There are a few questions on here that are similar, but they didn't work
I am aware that there were similar questions in past few years, but after
I've found a few posts that are similar in nature to this but they
Let me start by saying I have looked at many similar questions asked, but
I have been doing some research about this and have found a few similar
There have been similar questions but the answers weren't what I was looking for.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.