Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Your code is mostly a div/mod benchmark if you run it in the profiler. For example, the following is ~200 times faster. Apart from the obvious use of machine ints, note that appending to a list is much faster than prepending:

%cython
cimport cython
cdef generate_subgroup_list_by_list_of_subgroups_and_number(subgroups, int number):
    cdef int number_subgroups = len(subgroups)
    subgroups_list = []
    cdef int i, exp2, factor
    for i in range(number_subgroups-1, -1, -1): 
        exp2 = 2<<i
        factor = cython.cdiv(number, exp2)
        number = cython.cmod(number, exp2)
        if factor == 1:
            # subgroups_list = [subgroups[i]] + subgroups_list
            subgroups_list.append(subgroups[i])
    return subgroups_list

def dostuff():
    array = range(1,11)
    for i in range(1,101):
       generate_subgroup_list_by_list_of_subgroups_and_number(array, i)