Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '-*-': 0.07; 'nested': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'idea?': 0.09; 'skip:# 60': 0.09; 'def': 0.12; '"python': 0.16; 'arent': 0.16; 'itertools': 0.16; 'numpy': 0.16; 'subject:skip:m 10': 0.16; 'import': 0.22; 'print': 0.22; 'skip:_ 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'skip:p 30': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '"': 0.31; 'skip:m 60': 0.31; 'supposed': 0.32; 'run': 0.32; 'received:google.com': 0.35; '8bit%:9': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'skip:\xc2 10': 0.60; 'times': 0.62; 'making': 0.63; '8bit%:10': 0.64; 'dear': 0.65; '\xc2\xa0\xc2\xa0': 0.74; 'subject:space': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Aj5fat4+/1daawH2xQE2FyDz0rBf/PER1+eG9bvA05I=; b=vuGtlHFEwkD9OkxuaZdfqmd7+xFK8IdqSRjcE6CQTcfaeE2n6SgzmmfysrLXiPDWDx MyEWdk9rFjlXlaQTtwj8/by6+wPI1W9e4vMzpfclc4vGJEFmAwO9+vGegdmfDTwAUSwT anNBFU4dpEpo+YoLW4BFuzONKxeeNSDVIQlMwahTXOGxLIhbyi2tKszPFUA20e+xyk+j 9KctBw5jEffXK1om6Ai+NDb8ftIW48+tPDXSsz1WkXU2aGkIpxafVoEE5ZNvadT1I2zQ 0wWa2C94TT9wwN4Ldflkc4d5d3yV6KAcIlXuWjPhu6e6gzJHwpzTtAFASWvsyMpQ73hT z9XA== MIME-Version: 1.0 X-Received: by 10.194.9.228 with SMTP id d4mr12024346wjb.99.1407303253141; Tue, 05 Aug 2014 22:34:13 -0700 (PDT) In-Reply-To: References: Date: Wed, 6 Aug 2014 11:04:13 +0530 Subject: Re: Pythonic way to iterate through multidimensional space? From: Gayathri J To: python-list@python.org Content-Type: multipart/alternative; boundary=047d7b4508a87eae0904ffef52a8 X-Mailman-Approved-At: Wed, 06 Aug 2014 08:11:38 +0200 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: 99 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407305499 news.xs4all.nl 2888 [2001:888:2000:d::a6]:53599 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75780 --047d7b4508a87eae0904ffef52a8 Content-Type: text/plain; charset=UTF-8 Dear Peter Below is the code I tried to check if itertools.product() was faster than normal nested loops... they arent! arent they supposed to be...or am i making a mistake? any idea? *############################################################* *# -*- coding: utf-8 -*-* *import numpy as np* *import time* *from itertools import product,repeat* *def main():* * # N - size of grid* * # nvel - number of velocities* * # times - number of times to run the functions* * N=256* * times=3* * f=np.random.rand(N,N,N)* * # using loops* * print "normal nested loop"* * python_dot_loop1(f,times,N)* * print "nested loop using itertools.product()"* * python_dot_loop2(f,times,N)* *def python_dot_loop1(f,times,N):* * for t in range(times):* * t1=time.time()* * for i in range(N):* * for j in range(N):* * for k in range(N):* * f[i,j,k] = 0.0* * print "python dot loop " + str(time.time()-t1)* *def python_dot_loop2(f,times,N):* * rangeN=range(N)* * for t in range(times):* * t1=time.time()* * for i,j,k in product(rangeN,repeat=3):* * f[i,j,k]=0.0* * print "python dot loop " + str(time.time()-t1)* *if __name__=='__main__':* * main()* *############################################################* --047d7b4508a87eae0904ffef52a8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Dear Peter

Below is the code I tried to= check if itertools.product() was faster than normal nested loops...
<= div>
they arent! arent they supposed to be...or am i making a= mistake? any idea?

#############################################= ###############
# -*- coding: utf-8 -*-
import numpy as np
import time
from= itertools import product,repeat
def main():
=C2=A0 =C2=A0 # N - size of grid
=C2=A0 =C2=A0 # nvel - number of velocities
= =C2=A0 =C2=A0 # times - number of times to run the functions
= =C2=A0 =C2=A0 N=3D256
=C2=A0 =C2=A0 times=3D3
=C2=A0 =C2=A0 f=3Dnp.random.rand(N,N,N)
=C2=A0 =C2= =A0
=C2=A0 =C2=A0 # using loops
=C2=A0 = =C2=A0 print "normal nested loop"
=C2=A0 =C2=A0 = python_dot_loop1(f,times,N)

=C2=A0 =C2=A0 print "nested loop using itertools.product()&quo= t;
=C2=A0 =C2=A0 python_dot_loop2(f,times,N)

def python_dot_loop1(f,times,N):
=C2=A0 =C2=A0 for t in range(times):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t1=3Dtime.time()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for i in range(N):
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for j in range(N):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= for k in range(N):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0f[i,j,k] =3D 0.0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print "python dot loop "= ; + str(time.time()-t1)
=C2=A0 =C2=A0
def= python_dot_loop2(f,times,N):
=C2=A0 =C2=A0 rangeN=3Drange= (N)
=C2=A0 =C2=A0 for t in range(times):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0t1=3Dtime.time()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for i,j,k in product(rangeN,repeat=3D3):=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0f[i,j,k]= =3D0.0
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print "pytho= n dot loop " + str(time.time()-t1)


if __name__=3D=3D'_= _main__':
=C2=A0 =C2=A0 main()
######= ######################################################
--047d7b4508a87eae0904ffef52a8--