*在C中具有特殊含义,就像在C中一样吗?我在Python Cookbook中看到了这样的函数:
def get(self, *a, **kw)
Run Code Online (Sandbox Code Playgroud)
您能否向我解释或指出我能在哪里找到答案(Google将*解释为外卡字符,因此我找不到满意的答案).
非常感谢你.
我正在学习如何在Python中嵌入Rust函数,如果我的输入是ints而不是list ,一切正常.
如果我的lib.rs文件是:
#[no_mangle]
pub extern fn my_func(x: i32, y: i32) -> i32 {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用如下:
In [1]: from ctypes import cdll
In [2]: lib = cdll.LoadLibrary("/home/user/RustStuff/embed/target/release/libembed.so")
In [3]: lib.my_func(5,6)
Out[3]: 11
Run Code Online (Sandbox Code Playgroud)
但是,如果我将我lib.rs改为:
#[no_mangle]
pub extern fn my_func(my_vec: Vec<i32>) -> i32 {
let mut my_sum = 0;
for i in my_vec {
my_sum += i;
}
return my_sum;
}
Run Code Online (Sandbox Code Playgroud)
我不能再在Python中使用它(这编译得很好):
In [1]: from ctypes import cdll
In [2]: lib = …Run Code Online (Sandbox Code Playgroud) 我的代码的瓶颈目前是使用ctypes从Python列表转换为C数组,如本问题所述.
一个小实验表明,与其他Python指令相比,它确实非常慢:
import timeit
setup="from array import array; import ctypes; t = [i for i in range(1000000)];"
print(timeit.timeit(stmt='(ctypes.c_uint32 * len(t))(*t)',setup=setup,number=10))
print(timeit.timeit(stmt='array("I",t)',setup=setup,number=10))
print(timeit.timeit(stmt='set(t)',setup=setup,number=10))
Run Code Online (Sandbox Code Playgroud)
得到:
1.790962941000089
0.0911122129996329
0.3200237319997541
Run Code Online (Sandbox Code Playgroud)
我用CPython 3.4.2获得了这些结果.我和CPython 2.7.9和Pypy 2.4.0的时间相似.
我尝试运行上面的代码perf,注释timeit指令一次只运行一个.我得到这些结果:
ctypes的
Performance counter stats for 'python3 perf.py':
1807,891637 task-clock (msec) # 1,000 CPUs utilized
8 context-switches # 0,004 K/sec
0 cpu-migrations # 0,000 K/sec
59 523 page-faults # 0,033 M/sec
5 755 704 178 cycles # 3,184 GHz
13 552 506 138 instructions …Run Code Online (Sandbox Code Playgroud) 我想将一个char指针数组传递给C函数.
我指的是http://docs.python.org/library/ctypes.html#arrays
我写下面的代码.
from ctypes import *
names = c_char_p * 4
# A 3 times for loop will be written here.
# The last array will assign to a null pointer.
# So that C function knows where is the end of the array.
names[0] = c_char_p('hello')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
TypeError:'_ typeype.PyCArrayType'对象不支持项目分配
知道如何解决这个问题吗?我想与之交流
c_function(const char** array_of_string);
Run Code Online (Sandbox Code Playgroud) 我试图在Python中创建一个2D列表.我找到了两种可能性.
def cArray(size):
c = [[0. for i in range(size)] for j in range(size)]
return c
def npArray(size):
np = numpy.zeros((size,size))
return np
Run Code Online (Sandbox Code Playgroud)
现在这两个函数都给出了正确的答案.这里的问题在于性能.我使用timeit运行了这两个,这是我的结果:
list size is 5000
number of times run is 5
cArray average time: 3.73241295815
npArray average time: 0.210782241821
Run Code Online (Sandbox Code Playgroud)
很明显,我想避免使用第一个解决方案,特别是因为这将适用于高达100k的大小.但是,我也不想使用太多的依赖项.有没有办法让我有效地创建一个2D数组,没有numpy?它不需要完全达到速度,只要它不慢17倍.
这是一个两部分的问题。为了提供一些背景知识,我有一个 C 代码,如下所示:
int c_func(const char* dir, float a, float b, float c, float d )
{
printf("%s\n", dir);
printf("%f\n",a);
printf("%f\n",b);
printf("%f\n",c);
printf("%f\n",d);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个简单的函数,它接受一个字符串和 4 个浮点数作为参数并将它们打印出来我正在尝试测试我的 phython/C 接口。我的python代码如下:
calling_function = ctypes.CDLL("/home/ruven/Documents/Sonar/C interface/Interface.so")
calling_function.c_func("hello",1, 2, 3, 4])
Run Code Online (Sandbox Code Playgroud)
现在既然这样有效,我想传递一个包含 4 个浮点数的列表,而不是传递 4 个单独的浮点数。我在网上尝试了不同的代码来编辑我的 C 函数,以便它接受一个列表作为它的参数之一,但我似乎无法弄清楚如何这样做,因为我是一个新程序员,而且我对 C 没有经验。
问题 1:如何编写 C 函数以接受列表作为其参数?
问题 2:这个包含四个浮点数的列表实际上来自我的 Python 代码中的列表列表。在对 C 函数进行编码后,我是否可以使用称为testfv2[0,:] C 函数输入的numpy 数组?testfv2[0,:]是一个 1x4 的维度列表,testfv2是一个 117x4 的维度列表。现在,我想一次进入 C 函数 1 行,这就是为什么我想使用testfv2[0,:].