"Alain Ketterlin" <al...@dpt-info.u-strasbg.fr> wrote in message news:87fwyxgvuv....@dpt-info.u-strasbg.fr... > "Bartc" <ba...@freeuk.com> writes:
>> def norm3d(v): >> L = math.sqrt((v[0]*v[0]+v[1]*v[1]+v[2]*v[2])) >> return (v[0]/L,v[1]/L,v[2]/L) >> >> (Strangely, changing those divides to multiplies made it slower.) > > You mean by setting L to 1.0 / math.sqrt(...) and using v[0]*L etc.? Yes. > I think * and / have the same cost on floats, and the added / adds > some cost. I expected no measurable difference, not running Python anyway (I tried it in gcc and using divides increased runtimes by 50%, corresponding to some 1% for Python). I would naturally have written it using multiplies, and was just surprised at a 3-4% slowdown. > But what you observe is probably caused by the overloading of > "*", that needs more type checks. That sounds reasonable. -- Bartc -- http://mail.python.org/mailman/listinfo/python-list