On Sep 7, 1:37 pm, "wang frank" <[EMAIL PROTECTED]> wrote: > Hi, > Here is the matlab code: > function [z]=bench1(n) > for i=1:n, > for j=1:1000, > z=log(j); > z1=log(j+1); > z2=log(j+2); > z3=log(j+3); > z4=log(j+4); > z5=log(j+5); > z6=log(j+6); > z7=log(j+7); > z8=log(j+8); > z9=log(j+9); > end > end > z = z9; > > I am not familiar with python, so I just simply try to reproduce the same > code in python. > If you think that my python script is not efficient, could you tell me how > to make it more efficient?
> > > import math > > > def bench1(n): > > > for i in range(n): > > > for j in range(1000): The range(1000) call creates a list of 1000 elements each time it is called. This is expensive. You could try something like the following: def bench1a(n): r = range(1000) for i in range(n): # reuse the list for j in r: ... def bench1b(n): for i in range(n): # don't use a list at all j = 0 while j < 1000: ... I'm no expert on Python optimization either, so I cannot guarantee that both are the best way to write this algorithm. > > > [...] > > > m=j+1 This step also doesn't occur in the Matlab code. Hope this helps, although maybe I'm not the right person to talk about optimization, and I haven't measured my code. -- Roberto Bonvallet
-- http://mail.python.org/mailman/listinfo/python-list