Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'subject:Python': 0.06; 'none:': 0.07; 'assuming': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; 'columns': 0.16; 'dictionaries': 0.16; 'effect.': 0.16; 'fetches': 0.16; 'patil': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:too': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'help.': 0.21; 'example': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'lets': 0.24; 'code:': 0.26; 'excel': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'idea': 0.28; 'code': 0.31; 'skip:t 40': 0.33; "i'd": 0.34; 'but': 0.35; 'there': 0.35; 'possible': 0.36; 'too': 0.37; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'skip:y 20': 0.60; 'break': 0.61; 'provide': 0.64; 'different': 0.65; 'taking': 0.65; 'between': 0.67; 'avoids': 0.84; 'excel,': 0.84; 'subject:much': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Python While loop Takes too much time. Date: Mon, 30 Jun 2014 14:46:21 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb6df.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404132401 news.xs4all.nl 2975 [2001:888:2000:d::a6]:48990 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73744 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.