我已经在python中编写了一些代码,它运行得很好.但是现在我正在扩大我正在分析的问题的大小,并且python非常慢.python代码的缓慢部分是
for i in range(0,H,1):
x1 = i - length
x2 = i + length
for j in range(0,W,1):
#print i, ',', j # check the limits
y1 = j - length
y2 = j + length
IntRed[i,j] = np.mean(RawRed[x1:x2,y1:y2])
Run Code Online (Sandbox Code Playgroud)
当H和W等于1024时,该功能大约需要5分钟才能执行.我编写了一个简单的c ++程序/函数,它执行相同的计算,并且在不到一秒的时间内以相同的数据大小进行操作.
double summ = 0;
double total_num = 0;
double tmp_num = 0 ;
int avesize = 2;
for( i = 0+avesize; i <X-avesize ;i++)
for(j = 0+avesize;j<Y-avesize;j++)
{
// loop through sub region of the matrix
// if …Run Code Online (Sandbox Code Playgroud) 我正在使用numpy loadtxt函数来读取大量数据.数据似乎四舍五入.例如:文本文件中的数字是-3.79000000000005E + 01,但是numpy以-37.9的形式读取数字.我在loadtxt调用中将dypte设置为np.float64.反正是否保持原始数据文件的精度?
我试图弄清楚如何使用SWIG包装一个返回2d向量的c ++函数到python.I有文件functions.h
#include <vector>
std::vector< std::vector<double> > array_mean(std::vector< std::vector<double> > array)
{
std::vector< std::vector<double> > mean_array( rows, std::vector<double>(cols));
....
return mean_array;
}
Run Code Online (Sandbox Code Playgroud)
在接口文件functions.i我有
%module functions
%{
#include "functions.h"
%}
%include "std_vector.i"
namespace std {
%template(VecVecdouble) vector< vector<double> >;
}
%include "functions.h"
Run Code Online (Sandbox Code Playgroud)
然后我表演
swig -c ++ -python functions.i
g ++ -O2 -fPIC -c functions_wrap.cxx -I/usr/include/python2.4 -I/usr/lib/python2.4
编译器会发出一堆错误.
functions_wrap.cxx: In function 'bool swig::check(PyObject*) [with Type = double]':
functions_wrap.cxx:3763: instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = double]'
functions_wrap.cxx:3820: instantiated from 'static int …Run Code Online (Sandbox Code Playgroud) 我的第一个问题是;通常我可以通过几次搜索找到答案,但这次不行。
我想编写一个在启动时运行的脚本,以检查外部显示器是否连接到笔记本电脑。
我想用 python 编写脚本。
我使用的是 32 位 Ubuntu 10.04。我四处搜寻但找不到任何有用的东西。有什么建议吗?谢谢