简体   繁体   English

Cython 快速列表 / numpy 访问

[英]Cython fast list / numpy accessing

I have a Python function I'm trying to speed up, which just takes a line of tshark output, eg:我有一个 Python function 我正在尝试加快速度,只需要一行 tshark output,例如:

'1\t0.000000000\tTCP\t100.0.1.190,111.0.0.2\t35291\t55321\t\t\t56\t20\t··········S·\t36\n'

and assigns the data to variables like so:并将数据分配给如下变量:

            arr = line.strip('\n').split("\t")

            sip = arr[3].split(',')[0]
            dip = arr[3].split(',')[1]

            s_flag = 1 if 'S' in arr[10] else '0'
            a_flag = 1 if 'A' in arr[10] else '0'
            f_flag = 1 if 'F' in arr[10] else '0'
            r_flag = 1 if 'R' in arr[10] else '0'
            p_flag = 1 if 'P' in arr[10] else '0'
            u_flag = 1 if 'U' in arr[10] else '0'
            e_flag = 1 if 'E' in arr[10] else '0'
            c_flag = 1 if 'C' in arr[10] else '0'

What is a way to speed this up using Cython?有什么方法可以使用 Cython 加快速度? I'm thinking of casting the results of line.strip('\n').split("\t") to a numpy array since I heard it's faster than Python lists in Cython?我正在考虑将 line.strip('\n').split("\t") 的结果转换为 numpy 数组,因为我听说它比 Cython 中的 Python 列表更快? How else can I speed this up?我还能如何加快速度? eg:例如:

import numpy
cimport numpy

arr = np.array(line.strip('\n').split("\t"))

Will this work?这行得通吗? Thank you in advance!先感谢您!

Since you're dealing with lists of strings, numpy, and likely even cython won't help you much.由于您正在处理字符串列表,numpy,甚至 cython 都可能对您没有多大帮助。 The transformations you are looking for are so trivial, you can just clean up your python code a bit and move on:您正在寻找的转换是如此微不足道,您可以稍微清理一下 python 代码并继续:

FLAGS = np.array(list('SAFRPUEC'))

items = line.strip('\n').split("\t")
sip, dip = items[3].split(',')
flags = dict(zip(FLAGS, np.isin(FLAGS, list(items[10]))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM