如何在不同的定义时间为每个触发器设置数万个任务?

发布于 2024-09-24 11:34:50 字数 494 浏览 2 评论 0原文

我正在构建一个数据可视化系统,可以可视化一段时间内超过 100,000 个数据点(对网站的访问)。然后将时间段(例如1周)转换为模拟时间(模拟中1周=2分钟),并在模拟时间(每次访问的时间)中发生的特定时间对每条数据执行任务一周内实时发生)。与我一起? =p

在其他编程语言(例如Java)中,我只需为每个数据点设置一个计时器。每个计时器完成后,它都会触发一个回调,允许我在我的应用程序中显示该数据点。我是 C++ 新手,不幸的是,带有回调的计时器似乎不是内置的。例如,我在 ActionScript 中执行的另一种方法是使用在特定时间范围后触发的自定义事件。但话又说回来,我认为 C++ 也不支持自定义事件。

简而言之;假设我有 1000 条数据,跨度为 60 秒。每条数据都有其自己与 60 秒周期相关的时间。例如,一个需要在 1 秒触发某件事,另一个需要在 5 秒触发,等等。

我是否以正确的方式处理这个问题,或者是否有更简单的方法来做到这一点?

诗。我使用的是 Mac OS X,而不是 Windows

I'm constructing a data visualisation system that visualises over 100,000 data points (visits to a website) across a time period. The time period (say 1 week) is then converted into simulation time (1 week = 2 minutes in simulation), and a task is performed on each and every piece of data at the specific time it happens in simulation time (the time each visit occurred during the week in real time). With me? =p

In other programming languages (eg. Java) I would simply set a timer for each datapoint. After each timer is complete it triggers a callback that allows me to display that datapoint in my app. I'm new to C++ and unfortunately it seems that timers with callbacks aren't built-in. Another method I would have done in ActionScript, for example, would be using custom events that are triggered after a specific timeframe. But then again I don't think C++ has support for custom events either.

In a nutshell; say I have 1000 pieces of data that span across a 60 second period. Each piece of data has it's own time in relation to that 60 second period. For example, one needs to trigger something at 1 second, another at 5 seconds, etc.

Am I going about this the right way, or is there a much easier way to do this?

Ps. I'm using Mac OS X, not Windows

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-10-01 11:34:50

我不会使用计时器来做到这一点。听起来您的事件太多,而且它们可能彼此距离太近。计时器的性能和准确性可能会很差。

模拟通常是这样完成的:
您只是在进行循环(或迭代)。在每个循环中,您都可以在模拟时间中添加测量的(实时)或恒定的(非实时)量。
然后,您手动检查所有事件并在必要时执行它们。
在您的情况下,将它们按执行时间排序会有所帮助,这样您就不必每次迭代都循环遍历它们。

Tme 测量可以使用 gettimer() c 函数来完成,精度较低,或者有更好的函数实现更高的精度,例如 Windows 上的 QueryPerformanceTimer() - 不知道 Mac 上的等效函数。

I would not use timers to do that. Sounds like you have too many events and they may lie too close to each other. Performance and accuracy may be bad with timers.

a simulation is normally done like that:
You are simly doing loops (or iterations). And on every loop you add an either measured (for real time) or constant (non real time) amount to your simulation time.
Then you manually check all your events and execute them if they have to.
In your case it would help to have them sorted for execution time so you would not have to loop through them all every iteration.

Tme measuring can be done with gettimer() c function for low accuracy or there are better functions for higher accuracy e.g. QueryPerformanceTimer() on windows - dont know the equivalent for Mac.

夜光 2024-10-01 11:34:50

自己做一个“定时器”机制就行了,那就是最好、最快、最灵活的方式。

->创建一个事件数组(链接到每个发生事件的对象)(c++/STL 中的 std::vector)
->按时对数组进行排序(c++/STL 中的 std::sort)
->然后只需在数组上循环并在范围内的时间触发对象操作/方法。

粗略地给出了 C++ 中的内容:

// action upon data + data itself
class Object{
public:
  Object(Data d) : data(d) {

  void Action(){display(data)};

  Data data;
};

// event time + object upon event acts
class Event{
public:
   Event(double t, Object o) time (t), object(o) {};

   // useful for std::sort
   bool operator<(Event e) { return time < e.time; }


  double   time;
  Object   object;


}
//init
std::vector<Event> myEvents;

myEvents.push_back(Event(1.0, Object(data0)));
//...
myEvents.push_back(Event(54.0, Object(data10000)));

// could be removed is push_back() is guaranteed to be in the correct order
std::sort(myEvents.begin(), myEvents.end());

// the way you handle time... period is for some fuzziness/animation ?
const double period = 0.5;
const double endTime = 60;
std::vector<Event>::iterator itLastFirstEvent = myEvents.begin();
for (double currtime = 0.0; currtime < endTime; currtime+=0.1)
{
  for (std::vector<Event>::iterator itEvent = itLastFirstEvent ; itEvent != myEvents.end();++itEvent)
  {
    if (currtime - period < itEvent.time)
      itLastFirstEvent = itEvent; // so that next loop start is optimised
    else if (itEvent.time < currtime + period) 
      itEvent->actiontick(); // action speaks louder than words
    else 
      break; // as it's sorted, won't be any more tick this loop
  }
 }

ps: 关于自定义事件,您可能想要阅读/搜索 C++ 中的委托和函数/方法指针。

Just make a "timer" mechanism yourself, that's the best, fastest and most flexible way.

-> make an array of events (linked to each object event happens to) (std::vector in c++/STL)
-> sort the array on time (std::sort in c++/STL)
-> then just loop on the array and trigger the object action/method upon time inside a range.

Roughly that gives in C++:

// action upon data + data itself
class Object{
public:
  Object(Data d) : data(d) {

  void Action(){display(data)};

  Data data;
};

// event time + object upon event acts
class Event{
public:
   Event(double t, Object o) time (t), object(o) {};

   // useful for std::sort
   bool operator<(Event e) { return time < e.time; }


  double   time;
  Object   object;


}
//init
std::vector<Event> myEvents;

myEvents.push_back(Event(1.0, Object(data0)));
//...
myEvents.push_back(Event(54.0, Object(data10000)));

// could be removed is push_back() is guaranteed to be in the correct order
std::sort(myEvents.begin(), myEvents.end());

// the way you handle time... period is for some fuzziness/animation ?
const double period = 0.5;
const double endTime = 60;
std::vector<Event>::iterator itLastFirstEvent = myEvents.begin();
for (double currtime = 0.0; currtime < endTime; currtime+=0.1)
{
  for (std::vector<Event>::iterator itEvent = itLastFirstEvent ; itEvent != myEvents.end();++itEvent)
  {
    if (currtime - period < itEvent.time)
      itLastFirstEvent = itEvent; // so that next loop start is optimised
    else if (itEvent.time < currtime + period) 
      itEvent->actiontick(); // action speaks louder than words
    else 
      break; // as it's sorted, won't be any more tick this loop
  }
 }

ps: About custom events, you might want to read/search about delegates in c++ and function/method pointers.

伏妖词 2024-10-01 11:34:50

如果您使用本机 C++,则应查看 MSDN 网站上 Windows API 的计时器部分。他们应该准确地告诉您您需要了解的信息。

If you are using native C++, you should look at the Timers section of the Windows API on the MSDN website. They should tell you exactly what you need to know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文