Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate exponentially increasing range in Python

Tags:

python

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 10. This is how I'm doing it so far but it looks a bit hacky. Suggestions for improvements without introducing non-standard libraries?

numbers_size = 100
increment = 100
numbers_range = 1000000000
while numbers_size < numbers_range:
    t = time.time()
    test( numbers_size )
    taken_t = time.time() - t
    print numbers_size, test, taken_t

    increment = 10 ** (len(str(numbers_size))-1)
    numbers_size += increment
like image 393
Martlark Avatar asked Jul 12 '12 01:07

Martlark


1 Answers

If you consider numpy as one of the standards ;), you may use numpy.logspace since that is what it is supposed to do.... (note: 100=10^2, 1000000000=10^9)

for n in numpy.logspace(2,9,num=9-2, endpoint=False):
    test(n)

example 2 (note: 100=10^2, 1000000000=10^9, want to go at a step 10x, it is 9-2+1 points...):

In[14]: np.logspace(2,9,num=9-2+1,base=10,dtype='int')
Out[14]: 
array([       100,       1000,      10000,     100000,    1000000,
         10000000,  100000000, 1000000000])

example 3:

In[10]: np.logspace(2,9,dtype='int')
Out[10]: 
array([       100,        138,        193,        268,        372,
              517,        719,       1000,       1389,       1930,
             2682,       3727,       5179,       7196,      10000,
            13894,      19306,      26826,      37275,      51794,
            71968,     100000,     138949,     193069,     268269,
           372759,     517947,     719685,    1000000,    1389495,
          1930697,    2682695,    3727593,    5179474,    7196856,
         10000000,   13894954,   19306977,   26826957,   37275937,
         51794746,   71968567,  100000000,  138949549,  193069772,
        268269579,  372759372,  517947467,  719685673, 1000000000])

on your case, we use endpoint=False since you want not to include the endpoint... (e.g. np.logspace(2,9,num=9-2, endpoint=False) )

like image 119
ntg Avatar answered Sep 18 '22 17:09

ntg