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
最满意答案
不幸的是,没有办法通过当前的API一次获得多个线程主题。 但是,您可以采取一些措施来改善性能:
使用API的分页功能一次获取有限数量的线程。
并行获取批量消息,而不是一次尝试获取所有消息或一次获取所有消息。 为自己做实验,但5开始时就是一个很好的数字。
如果这是一个浏览器实现,请考虑拨打后端,而不是从浏览器拨打电话。 这样,客户端每页只进行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:
Use the API's paging feature to fetch limited amounts of threads at once.
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.
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类别(Get Gmail categories)[2022-12-03]
可以轻松搜索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 ... -
如何连接到Gmail收件箱(How to connect to a Gmail inbox)[2023-11-02]
非常感谢zdim帮助排除故障。 首先,zdim指出我有错误的错误变量。 $ _应该是$! 这显示错误消息“网络无法访问”,但我能够成功地使用pint和telnet到'imap.gmail.com'。 在这里找到解决方案Perl IO :: Socket :: SSL:connect:网络无法访问 。 将Net :: IMAP :: Client模块中的use语句更改为以下内容: use IO::Socket::SSL 'inet4'; 在此之后,建立了连接,但登录失败 Login failed: [ALE ... -
获取Gmail中的主题ID(Getting the thread id in Gmail)[2023-11-19]
你可以尝试: 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通过“/”分隔的 ...