计算用户按下按钮的频率的函数 [英] Function to calculate how often users are pressing a button
问题描述
我有一个功能.下面是一个原型
I have got a function. Below is a prototype
void onNewButtonPress(int64_t nanoseconds_timestamp, int32_t user_id);
有点杂乱无章.每当具有user_id的用户按下按钮时,都会调用此函数.其中nanoseconds_timestamp参数是自纪元以来的时间(以纳秒为单位)
A bit of destription. This function will be called each time a user with a user_id is pressing the button. Where nanoseconds_timestamp parameter is the time in nanoseconds since the epoch
此功能将需要获取用户按下按钮的速率,基本上是用户每秒按下一次按钮的次数.
This function will need to get the rate of user button presses, basically how many times per second a user pressed a button.
如何计算每个用户的费率,存储并定期更新?是否可以使用上面的函数来完成此操作,否则我将不得不创建另一个函数,该函数将定期调用并计算费率.
How can I calculate the rate for each user, store it and update it periodically? Is it possible to do it using just above function or I will have to create another function which will be called periodically and will calculate the rates.
如何处理在周期结束时开始按下按钮的用户.我需要平均吗?
How do I deal with users which started pressing button close to the end of the period. Do I need to average it?
根据您的经验,我很想听听您的意见.
I will be very interested to hear your opinions based on your experience.
推荐答案
类似这样的东西
具有用于存储用户时间戳记的全局/成员变量
Have a global/member variable that stores the users timestamps
std::map<int32_t, int64_t> lastClick;
然后计算差异并将其求反以得出汇率:
Then calculate the difference and invert it to get the rate:
void onNewButtonPress(int64_t nanoseconds_timestamp, int32_t user_id) {
if (auto last = lastClick[user_id]) {
auto rate = 1000000000 / (nanoseconds_timestamp - last);
// Use the rate for something
lastClick[user_id] = nanoseconds_timestamp;
}
}
这篇关于计算用户按下按钮的频率的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!