python函数的分析

python函数的分析,python,profiling,python-3.x,Python,Profiling,Python 3.x,你知道我怎样才能使这个功能更省时吗 def c(n): word = 32 #l = [] c = 0 for i in range(0, 2**word): #print(str(bin(i)))#.count('1') if str(bin(i)).count('1') == n: c = c + 1 print(c) if i == 2**28:

你知道我怎样才能使这个功能更省时吗

def c(n):
    word = 32
    #l = []
    c = 0
    for i in range(0, 2**word):
        #print(str(bin(i)))#.count('1')
        if str(bin(i)).count('1') == n:
            c = c + 1 
            print(c)

        if i == 2**28:
            print('6 %')
        if i == 2**29:
            print('12 %')
        if i == 2**30:
            print('25 %')
        if i == 2**31:
            print('50 %')
        if i == 2**32:
            print('100 %')
    return c



135274023 function calls in 742.161 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1  391.662  391.662  742.161  742.161 <pyshell#3>:1(c)
        1    0.000    0.000  742.161  742.161 <string>:1(<module>)
     4816    0.014    0.000    0.014    0.000 rpc.py:149(debug)
      688    0.010    0.000    3.162    0.005 rpc.py:208(remotecall)
      688    0.017    0.000    0.107    0.000 rpc.py:218(asynccall)
      688    0.019    0.000    3.043    0.004 rpc.py:238(asyncreturn)
      688    0.002    0.000    0.002    0.000 rpc.py:244(decoderesponse)
      688    0.007    0.000    3.018    0.004 rpc.py:279(getresponse)
      688    0.006    0.000    0.010    0.000 rpc.py:287(_proxify)
      688    0.025    0.000    3.000    0.004 rpc.py:295(_getresponse)
      688    0.002    0.000    0.002    0.000 rpc.py:317(newseq)
      688    0.023    0.000    0.062    0.000 rpc.py:321(putmessage)
      688    0.007    0.000    0.011    0.000 rpc.py:546(__getattr__)
      688    0.002    0.000    0.002    0.000 rpc.py:587(__init__)
      688    0.004    0.000    3.166    0.005 rpc.py:592(__call__)
     1376    0.008    0.000    0.011    0.000 threading.py:1012(current_thread)
      688    0.004    0.000    0.019    0.000 threading.py:172(Condition)
      688    0.009    0.000    0.015    0.000 threading.py:177(__init__)
      688    0.019    0.000    2.962    0.004 threading.py:226(wait)
      688    0.002    0.000    0.002    0.000 threading.py:45(__init__)
      688    0.002    0.000    0.002    0.000 threading.py:50(_note)
      688    0.004    0.000    0.004    0.000 threading.py:88(RLock)
      688    0.004    0.000    0.004    0.000 {built-in method allocate_lock}
 67620326  162.442    0.000  162.442    0.000 {built-in method bin}
      688    0.007    0.000    0.007    0.000 {built-in method dumps}
        1    0.000    0.000  742.161  742.161 {built-in method exec}
     1376    0.003    0.000    0.003    0.000 {built-in method get_ident}
     1376    0.004    0.000    0.004    0.000 {built-in method isinstance}
     2064    0.005    0.000    0.005    0.000 {built-in method len}
      688    0.002    0.000    0.002    0.000 {built-in method pack}
      344    0.009    0.000    3.187    0.009 {built-in method print}
      688    0.008    0.000    0.008    0.000 {built-in method select}
      688    0.003    0.000    0.003    0.000 {method '_acquire_restore' of '_thread.RLock' objects}
      688    0.002    0.000    0.002    0.000 {method '_is_owned' of '_thread.RLock' objects}
      688    0.002    0.000    0.002    0.000 {method '_release_save' of '_thread.RLock' objects}
      688    0.003    0.000    0.003    0.000 {method 'acquire' of '_thread.RLock' objects}
     1376    2.929    0.002    2.929    0.002 {method 'acquire' of '_thread.lock' objects}
      688    0.002    0.000    0.002    0.000 {method 'append' of 'list' objects}
 67620325  184.869    0.000  184.869    0.000 {method 'count' of 'str' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
      688    0.002    0.000    0.002    0.000 {method 'get' of 'dict' objects}
      688    0.002    0.000    0.002    0.000 {method 'release' of '_thread.RLock' objects}
      688    0.015    0.000    0.015    0.000 {method 'send' of '_socket.socket' objects}
def c(n):
字=32
#l=[]
c=0
对于范围内的i(0,2**word):
#打印(str(bin(i))#.计数('1'))
如果str(bin(i)).count('1')==n:
c=c+1
印刷品(c)
如果i==2**28:
打印('6%”)
如果i==2**29:
打印('12%”)
如果i==2**30:
打印(“25%”)
如果i==2**31:
打印(“50%”)
如果i==2**32:
打印(“100%”)
返回c
742.161秒内调用135274023个函数
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1391.662391.662742.161742.161:1(c)
1    0.000    0.000  742.161  742.161 :1()
4816 0.014 0.000 0.014 0.000 rpc.py:149(调试)
688 0.010 0.000 3.162 0.005 rpc.py:208(远程调用)
688 0.017 0.000 0.107 0.000 rpc.py:218(异步调用)
688 0.019 0.000 3.043 0.004 rpc.py:238(异步返回)
688 0.002 0.000 0.002 0.000 rpc.py:244(解码响应)
688 0.007 0.000 3.018 0.004 rpc.py:279(getresponse)
688 0.006 0.000 0.010 0.000 rpc.py:287(_proxify)
688 0.025 0.000 3.000 0.004 rpc.py:295(_getresponse)
688 0.002 0.000 0.002 0.000 rpc.py:317(newseq)
688 0.023 0.000 0.062 0.000 rpc.py:321(putmessage)
688 0.007 0.000 0.011 0.000 rpc.py:546
688 0.002 0.000 0.002 0.000 rpc.py:587(初始)
688 0.004 0.000 3.166 0.005 rpc.py:592(调用)
1376 0.008 0.000 0.011 0.000螺纹。py:1012(当前螺纹)
688 0.004 0.000 0.019 0.000螺纹。py:172(条件)
688 0.009 0.000 0.015 0.000螺纹。py:177(初始)
688 0.019 0.000 2.962 0.004螺纹。py:226(等待)
688 0.002 0.000 0.002 0.000螺纹。py:45(初始)
688 0.002 0.000 0.002 0.000螺纹。py:50(注)
688 0.004 0.000 0.004 0.000螺纹。py:88(锁紧)
688 0.004 0.000 0.004 0.000{内置方法分配锁}
67620326 162.442 0.000 162.442 0.000{内置方法bin}
688 0.007 0.000 0.007 0.000{内置方法转储}
1 0.000 0.000 742.161 742.161{内置方法exec}
1376 0.003 0.000 0.003 0.000{内置方法获取标识}
1376 0.004 0.000 0.004 0.000{内置方法isinstance}
2064 0.005 0.000 0.005 0.000{内置方法len}
688 0.002 0.000 0.002 0.000{内置方法包}
344 0.009 0.000 3.187 0.009{内置方法打印}
688 0.008 0.000 0.008 0.000{内置方法选择}
688 0.003 0.000 0.003 0.000{方法{u thread.RLock'对象的}
688 0.002 0.000 0.002 0.000{method''u是''u thread.RLock'对象的''u所拥有的'}
688 0.002 0.000 0.002 0.000{thread.RLock'objects}的方法{u release\u save}
688 0.003 0.000 0.003 0.000{method'acquire'of'\u thread.RLock'objects}
1376 2.929 0.002 2.929 0.002{方法'acquire'的'u thread.lock'对象}
688 0.002 0.000 0.002 0.000{“列表”对象的“附加”方法}
67620325 184.869 0.000 184.869 0.000{“str”对象的方法“计数”}
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
688 0.002 0.000 0.002 0.000{“dict”对象的“get”方法}
688 0.002 0.000 0.002 0.000{方法'release'的'\u thread.RLock'对象}
688 0.015 0.000 0.015 0.000{_socket.socket'对象的'send'方法}

我试图实现的是计算从0到2**32的数字中有多少个具有二进制表示形式的
n
number of
1

您正在计算有多少32位数字具有给定数量的
1
s。此数字是32位选择位,可通过以下公式计算:

from math import factorial
print factorial(32) // (factorial(bits) * factorial(32-bits))

也许这是有趣的:哦,天哪!那是小菜一碟!你需要克里皮的时候他在哪里?“看起来您正在尝试计算二项式系数…”