Python 能推断或暗示麻木中的当地人类型吗?

Python 能推断或暗示麻木中的当地人类型吗?,python,numba,Python,Numba,我正在用numba来代替我们的一些cython代码 在这里: 查看inspect_类型的输出,似乎所有的局部变量都是python对象,而不是类型化的C值。例如,在inspect_类型输出中,我们有: # $0.12 = getitem(index=$const0.11, value=arr) :: pyobject # last_out = $0.12 :: pyobject last_out = arr[0] 这让我觉得last_out被视为pyobject,而不是arr中的i

我正在用numba来代替我们的一些cython代码

在这里:

查看inspect_类型的输出,似乎所有的局部变量都是python对象,而不是类型化的C值。例如,在inspect_类型输出中,我们有:

#   $0.12 = getitem(index=$const0.11, value=arr)  :: pyobject
#   last_out = $0.12  :: pyobject

last_out = arr[0]
这让我觉得last_out被视为pyobject,而不是arr中的int64

有没有办法优化上述功能,使其以与等效cython实现类似的速度运行

下面是inspect_类型的完整输出

unique_in_sorted (array(int64, 1d, C),)
--------------------------------------------------------------------------------
# File: /home/user1/py/fast_ops.py
# --- LINE 142 --- 
# label 0
#   del $0.1
#   del $0.2
#   del $0.4
#   del $const0.6
#   del $const0.9
#   del $0.7
#   del $const0.11
#   del $0.12
#   del $const0.13

@jit

# --- LINE 143 --- 

def unique_in_sorted(arr):

    # --- LINE 144 --- 
    #   arr = arg(0, name=arr)  :: pyobject
    #   $0.1 = global(np: <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'>)  :: pyobject
    #   $0.2 = getattr(attr=empty_like, value=$0.1)  :: pyobject
    #   $0.4 = call $0.2(arr, kws=[], args=[Var(arr, /home/user1/py/fast_ops.py (144))], func=$0.2, vararg=None)  :: pyobject
    #   result = $0.4  :: pyobject

    result = np.empty_like(arr)

    # --- LINE 145 --- 
    #   $const0.6 = const(int, 0)  :: pyobject
    #   $0.7 = getitem(index=$const0.6, value=arr)  :: pyobject
    #   $const0.9 = const(int, 0)  :: pyobject
    #   result[$const0.9] = $0.7  :: pyobject

    result[0] = arr[0]

    # --- LINE 146 --- 
    #   $const0.11 = const(int, 0)  :: pyobject
    #   $0.12 = getitem(index=$const0.11, value=arr)  :: pyobject
    #   last_out = $0.12  :: pyobject

    last_out = arr[0]

    # --- LINE 147 --- 
    #   $const0.13 = const(int, 1)  :: pyobject
    #   count_out = $const0.13  :: pyobject
    #   jump 45
    # label 45

    count_out = 1

    # --- LINE 148 --- 
    #   jump 48
    # label 48
    #   $48.1 = global(range: <built-in function range>)  :: pyobject
    #   $48.2 = global(len: <built-in function len>)  :: pyobject
    #   $48.4 = call $48.2(arr, kws=[], args=[Var(arr, /home/user1/py/fast_ops.py (144))], func=$48.2, vararg=None)  :: pyobject
    #   del $48.2
    #   $48.5 = call $48.1($48.4, kws=[], args=[Var($48.4, /home/user1/py/fast_ops.py (148))], func=$48.1, vararg=None)  :: pyobject
    #   del $48.4
    #   del $48.1
    #   $48.6 = getiter(value=$48.5)  :: pyobject
    #   del $48.5
    #   $phi64.1 = $48.6  :: pyobject
    #   del $48.6
    #   jump 64
    # label 64
    #   $64.2 = iternext(value=$phi64.1)  :: pyobject
    #   $64.3 = pair_first(value=$64.2)  :: pyobject
    #   $64.4 = pair_second(value=$64.2)  :: pyobject
    #   del $64.2
    #   $phi153.2 = $phi64.1  :: pyobject
    #   del $phi153.2
    #   $phi153.1 = $64.3  :: pyobject
    #   del $phi153.1
    #   $phi67.1 = $64.3  :: pyobject
    #   del $64.3
    #   branch $64.4, 67, 153
    # label 67
    #   del $64.4
    #   i = $phi67.1  :: pyobject
    #   del $phi67.1
    #   del i
    #   del $67.4
    # label 155
    #   del a
    #   del $119.3
    #   jump 64

    for i in range(len(arr)):

        # --- LINE 149 --- 
        #   $67.4 = getitem(index=i, value=arr)  :: pyobject
        #   a = $67.4  :: pyobject

        a = arr[i]

        # --- LINE 150 --- 
        #   $67.7 = a < last_out  :: pyobject
        #   branch $67.7, 92, 119
        # label 92
        #   del result
        #   del count_out
        #   del arr
        #   del $phi64.1
        #   del $67.7
        #   del $const92.2
        #   del last_out
        #   del a
        #   del $92.3
        #   del $92.6
        #   del $92.1

        if a<last_out:

            # --- LINE 151 --- 
            #   $92.1 = global(Exception: <type 'exceptions.Exception'>)  :: pyobject
            #   $const92.2 = const(str, Input not sorted: {} .. {})  :: pyobject
            #   $92.3 = getattr(attr=format, value=$const92.2)  :: pyobject
            #   $92.6 = call $92.3(last_out, a, kws=[], args=[Var(last_out, /home/user1/py/fast_ops.py (146)), Var(a, /home/user1/py/fast_ops.py (149))], func=$92.3, vararg=None)  :: pyobject
            #   $92.7 = call $92.1($92.6, kws=[], args=[Var($92.6, /home/user1/py/fast_ops.py (151))], func=$92.1, vararg=None)  :: pyobject
            #   raise $92.7
            # label 119
            #   del $67.7

            raise Exception("Input not sorted: {} .. {}".format(last_out, a))

        # --- LINE 152 --- 
        #   $119.3 = last_out != a  :: pyobject
        #   branch $119.3, 131, 155
        # label 131
        #   del $119.3
        #   del a

        if last_out!=a:

            # --- LINE 153 --- 
            #   result[count_out] = a  :: pyobject

            result[count_out] = a

            # --- LINE 154 --- 
            #   last_out = a  :: pyobject
            #   jump 155
            # label 153
            #   del last_out
            #   del arr
            #   del $phi67.1
            #   del $phi64.1
            #   del $64.4
            #   jump 154
            # label 154
            #   del result
            #   del count_out
            #   del $154.2
            #   del $154.4

            last_out = a

        # --- LINE 155 --- 



    # --- LINE 156 --- 
    #   $154.2 = getattr(attr=resize, value=result)  :: pyobject
    #   $154.4 = call $154.2(count_out, kws=[], args=[Var(count_out, /home/user1/py/fast_ops.py (147))], func=$154.2, vararg=None)  :: pyobject
    #   $154.5 = cast(value=$154.4)  :: pyobject
    #   return $154.5

    return result.resize(count_out)
unique_in_排序(数组(int64,1d,C),)
--------------------------------------------------------------------------------
#文件:/home/user1/py/fast\u ops.py
#---第142行--
#标签0
#德尔0.1美元
#德尔0.2美元
#德尔0.4美元
#del$const0.6
#德尔康斯特0.9美元
#德尔0.7美元
#del$const0.11
#德尔0.12美元
#德尔$0.13
@准时制
#---第143行--
def unique_in_排序(arr):
#---第144行--
#arr=arg(0,name=arr)::pyobject
#$0.1=全局(np:)::pyobject
#$0.2=getattr(attr=empty_-like,value=$0.1)::pyobject
#$0.4=调用$0.2(arr,kws=[],args=[Var(arr,/home/user1/py/fast_ops.py(144))],func=$0.2,vararg=None)::pyobject
#结果=$0.4::pyobject
结果=np.空类(arr)
#---第145行--
#$const0.6=const(int,0)::pyobject
#$0.7=getitem(索引=$const0.6,值=arr)::pyobject
#$const0.9=const(int,0)::pyobject
#结果[$const0.9]=$0.7::pyobject
结果[0]=arr[0]
#---第146行--
#$const0.11=const(int,0)::pyobject
#$0.12=getitem(index=$const0.11,value=arr)::pyobject
#last_out=$0.12::pyobject
last_out=arr[0]
#---第147行--
#$const0.13=const(int,1)::pyobject
#count_out=$const0.13::pyobject
#跳45
#标签45
计数=1
#---第148行----
#跳48
#标签48
#$48.1=全局(范围:)::pyobject
#$48.2=全局(len:)::pyobject
#$48.4=调用$48.2(arr,kws=[],args=[Var(arr,/home/user1/py/fast_ops.py(144)),func=$48.2,vararg=None)::pyobject
#德尔48.2美元
#$48.5=调用$48.1($48.4,kws=[],args=[Var($48.4,/home/user1/py/fast_ops.py(148))],func=$48.1,vararg=None::pyobject
#德尔48.4美元
#德尔48.1美元
#$48.6=getiter(值=48.5)::pyobject
#德尔48.5美元
#$phi64.1=$48.6::pyobject
#德尔48.6美元
#跳跃64
#标签64
#$64.2=iternext(值=$phi64.1)::pyobject
#$64.3=pair_first(值=$64.2)::pyobject
#$64.4=成对秒(值=$64.2)::pyobject
#德尔$64.2
#$phi153.2=$phi64.1::pyobject
#德尔$153.2
#$phi153.1=$64.3::pyobject
#德尔$153.1
#$phi67.1=$64.3::pyobject
#德尔64.3美元
#分行64.4、67、153美元
#标签67
#德尔64.4美元
#i=$phi67.1::pyobject
#del$phi67.1
#德利
#德尔$67.4
#标签155
#德拉
#德尔119.3美元
#跳跃64
对于范围内的i(len(arr)):
#---第149行--
#$67.4=getitem(index=i,value=arr)::pyobject
#a=$67.4::pyobject
a=arr[i]
#---第150行--
#$67.7=a如果a,则异常处理会将其置于对象模式

修正如下:

@jit
def unique_in_sorted(arr):
    result = np.empty_like(arr)
    count_out =_unique_in_sorted(arr, result)
    result.resize(count_out)
    return result

@jit(nopython=True)
def _unique_in_sorted(arr, result):
    result[0] = arr[0]
    last_out = arr[0]
    count_out = 1
    for i in range(len(arr)):
        a = arr[i]
        # if a<last_out:
        #     raise Exception("Input not sorted: {} .. {}".format(last_out, a))
        if last_out!=a:
            result[count_out] = a
            last_out = a
    return count_out
@jit
def unique_in_排序(arr):
结果=np.空类(arr)
count\u out=\u unique\u in\u排序(arr,result)
结果.调整大小(计数)
返回结果
@jit(nopython=True)
def_unique_in_排序(arr,结果):
结果[0]=arr[0]
last_out=arr[0]
计数=1
对于范围内的i(len(arr)):
a=arr[i]

#如果您应该能够引发
异常,但异常的参数必须是常量
unique_in_sorted (array(int64, 1d, C),)
--------------------------------------------------------------------------------
# File: /home/user1/py/fast_ops.py
# --- LINE 142 --- 
# label 0
#   del $0.1
#   del $0.2
#   del $0.4
#   del $const0.6
#   del $const0.9
#   del $0.7
#   del $const0.11
#   del $0.12
#   del $const0.13

@jit

# --- LINE 143 --- 

def unique_in_sorted(arr):

    # --- LINE 144 --- 
    #   arr = arg(0, name=arr)  :: pyobject
    #   $0.1 = global(np: <module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'>)  :: pyobject
    #   $0.2 = getattr(attr=empty_like, value=$0.1)  :: pyobject
    #   $0.4 = call $0.2(arr, kws=[], args=[Var(arr, /home/user1/py/fast_ops.py (144))], func=$0.2, vararg=None)  :: pyobject
    #   result = $0.4  :: pyobject

    result = np.empty_like(arr)

    # --- LINE 145 --- 
    #   $const0.6 = const(int, 0)  :: pyobject
    #   $0.7 = getitem(index=$const0.6, value=arr)  :: pyobject
    #   $const0.9 = const(int, 0)  :: pyobject
    #   result[$const0.9] = $0.7  :: pyobject

    result[0] = arr[0]

    # --- LINE 146 --- 
    #   $const0.11 = const(int, 0)  :: pyobject
    #   $0.12 = getitem(index=$const0.11, value=arr)  :: pyobject
    #   last_out = $0.12  :: pyobject

    last_out = arr[0]

    # --- LINE 147 --- 
    #   $const0.13 = const(int, 1)  :: pyobject
    #   count_out = $const0.13  :: pyobject
    #   jump 45
    # label 45

    count_out = 1

    # --- LINE 148 --- 
    #   jump 48
    # label 48
    #   $48.1 = global(range: <built-in function range>)  :: pyobject
    #   $48.2 = global(len: <built-in function len>)  :: pyobject
    #   $48.4 = call $48.2(arr, kws=[], args=[Var(arr, /home/user1/py/fast_ops.py (144))], func=$48.2, vararg=None)  :: pyobject
    #   del $48.2
    #   $48.5 = call $48.1($48.4, kws=[], args=[Var($48.4, /home/user1/py/fast_ops.py (148))], func=$48.1, vararg=None)  :: pyobject
    #   del $48.4
    #   del $48.1
    #   $48.6 = getiter(value=$48.5)  :: pyobject
    #   del $48.5
    #   $phi64.1 = $48.6  :: pyobject
    #   del $48.6
    #   jump 64
    # label 64
    #   $64.2 = iternext(value=$phi64.1)  :: pyobject
    #   $64.3 = pair_first(value=$64.2)  :: pyobject
    #   $64.4 = pair_second(value=$64.2)  :: pyobject
    #   del $64.2
    #   $phi153.2 = $phi64.1  :: pyobject
    #   del $phi153.2
    #   $phi153.1 = $64.3  :: pyobject
    #   del $phi153.1
    #   $phi67.1 = $64.3  :: pyobject
    #   del $64.3
    #   branch $64.4, 67, 153
    # label 67
    #   del $64.4
    #   i = $phi67.1  :: pyobject
    #   del $phi67.1
    #   del i
    #   del $67.4
    # label 155
    #   del a
    #   del $119.3
    #   jump 64

    for i in range(len(arr)):

        # --- LINE 149 --- 
        #   $67.4 = getitem(index=i, value=arr)  :: pyobject
        #   a = $67.4  :: pyobject

        a = arr[i]

        # --- LINE 150 --- 
        #   $67.7 = a < last_out  :: pyobject
        #   branch $67.7, 92, 119
        # label 92
        #   del result
        #   del count_out
        #   del arr
        #   del $phi64.1
        #   del $67.7
        #   del $const92.2
        #   del last_out
        #   del a
        #   del $92.3
        #   del $92.6
        #   del $92.1

        if a<last_out:

            # --- LINE 151 --- 
            #   $92.1 = global(Exception: <type 'exceptions.Exception'>)  :: pyobject
            #   $const92.2 = const(str, Input not sorted: {} .. {})  :: pyobject
            #   $92.3 = getattr(attr=format, value=$const92.2)  :: pyobject
            #   $92.6 = call $92.3(last_out, a, kws=[], args=[Var(last_out, /home/user1/py/fast_ops.py (146)), Var(a, /home/user1/py/fast_ops.py (149))], func=$92.3, vararg=None)  :: pyobject
            #   $92.7 = call $92.1($92.6, kws=[], args=[Var($92.6, /home/user1/py/fast_ops.py (151))], func=$92.1, vararg=None)  :: pyobject
            #   raise $92.7
            # label 119
            #   del $67.7

            raise Exception("Input not sorted: {} .. {}".format(last_out, a))

        # --- LINE 152 --- 
        #   $119.3 = last_out != a  :: pyobject
        #   branch $119.3, 131, 155
        # label 131
        #   del $119.3
        #   del a

        if last_out!=a:

            # --- LINE 153 --- 
            #   result[count_out] = a  :: pyobject

            result[count_out] = a

            # --- LINE 154 --- 
            #   last_out = a  :: pyobject
            #   jump 155
            # label 153
            #   del last_out
            #   del arr
            #   del $phi67.1
            #   del $phi64.1
            #   del $64.4
            #   jump 154
            # label 154
            #   del result
            #   del count_out
            #   del $154.2
            #   del $154.4

            last_out = a

        # --- LINE 155 --- 



    # --- LINE 156 --- 
    #   $154.2 = getattr(attr=resize, value=result)  :: pyobject
    #   $154.4 = call $154.2(count_out, kws=[], args=[Var(count_out, /home/user1/py/fast_ops.py (147))], func=$154.2, vararg=None)  :: pyobject
    #   $154.5 = cast(value=$154.4)  :: pyobject
    #   return $154.5

    return result.resize(count_out)
@jit
def unique_in_sorted(arr):
    result = np.empty_like(arr)
    count_out =_unique_in_sorted(arr, result)
    result.resize(count_out)
    return result

@jit(nopython=True)
def _unique_in_sorted(arr, result):
    result[0] = arr[0]
    last_out = arr[0]
    count_out = 1
    for i in range(len(arr)):
        a = arr[i]
        # if a<last_out:
        #     raise Exception("Input not sorted: {} .. {}".format(last_out, a))
        if last_out!=a:
            result[count_out] = a
            last_out = a
    return count_out