473,809 Members | 2,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to build a dict including a large number of data

hi everyone
i'm a newbie to python :)
i have a binary file named test.dat including 9600000 records.
the record format is int a + int b + int c + int d
i want to build a dict like this: key=int a,int b values=int c,int d
i choose using bsddb and it takes about 140 seconds to build the dict.
what can i do if i want to make my program run faster?
or is there another way i can choose?
Thanks in advance.

My Code:
-----------------------------------------------------------------------------------
my_file = file('test.dat' ,'rb')
content = my_file.read()
record_number = len(content) / 16

db = bsddb.btopen('t est.dat.db','n' ,cachesize=5000 00000)
for i in range(0,record_ number):
a = struct.unpack(" IIII",content[i*16:i*16+16])
db['%d_%d' % (a[0],a[1])] = '%d_%d' % (a[2],a[3])

db.close()
my_file.close()
Jan 4 '08 #1
3 1737
On Jan 4, 3:57 pm, wanzathe <wanza...@gmail .comwrote:
hi everyone
i'm a newbie to python :)
i have a binary file named test.dat including 9600000 records.
the record format is int a + int b + int c + int d
i want to build a dict like this: key=int a,int b values=int c,int d
i choose using bsddb and it takes about 140 seconds to build the dict.
what can i do if i want to make my program run faster?
or is there another way i can choose?
Thanks in advance.

My Code:
-----------------------------------------------------------------------------------
my_file = file('test.dat' ,'rb')
content = my_file.read()
record_number = len(content) / 16

db = bsddb.btopen('t est.dat.db','n' ,cachesize=5000 00000)
for i in range(0,record_ number):
a = struct.unpack(" IIII",content[i*16:i*16+16])
db['%d_%d' % (a[0],a[1])] = '%d_%d' % (a[2],a[3])

db.close()
my_file.close()
my_file = file('test.dat' ,'rb')
db = bsddb.btopen('t est.dat.db','n' ,cachesize=5000 00000)
content = myfile.read(16)
while content:
a = struct.unpack(' IIII',content)
db['%d_%d' % (a[0],a[1])] = '%d_%d' % (a[2],a[3])
content = myfile.read(16)

db.close()
my_file.close()

That would be more memory efficient, as for speed you would need to
time it on your side.
Jan 4 '08 #2
wanzathe wrote:
i have a binary file named test.dat including 9600000 records.
the record format is int a + int b + int c + int d
i want to build a dict like this: key=int a,int b values=int c,int d
i choose using bsddb and it takes about 140 seconds to build the dict.
you're not building a dict, you're populating a persistent database.
storing ~70000 records per second isn't that bad, really...
what can i do if i want to make my program run faster?
or is there another way i can choose?
why not just use a real Python dictionary, and the marshal module for
serialization?

</F>

Jan 4 '08 #3
On 1ÔÂ4ÈÕ, ÏÂÎç10ʱ17·Ö, Fredrik Lundh <fred...@python ware.comwrote:
wanzathe wrote:
i have a binary file named test.dat including 9600000 records.
the record format is int a + int b + int c + int d
i want to build a dict like this: key=int a,int b values=int c,int d
i choose using bsddb and it takes about 140 seconds to build the dict.

you're not building a dict, you're populating a persistent database.
storing ~70000 records per second isn't that bad, really...
what can i do if i want to make my program run faster?
or is there another way i can choose?

why not just use a real Python dictionary, and the marshal module for
serialization?

</F>
hi,Fredrik Lundn
you are right, i'm populating a persistent database.
i plan to use a real Python dictionary and use cPickle for
serialization at first, but it did not work because the number of
records is too large.
Thanks
Jan 4 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
12243
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after them. What I want to do is filter out a subset of this dict to produce another dict that satisfies a set of criteria (in this case whether it contains all four values) to end up with something
5
1412
by: cpmcdaniel | last post by:
I was wondering if the following two "if" statements compile down to the same bytecode for a standard Dictionary type: m = {"foo": 1, "blah": 2} if "foo" in m: print "sweet" if m.has_key("foo"): print "dude"
2
2271
by: Dave | last post by:
After much general searching, a JS newbie asks: can anyone point me to where I can find out how to build a page (including form) using Javascript? The reason I'd want to do this is because the page html is becoming quite large due to form element attributes for each row of a large form. I'm under the impression that I could condense this by assigning variables to blocks of text that make up the page elements and using script to write these...
3
3795
by: Peter Beattie | last post by:
I was wondering whether certain data structures in Python, e.g. dict, might have limits as to the amount of memory they're allowed to take up. Is there any documentation on that? Why am I asking? I'm reading 3.6 GB worth of BLAST output files into a nested dictionary structure (dict within dict ...). Looks something like this: { GenomeID: { ProteinID:
7
1683
by: George Young | last post by:
I am puzzled that creating large dicts with an explicit iterable of key,value pairs seems to be slow. I thought to save time by doing: palettes = dict((w,set(w)) for w in words) instead of: palettes={} for w in words: palettes=set(w)
4
3525
dshimer
by: dshimer | last post by:
I have a file whose structure in strictly generic terms is similar to the following.keyname first keyword1 1.1 keyword2 1.2 keyword3 1.3 keyname second keyword1 2.1 keyword2 2.2 keyword3 2.3 keyname third keyword1 3.1
4
1932
by: bcomeara | last post by:
I am writing a program which needs to include a large amount of data. Basically, the data are p values for different possible outcomes from trials with different number of observations (the p values are necessarily based on slow simulations rather than on a standard function, so I estimated them once and want the program to include this information). Currently, I have this stored as a vector of vectors of varying sizes (first vector is...
3
5162
by: james_027 | last post by:
hi, a_dict = {'name':'apple', 'color':'red', 'texture':'smooth', 'shape':'sphere'} is there any difference between .. for key in a_dict: from
14
11808
by: yxq | last post by:
Hello, I want to build the multi-language application with the xml file, how to do? could anyone tell a sample? Thank you
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5689
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.