How to generate exponentially increasing range in Python

前端 未结 8 1087
醉梦人生
醉梦人生 2020-12-15 23:05

I want to test the performance of some code using an exponentially increasing value. So that as an extra digit is added to the numbers_size the increment is multiplied by 1

相关标签:
8条回答
  • 2020-12-15 23:46

    The simplest thing to do is to use a linear sequence of exponents:

    for e in range(1, 90):
        i = int(10**(e/10.0))
        test(i)
    

    You can abstract the sequence into its own generator:

    def exponent_range(max, nsteps):
        max_e = math.log10(max)
        for e in xrange(1, nsteps+1):
            yield int(10**(e*max_e/nsteps))
    
    for i in exponent_range(10**9, nsteps=100):
        test(i)
    
    0 讨论(0)
  • 2020-12-15 23:50

    To produce the same numbers as your code:

    numbers_sizes = (i*10**exp for exp in range(2, 9) for i in range(1, 10))
    for n in numbers_sizes:
        test(n)
    
    0 讨论(0)
提交回复
热议问题