Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73744
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: Python While loop Takes too much time. |
| Date | 2014-06-30 14:46 +0200 |
| Organization | None |
| References | <e5af8347-010d-45bd-acad-710a0a22d509@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.11338.1404132401.18130.python-list@python.org> (permalink) |
Jaydeep Patil wrote:
> I have did excel automation using python.
> In my code I am creating python dictionaries for different three columns
> data at a time.There are are many rows above 4000. Lets have look in below
> function. Why it is taking too much time?
>
> Code:
>
> def transientTestDict(self,ws,startrow,startcol):
>
> self.hwaDict = OrderedDict()
> self.yawRateDict = OrderedDict()
>
> rng = ws.Cells(startrow,startcol)
>
> while not rng.Value is None:
> r = rng.Row
> c = rng.Column
>
> time = rng.Value
>
> rng1 = rng.GetOffset(0,1)
> hwa = rng1.Value
>
> rng2 = rng.GetOffset(0,2)
> yawrate = rng2.Value
>
> self.hwaDict[time] = hwa,rng.Row,rng.Column
> self.yawRateDict[time] = yawrate,rng.Row,rng.Column
>
> rng = ws.Cells(r+1,c)
>
>
>
> Please have look in above code & suggest me to improve speed of my code.
Assuming that what slows down things is neither Python nor Excel, but the
communication between these I'd try to do as much as possible in Python. For
example (untested):
def transientTestDict(self, ws, startrow, startcol):
self.hwaDict = OrderedDict()
self.yawRateDict = OrderedDict()
time_col, hwa_col, yawrate_col = range(startcol, startcol+3)
for row in xrange(startrow, sys.maxint):
time = ws.Cells(row, time_col).Value
if time is None:
break
hwa = ws.Cells(row, hwa_col).Value
yawrate = ws.Cells(row, yawrate_col).Value
self.hwaDict[time] = hwa, row, time_col
self.yawRateDict[time] = yawrate, row, time_col
While this avoids cell arithmetic in Excel it still fetches every value
separately, so I have no idea if there is a significant effect.
Does Excel provide a means to get multiple cell values at once? That would
likely help.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python While loop Takes too much time. Jaydeep Patil <patil.jay2009@gmail.com> - 2014-06-30 04:32 -0700
Re: Python While loop Takes too much time. Peter Otten <__peter__@web.de> - 2014-06-30 14:46 +0200
Re: Python While loop Takes too much time. Jaydeep Patil <patil.jay2009@gmail.com> - 2014-07-01 05:04 -0700
Re: Python While loop Takes too much time. Peter Otten <__peter__@web.de> - 2014-07-01 14:40 +0200
Re: Python While loop Takes too much time. Denis McMahon <denismfmcmahon@gmail.com> - 2014-07-01 18:45 +0000
Re: Python While loop Takes too much time. marco.nawijn@colosso.nl - 2014-06-30 11:34 -0700
Re: Python While loop Takes too much time. Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-07-01 11:50 +1200
csiph-web