performance question: dictionary or list, float or string?
bkamrani at gmail.com
bkamrani at gmail.com
Thu Dec 4 04:26:13 EST 2008
Matt, really thanks for your comments!
Even thogh it was not a direct answer to my questions,
I like your coding style very much and I think you have a good point.
About the number of line in the file, because I get that info from
another
in advance. Therefore I thought it could be hard coded.
BTW, could you recommend a book or a note on points you have mentioned
so that I can learn more like that?
Thanks,
/Ben
On Dec 2, 8:37 pm, Matimus <mccre... at gmail.com> wrote:
> On Dec 2, 3:51 am, bkamr... at gmail.com wrote:
>
>
>
> > I forgot to mention that I did a simple timeit test which doesn't
> > show
> > significant runtime difference 3.5 sec for dictionary case and 3.48
> > for
> > list case.
>
> > def read_as_dictionary():
> > fil = open('myDataFile', 'r')
> > forces = {}
> > for region in range(25):
> > forces[region] = {}
>
> > for step in range(20000):
> > for region in range(25):
> > line = fil.next(); spl = line.split()
> > forces[region] [step] = spl
>
> > def read_as_list():
> > fil = open('myDataFile.txt', 'r')
> > forces = []
> > for region in range(25):
> > forces.append([])
>
> > for step in range(20000):
> > for region in range(25):
> > line = fil.next(); spl = line.split()
> > forces[region].append(spl)
>
> > Cheers,
> > /Ben
>
> There really isn't enough information to recommend a particular
> direction. A dictionary doesn't seem appropriate for
> this information though. Also, you are hard coding the step range to
> 20000. Is that the number of lines in the file? That isn't really a
> safe way to do it.
>
> # this is just bad style in python:
> line = fil.next(); spl = line.split()
> # better written
> spl = fil.next().split()
>
> I would just do it this way:
>
> def read_as_list(data, regions=25, maxlines=20000):
> # If data is a filename, open the file. If it is a file
> # object or any sequence of 'lines' it should just work.
>
> file_opened = False
> if isinstance(data, basestring):
> data = open(data, 'r')
> file_opened = True
>
> forces = [[] for _ in xrange(regions)]
> try:
> for i, line in data:
> if i == maxlines:
> break
> forces[i % 25].append(line.split())
> finally:
> if file_opened:
> f.close()
> return forces
>
> Matt
More information about the Python-list
mailing list