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

  • Home
  • SEARCH
  • 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 7509789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:01:32+00:00 2026-05-29T23:01:32+00:00

I am creating dictionary out of a large file. def make_dic(): big_dic={} for foo

  • 0

I am creating dictionary out of a large file.

def make_dic():
    big_dic={}
    for foo in open(bar):
           key,value=do_something(foo)
           big_dic[key]=value
def main():
    make_dic() #this takes time

I have to access this dictionary many times but from completely different programs. It takes lot of time to read this file and make dictionary. Is it possible to make a dictionary which remains in memory even if one program exits???? So that I create it once but can use it again and again from different programs….

  • 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-05-29T23:01:35+00:00Added an answer on May 29, 2026 at 11:01 pm

    This won’t work for all situations that fit your description, but cPickle should help with speed.

    The only problem I can think of is that combining data persistence with IPC is tough. So if these different programs are modifying the dictionary at the same time, pickle won’t help. Another approach might be to use a database…

    I like Sven Marnach‘s suggestion, but there are some tradeoffs worth considering. Some setup…

    >>> pickle_file = open('pickle_foo', 'w')
    >>> anydbm_file = anydbm.open('anydbm_foo', 'c')
    >>> d = dict((str(i), str(j)) for i, j in zip(range(999999, -1, -1), range(0, 1000000)))
    

    Obviously populating the anydbm_file will be pretty slow:

    >>> %timeit for k, v in d.iteritems(): anydbm_file[k] = v
    1 loops, best of 3: 5.14 s per loop
    

    The time is comparable to the time it takes to dump and load a pickle file:

    >>> %timeit cPickle.dump(d, pickle_file)
    1 loops, best of 3: 3.79 s per loop
    >>> pickle_file.close()
    >>> pickle_file = open('pickle_foo', 'r')
    >>> %timeit d = cPickle.load(pickle_file)
    1 loops, best of 3: 2.03 s per loop
    

    But the anydbm_file you only have to create once; then, opening it again is nigh-instantaneous.

    >>> %timeit anydbm_file = anydbm.open('anydbm_foo', 'r')
    10000 loops, best of 3: 74.3 us per loop
    

    So anydbm has the advantage there. On the other hand,

    >>> %timeit for i in range(1, 1000): x = anydbm_file[str(i)]
    100 loops, best of 3: 3.15 ms per loop
    >>> %timeit for i in range(1, 1000): x = d[str(i)]
    1000 loops, best of 3: 374 us per loop
    

    Reading a key from anydbm_file takes ten times longer than reading a key from a dictionary in memory. You’d have to do a lot of lookups for this difference to outweigh the 5 seconds necessary for a pickle dump/load cycle; but even if you don’t, the difference in read times here could lead to sluggish performance, depending on what you’re doing.

    Other options are SQLite3 or (for a separate database server process that allows connections from multiple processes running concurrently), psycopg2 + PostgreSQL.

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

Sidebar

Related Questions

I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
Is there some kind of a shorthand fluent interface for creating a parameters dictionary
I'm creating a dictionary database using php and MySQL. The user can search for
Main question: Is there a better way to accomplish creating a reusable control? So
I'm enumerating through a Dictionary and creating an item to be added to a
its me again! :) I've been tasked with creating a system to auditing out
I am creating class instances by running a text file and slicing the words
I have a simple interface with methods such as bool TryGetValue(string key, out string
What I'm trying to do is this. I have a dictionary laid out as
I am creating a BubbleSeries in a function within CS file. As a data

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.