首页 \ 问答 \ R数值向量列表 - >带有Rcpp的C ++ 2d数组(R List of numeric vectors -> C++ 2d array with Rcpp)

R数值向量列表 - >带有Rcpp的C ++ 2d数组(R List of numeric vectors -> C++ 2d array with Rcpp)

我主要使用R,但最终想使用Rcpp与一些C ++函数接口,这些函数接收并返回2d数字数组。 因此,为了开始使用C ++和Rcpp,我想我只是做了一个小函数,将我的可变长度数字向量的R列表转换为C ++等价物并再次返回。

require(inline)
require(Rcpp)

test1 = cxxfunction(signature(x='List'), body = 
'
  using namespace std;
  List xlist(x);
  int xlen = xlist.size();
  vector< vector<int> > xx;
  for(int i=0; i<xlen; i++) {
    vector<int> test = as<vector<int> > (xlist[i]);
    xx.push_back(test);
  }
  return(wrap(xx));
'
, plugin='Rcpp')

这有点像我期望的那样:

> test1(list(1:2, 4:6))
[[1]]
[1] 1 2

[[2]]
[1] 4 5 6

不可否认,我只是通过非常详尽的文档的一部分,但有一个更好的(即更像Rcpp)方式来做R - > C ++转换而不是for循环? 我想可能不是,因为文档提到(至少使用内置方法)“提供的灵活性较低,目前处理R对象转换为原始类型”,但我想检查,因为我非常喜欢这方面的新手。


I mainly use R, but eventually would like to use Rcpp to interface with some C++ functions that take in and return 2d numeric arrays. So to start out playing around with C++ and Rcpp, I thought I'd just make a little function that converts my R list of variable-length numeric vectors to the C++ equivalent and back again.

require(inline)
require(Rcpp)

test1 = cxxfunction(signature(x='List'), body = 
'
  using namespace std;
  List xlist(x);
  int xlen = xlist.size();
  vector< vector<int> > xx;
  for(int i=0; i<xlen; i++) {
    vector<int> test = as<vector<int> > (xlist[i]);
    xx.push_back(test);
  }
  return(wrap(xx));
'
, plugin='Rcpp')

This works like I expect:

> test1(list(1:2, 4:6))
[[1]]
[1] 1 2

[[2]]
[1] 4 5 6

Admittedly I am only part way through the very thorough documentation, but is there a nicer (i.e. more Rcpp-like) way to do the R -> C++ conversion than with the for loop? I am thinking possibly not, since the documentation mentions that (at least with the built-in methods) as "offers less flexibility and currently handles conversion of R objects into primitive types", but I wanted to check because I'm very much a novice in this area.


原文:https://stackoverflow.com/questions/8159872
更新时间:2022-09-06 22:09

最满意答案

不幸的是,没有办法通过当前的API一次获得多个线程主题。 但是,您可以采取一些措施来改善性能:

  1. 使用API​​的分页功能一次获取有限数量的线程。

  2. 并行获取批量消息,而不是一次尝试获取所有消息或一次获取所有消息。 为自己做实验,但5开始时就是一个很好的数字。

  3. 如果这是一个浏览器实现,请考虑拨打后端,而不是从浏览器拨打电话。 这样,客户端每页只进行1次调用,它允许您为服务器添加缓存和预加载机制,从而改善客户体验。 这里潜在的缺点是可扩展性; 随着您获得更多客户端,您将需要比胖客户端方法更多的处理能力。

作为#2的示例,您最初可以获取5,然后让每个函数的回调触发下一个调用,因此总是有5个并发获取:

var CONCURRENCY_LIMIT = 5;

function getThread(threadId, done) {
  threadRequest.id = e.id;
  gmail.api.users.threads.get(threadRequest).execute(function(thread) {
    showThread();
    done();
  });
}

gmail.api.users.threads.list({userId:myUserId}).execute(function(threads) { 
  function fetchNextThread() {
    var nextThread = threads.shift();
    nextThread.id && getThread(nextThread.id, fetchNextThread);
  }

  for (var i = 0; i < CONCURRENCY_LIMIT; i++) {
    fetchNextThread();
  }
});

Unfortunately, there isn't a way to get more than one thread subject at a time through the current API. However, there are a few things you might do to improve the performance:

  1. Use the API's paging feature to fetch limited amounts of threads at once.

  2. Fetch batches of messages in parallel rather than attempting to fetch all at once or one at a time. Experiment for yourself, but 5 would be a good number to start with.

  3. If this is a browser implementation, consider making a call to your back-end instead of making the calls from the browser. This way the client only makes 1 call per page, and it allows you to potentially add caching and pre-loading mechanisms to your server that will improve the customer experience. The potential downside here is scalability; as you get more clients, you'll need considerably more processing power than an fat-client approach.

As an example of #2, you could fetch 5 initially, and then have the callback for each function fire the next call so there are always 5 fetching concurrently:

var CONCURRENCY_LIMIT = 5;

function getThread(threadId, done) {
  threadRequest.id = e.id;
  gmail.api.users.threads.get(threadRequest).execute(function(thread) {
    showThread();
    done();
  });
}

gmail.api.users.threads.list({userId:myUserId}).execute(function(threads) { 
  function fetchNextThread() {
    var nextThread = threads.shift();
    nextThread.id && getThread(nextThread.id, fetchNextThread);
  }

  for (var i = 0; i < CONCURRENCY_LIMIT; i++) {
    fetchNextThread();
  }
});

相关问答

更多
  • 您应该避免使用含糊不清的变量名称,“电子邮件”和“电子邮件”在谈论一方的线程和另一方的索引整数时是非常糟糕的选择...... 您的问题主要来自两个变量之间的混淆,您使用电子邮件而不是电子邮件* S *并且似乎忘记了您的值是一个线程数组,因此需要编制索引。 这是你的工作代码,只有一个字母差异;-)和几个括号...... function readBotsEmail() { var emails = GmailApp.search("label:" + incoming); Logger.log("T ...
  • 你可以使用gm密钥的gmail搜索语法 gmail.inbox.search(gm: "subject: -'Urgent'") 您还可以在此处找到可与gm使用的语法列表。 you can use the gmail search syntax with gm key gmail.inbox.search(gm: "subject: -'Urgent'") You can also find the list of syntax you can use with gm here.
  • 不。 Gmail实施不是线程安全的。 因此,如果您作为多线程应用程序运行,则您发出请求的每个线程都必须具有自己的httplib2.Http()实例。 资源链接: gmail api服务 Nope. Gmail implementation is not thread-safe. Therefore, if you are running as a multi-threaded application, each thread that you are making requests from must ha ...
  • 其他邮件客户端使用一些邮件标题来帮助线程化 - 不确定gmail是否支持它们。 第一个是标准的RFC-822“ In-Reply-To: ”,第二个是非标准的(从Usenet窃取)“ References: ,,... ”。 There are a couple of mail headers that other mail clients use to help threading - not sure if gmail supports ...
  • 我相信Starred标签仅表示可以在Starred邮箱中找到邮件。 Gmail为标签使用不同的IMAP邮箱。 MailSystem与Gmail的增强型IMAP命令尚不完全兼容(例如,不支持邮件的threadID)。 您可以通过向IMAP4Client类发出命令方法来检查IMAP是否实际存储了星形类型 ,该方法带来了完整的消息(通过BODY IMAP命令),但不幸的是,您将不得不解析并找出每个参数的含义。 Max说的其他解决方案是使用X-GM-RAW增强搜索命令来检查所需星号的主题邮件,在这种情况下,您必须使 ...
  • public string GetMail(){ GmailService service = (GmailService)HttpContext.Current.Session["service"]; Message messageFeed = service.Users.Messages.List("me").Execute().Messages.First(); UsersResource.MessagesResource.GetRequest getReq = new UsersResource.M ...
  • 不幸的是,没有办法通过当前的API一次获得多个线程主题。 但是,您可以采取一些措施来改善性能: 使用API的分页功能一次获取有限数量的线程。 并行获取批量消息,而不是一次尝试获取所有消息或一次获取所有消息。 为自己做实验,但5开始时就是一个很好的数字。 如果这是一个浏览器实现,请考虑拨打后端,而不是从浏览器拨打电话。 这样,客户端每页只进行1次调用,它允许您为服务器添加缓存和预加载机制,从而改善客户体验。 这里潜在的缺点是可扩展性; 随着您获得更多客户端,您将需要比胖客户端方法更多的处理能力。 作为#2的示 ...
  • 可以轻松搜索Gmail类别。 这是一个查找每个促销邮件的小代码。 结果是一个线程数组,你可以为每个线程添加Label,这样你的旧脚本就会很高兴;-) var threads = GmailApp.search('category:promotions');// check the category Gmail added to the thread 文档在这里 Gmail categories can easily be searched. Here is a small code that look ...
  • 非常感谢zdim帮助排除故障。 首先,zdim指出我有错误的错误变量。 $ _应该是$! 这显示错误消息“网络无法访问”,但我能够成功地使用pint和telnet到'imap.gmail.com'。 在这里找到解决方案Perl IO :: Socket :: SSL:connect:网络无法访问 。 将Net :: IMAP :: Client模块中的use语句更改为以下内容: use IO::Socket::SSL 'inet4'; 在此之后,建立了连接,但登录失败 Login failed: [ALE ...
  • 你可以尝试: var matched = window.location.hash.match(/[A-Za-z0-9]+$/); if (matched) { // Found alphanumeric string at end of hash } 并且您可以使用matched[0]获取值。 window.location.hash应该只抓取“#inbox / 13ddda647539dcca”部分。 然后正则表达式匹配字符串末尾的任何字母数字字符。 因此,“收件箱”与线程ID通过“/”分隔的 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)