Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'example:': 0.03; 'value,': 0.04; 'terry': 0.07; 'follows.': 0.09; 'ids': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'pm,': 0.10; 'wrote:': 0.14; '10:59': 0.16; 'file1': 0.16; 'gift)': 0.16; 'reedy': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; 'pair': 0.23; 'similarly': 0.23; 'values': 0.25; 'bound': 0.29; 'temporary': 0.29; 'second': 0.30; 'print': 0.31; 'minor': 0.32; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; 'break': 0.33; 'lines': 0.33; 'list.': 0.33; 'header:User- Agent:1': 0.35; 'try:': 0.35; 'two': 0.37; 'received:org': 0.38; 'creates': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'subject:about': 0.60; 'more': 0.60; 'unnecessary': 0.73; 'charset:gb2312': 0.76; 'below:': 0.79; 'sum': 0.88 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: something about performence Date: Tue, 21 Jun 2011 18:41:31 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 81 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308696111 news.xs4all.nl 49178 [::ffff:82.94.164.166]:43673 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8139 On 6/20/2011 10:59 PM, king6cong@gmail.com wrote: > Hi=A3=AC > I have two large files,each has more than 200000000 lines,and each line= =20 > consists of two fields,one is the id and the other a value, > the ids are sorted. >=20 > for example: >=20 > file1 > (uin_a y) > 1 10000245 > 2 12333 > 3 324543 > 5 3464565 > .... >=20 >=20 > file2 > (uin_b gift) > 1 34545 > 3 6436466 > 4 35345646 > 5 463626 > .... >=20 > I want to merge them and get a file,the lines of which consists of an i= d=20 > and the sum of the two values in file1 and file2=A1=A3 > the codes are as below: One minor thing you can do is use bound methods >=20 > uin_y=3Dopen('file1') > uin_gift=3Dopen(file2') ynext =3D open('file1').next gnext =3D open(file1').next > y_line=3Duin_y.next() > gift_line=3Duin_gift.next() y_line =3D ynext() gift_list =3D gnext() and similarly for all .next appearances in what follows. > while 1: > try: > uin_a,y=3D[int(i) for i in y_line.split()] This creates an unnecessary second temporary list. Unroll the loop. pair =3D y_line.split uin_a =3D int(pair[0]) y =3D int(pair[1]) > uin_b,gift=3D[int(i) for i in gift_line.split()] same for this line > if uin_a=3D=3Duin_b: > score=3Dy+gift > print uin_a,score > y_line=3Duin_y.next() > gift_line=3Duin_gift.next() > if uin_a print uin_a,y > y_line=3Duin_y.next() > if uin_a>uin_b: > print uin_b,gift > gift_line=3Duin_gift.next() > except StopIteration: > break --=20 Terry Jan Reedy