The problem is not with len(), merely with the conversion from an int to an Integer required in comparing len(results)<cota.
The following code is faster than either of your versions. Note that '3r' is an int, whereas '3' is an Integer. tri=pen=hex=dt=dp=dh=1r results=[] cota = 3r while len(results)<cota: if tri==pen==hex: results.append(tri) if tri<=pen and tri<=hex: dt+=1r tri+=dt elif pen<=hex: dp+=3r pen+=dp else: dh+=4r hex+=dh print results On Tue, Nov 23, 2010 at 3:00 AM, Pablo Angulo <pablo.ang...@uam.es> wrote: > Hello: > A colleague was working in code for a certain projecteuler problem, > and found that using "len" of a list has a severe penalty on the sage > notebook, which doesn't happen on either the sage or the python console. > The two versions of the code below differ only in a call to the function > len. In console, the penalty in performance is around 10%, but in the > Sage notebook, it takes 3 times longer: the call to "len" costs doble > time as the rest of the loop. Anyway, we've been getting weird results > from time and its family lately (like CPU time longer than Wall time: is > that ok?). This is tested on versions 4.6 and 4.4, on different > architectures. > > Regards > > Version 1 > > tri=pen=hex=dt=dp=dh=1 > results=[] > cota = 3 > while len(results)<cota: > if tri==pen==hex: > results.append(tri) > if tri<=pen and tri<=hex: > dt+=1 > tri+=dt > elif pen<=hex: > dp+=3; > pen+=dp > else: > dh+=4; > hex+=dh > print results > > > Version 2 > > cota = 3 > tri=pen=hex=dt=dp=dh=1 > results=[] > n=0 > while n<cota: > if tri==pen==hex: > results.append(tri) > n+=1 > if tri<=pen and tri<=hex: > dt+=1 > tri+=dt > elif pen<=hex: > dp+=3; > pen+=dp > else: > dh+=4; > hex+=dh > print results > > -- > To post to this group, send an email to sage-devel@googlegroups.com > To unsubscribe from this group, send an email to > sage-devel+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/sage-devel > URL: http://www.sagemath.org > -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org