简体   繁体   English

使用CPU计时器控制线程来执行两个单独的操作是否会导致性能下降?

[英]Will using CPU timer control over threads for two seperate operations cause a decrease in performance?

I'm writing a program in C++ that has to run two operations concurrently (or at least make it appear that it's doing so). 我正在用C ++编写一个程序,该程序必须同时运行两个操作(或至少看起来像是在这样做)。 I've always read that threads are the best solution for this, but I am not too familiar with them and instead opted for timer-based control based on milliseconds to split CPU resources back and forth between operation 1 and operation 2, as I plan to learn about multithreading at a later time. 我一直读到线程是对此的最佳解决方案,但是我不太熟悉线程,而是按计划选择基于毫秒的基于计时器的控制来在操作1和操作2之间来回分配CPU资源。稍后再学习多线程。

My program is running as it should functionally, but it appears to be slow, sluggish, and somewhat choppy. 我的程序运行正常,但运行缓慢,缓慢且有些断断续续。 Could this be due to the way I allocate resources using the timer? 这可能是由于我使用计时器分配资源的方式吗? I'm doing light vision processing within my first operation, and updating a GUI within my second operation. 我在第一个操作中进行光视觉处理,并在第二个操作中更新GUI。

For reference, I'm using an Intel i3-3110M and 4 GB of DDR3 RAM. 作为参考,我使用的是Intel i3-3110M和4 GB的DDR3 RAM。

For one thing, using a timer instead of threads means that only one core on your CPU can be used at a time. 一方面,使用计时器而不是线程意味着一次只能使用一个CPU内核。 With threads, your two tasks could (at least in principle) each get their own core and so they could both run literally simultaneously with each other, giving you a potential 2x speedup. 使用线程,您的两个任务可以(至少原则上)每个都有自己的核心,因此它们实际上可以同时运行,从而使您潜在的2倍加速。

A second problem with using a timer is really a corollary of the first: if the routine called by timer-event-number-1 takes longer than expected to complete, then by necessity the routine called by timer-event-number-2 will not be able to start until after the first routine has returned, and so it will start late. 使用计时器的第二个问题实际上是第一个问题的必然结果:如果timer-event-number-1调用的例程花费的时间比预期完成的时间长,那么必然地,timer-event-number-2调用的例程将不会直到第一个例程返回后才可以启动,因此它将延迟启动。 If taking longer than expected happens commonly (or every time) then the calls to each routine will get increasingly farther "behind schedule" as time goes on. 如果通常(或每次)花费比预期更长的时间,则随着时间的流逝,对每个例程的调用将越来越“落后于计划”。

A third problem with timers is knowing how long to make the delays. 计时器的第三个问题是知道延迟多长时间。 Perhaps with a vision-processing program it's obvious (eg if video is coming in at 20fps, then you might want to set the routines to execute once every 50mS), but for tasks that are logically dependent on each other (eg the second routine is supposed to consume the result produced by the first routine) this is a waste of CPU cycles, since the CPU may end up waiting for no good reason to process data at a specific time that it might just as well have processed sooner and gotten out of the way. 也许对于视觉处理程序来说很明显(例如,如果视频以20fps的速度进入,则您可能希望将例程设置为每50mS执行一次),但是对于逻辑上相互依赖的任务(例如,第二个例程是应该消耗掉第一个例程产生的结果),这浪费了CPU周期,因为CPU可能最终没有理由在特定时间等待处理数据,而这同样可能早就处理完了并从中退出。方式。 In cases like this it is usually better to use some sort of logical triggering mechanism (eg second routine is called by the first routine just before the first routine returns, or in the multithreaded case, have the first routine signal a semaphore or something to wake the second thread up immediately). 在这种情况下,通常最好使用某种逻辑触发机制(例如,在第一个例程返回之前,第二个例程由第一个例程调用,或者在多线程情况下,让第一个例程发出信号量或唤醒的信号)第二个线程立即向上)。 Even in the video-processing case it's usually better to have the first routine triggered by the receipt of a video frame than by a timer, since if the timer goes off late you've wasted valuable processing time, and if it goes off early then there won't be any video frame available yet to process. 即使在视频处理的情况下,通常最好让第一个例程由接收到视频帧来触发,而不是由计时器触发,因为如果计时器关闭得很晚,那您就浪费了宝贵的处理时间,而如果计时器很早就关闭了,尚无任何视频帧可供处理。

As for your particular program, its poor performance might be due to your use of a timer, or it might just be that your routines aren't efficient enough to get their work done in the amount of time you've allotted for them to do it. 对于您的特定程序,其性能不佳可能是由于您使用了计时器,或者仅仅是您的例程效率不足,无法在您分配给他们的时间内完成工作它。 I suggest running your program under a profiler and finding out where it is spending most of its time, and then investigating ways to make that part of the program more efficient (and then test and profile again, and repeat until you're satisfied with the program's performance). 我建议在程序分析器下运行程序,找出程序大部分时间都花在哪里,然后研究使程序的这一部分更有效率的方法(然后再次进行测试和分析,然后重复进行,直到您对程序满意为止)。程序的性能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM