- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 boost.python 的新手,并且创建了一个简单的函数来将列表从 python 传递到 C++ vector :
void SetXValues(boost::python::list xl){
int n = len((xl));
xvals.resize(n);
for(unsigned int i=0; i<n; i++){
xvals[i] = boost::python::extract<double>((xl)[i]);
}
}
xvals 是一个 C++ STL vector 。这个函数有效,我可以将 python 列表加载到 C++ 中,但它看起来非常慢。
我做的一个小速度测试是用 C++ 和纯 Python 编写分箱算法。结果表明,如果算上从 Python 传递数据的时间,C++ 方法的速度仅快 5 倍,但当然单独的分箱算法要快得多 (74 倍)。
那么有什么办法可以改进上面的功能,使其更加高效呢?
最佳答案
在性能关键部分,如您所描述的。我通常避免将数据存储在 python list
中首先。 list
是在其中存储任意对象的正确数据类型。特别是列表中的每个对象都可以有不同的类型。但是您已经知道这将是一个“ double 列表”。
我建议改为使用 std::vector<double>
已经在 python 。为此,您将导出 std::vector<double>
作为class_
, 让我们称之为 VectorOfDOubles
,使用 boost python。你可以这样做,在 python 中你不会看到 list
之间的区别。和 VectorOfDoubles
,主要区别在于您将其构建为 xl = VectorOfDoubles(55)
而不是 xl = []
.您需要做一些工作才能使 index-acees 正常工作,例如xl[5] = 4.5,但为此存在 boost 索引套件,我推荐版本 2,以帮助您。
另一种选择是使用 numpy ndarrays 而不是 list
.存在 boost numpy 库,可帮助您使用来自 boost python 的 numpy ndarrays。
但是正如您所说,您是 boost python 的新手,boost numpy 和 boost indexing suite 都可能有点难。也许您首先想解决创建自己的 std::vector<double>
子类的问题,说 VectorOfDOubles
, 并定义 double get(int i)
和 void set(int i, double val)
然后将这两个函数(连同 size(),构造函数)导出到 python。这需要对您的 Python 代码进行一些更改,但对于初学者来说更容易。
关于c++ - 将列表从 python 传递到 C++ vector 时如何加速 Boost::Python::Extract,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929196/
警告在块AccessRights〜Groups〜Navigator中[mini-css-extract-plugin] 之间的顺序冲突: css ../node_modules/css-loader?
解决练习问题“对于数据库中的每个月/年组合,交易表的销售日期列中有多少个不同的日期?”我发现一个有效的查询,它显然结合了年份和月份 SELECT DISTINCT (extract(year from
我有一个名为 Student 的 MySQL 表,它有一个名为 entry_date 的列,类型为 date。我想选择以下形式的 entry_date: November, 2014 即它将显示 en
我已经使用 iMacros 很长时间了,我有一个代码可以提取文本并使用它来创建我需要的特定 URL。 事情是这样的: #I can extract the XPath text with this.
我将一个 session 从 Fiddler 导出到 saz 文件。此 session 仅包含 jpg 文件,我想知道 - 如何快速轻松地从 saz 中提取 jpg 文件? 谢谢! 最佳答案 提取 J
应用 FFT 后,我得到了具有多个频率段的频谱。如何使用倒谱方法从该频谱中获取基频? 我做了很多研究,尝试了很多代码,并在 stackoverflow 上问了三遍(这很有帮助),我非常确定倒谱方法是在
我正在寻找一种解压缩或反编译微软代理的方法。 例如梅林代理 我想提取动画/图像。到目前为止我发现的最好方法是。用粉红色背景录制我的屏幕。并分割视频......但这不是一个很好的做法...... 有小费
我只想从youtube链接中提取MP3格式的音频,但是如果不从命令行使用youtube-dl选项调用--extract-audio,就无法弄清楚该如何做。在YoutubeDL类中是否有一种类似于her
我有一个 Azure 管道,应该构建一个项目并将 jar 复制到 Artifactory。这是应该安装节点的 yml: - task: NodeTool@0 inputs: version
例如,如果您有一个如下所示的关联数组: $array = array('first-value' => 'Hello'); 然后你要提取它: extract($array); 由于变量名称中不能使用连
我有大型 pdf 文件(法语的 100 页)描述了我的事件部门的一套规则。 我正在寻找一种服务,允许我一次查询一个 pdf(或我从中提取的文本)以自动获取信息。 (示例:x 的最大授权长度是多少?)
我是 RDF 初学者,我想从 HTML 中提取 RDF我正在使用 GRDDL,但它不太适合我,我每次都会收到安全异常:(您能向我推荐另一个工具吗? 感谢您的帮助。 最佳答案 我通常使用说唱歌手,你可以
我很好奇,PHP 的函数是如何实现的extract有用吗?我想做一个稍微修改的版本。我希望我的函数在从蛇形符号到驼峰式的数组键中提取时生成变量名,例如: 现在 extract 这样做: $array
如果我使用 PHP 的 extract() 函数从数组中导入变量,同名变量会被覆盖吗?我问的原因是因为我正在尝试初始化所有变量。 感谢您的宝贵时间。 最佳答案 默认情况下它将覆盖。 http://ph
我有大型 pdf 文件(法语的 100 页)描述了我的事件部门的一套规则。 我正在寻找一种服务,允许我一次查询一个 pdf(或我从中提取的文本)以自动获取信息。 (示例:x 的最大授权长度是多少?)
我正在尝试在 Python 中运行大量模拟,因此我尝试使用多处理来实现它。 import numpy as np import matplotlib.pyplot as plt import mult
尝试从主机名-rt45_34_we_35 中提取主机名-rt45。我正在使用/(.)_?./g。这似乎不起作用。我已经查看了正则表达式文档。想用?会使它变得贪婪并在第一个下划线处停止。我错过了什么?
我目前正在从事一个数据挖掘项目。我必须阅读 C# 源代码,并且必须找到连接 SQL 语句的位置。我真正想要的是获取连接字符串变量名称的名称。 示例: stat = "SELECT * FROM CUS
我正在订单表上运行查询,以计算每个用户在 6 个月前一个月内的任何一天发出的请求总数(例如:2013 年 8 月)。 这工作正常:- SELECT userid,firstname,surname,s
我对 PHP 很陌生,并且到处都看到不建议使用提取函数。我正在从 mysql 表中获取数据来填充网站的一部分。因此我不知道该表可以有多少行。 所以我使用 extract 函数,它为每行提供一个数组数组
我是一名优秀的程序员,十分优秀!