Here I have a simple range function that can use floats as steps or increments. In a time test (using Psyco 1.6, python 2.5, and a basic PC), the normal frange function was able to go up to 1,000,000 from 0, by .3 , and finish that in about 0.5929 seconds. I did the same with the more minimalistic version and got a slightly lower average time; about 0.5310 seconds. So about .1165 times faster than the normal version. Note also that the frange function can go from high to low; ie frange(10,-3,.25). It can also do the basic frange(-3,10,.25) and frange(10,.5)
Float-step range function
# both frange functions written by Luke Endres (fallopiano)
# If your going to use it, please give credit!
def frange(start, end=None, step=None):
'A range function that can accept float increments'
if end == None:
end = start
start = 0.0
if step == None:
step = 1.0
L = []
n = float(start)
if end > start:
while n < end:
L.append(n)
n += step
return L
elif end < start:
while n > end:
L.append(n)
n -= step
return L
# ------------------------------------------------------------- #
# a slightly faster, more minimalistic version of frange...
def frange_mini(start,end=None,step=None):
'A range function that can accept float increments'
if end==None:
end=start
start=0.0
if step==None:step=1.0
L=[]
n=float(start)
if end>start:
while n<end:
L.append(n)
n+=step
return L
elif end<start:
while n>end:
L.append(n)
n-=step
return L
fallopiano 0 Junior Poster in Training
fallopiano 0 Junior Poster in Training
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.