所有系统的循环速度是否相同?

发布于 2024-09-30 18:44:28 字数 56 浏览 1 评论 0原文

C# 中的循环对于所有系统都以相同的速度发生。如果没有,如何控制循环速度以使所有平台上的体验一致?

Does looping in C# occur at the same speed for all systems. If not, how can I control a looping speed to make the experience consistent on all platforms?

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

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

发布评论

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

评论(4

山色无中 2024-10-07 18:44:28

您可以设置循环所需时间的最短时间,如下所示:

for(int i= 0; i < 10; i++)
{
   System.Threading.Thread.Sleep(100);
   ... rest of your code...
}

sleep 调用将至少花费 100 毫秒(您不能说出最大值是多少),因此您的循环将至少需要 1 秒才能运行10 次迭代。

请记住,在用户界面线程上休眠与 Windows 编程的正常方式相反,但这对于您的快速破解可能很有用。

You can set a minimum time for the time taken to go around a loop, like this:

for(int i= 0; i < 10; i++)
{
   System.Threading.Thread.Sleep(100);
   ... rest of your code...
}

The sleep call will take a minimum of 100ms (you cannot say what the maximum will be), so your loop wil take at least 1 second to run 10 iterations.

Bear in mind that it's counter to the normal way of Windows programming to sleep on your user-interface thread, but this might be useful to you for a quick hack.

多情癖 2024-10-07 18:44:28

你永远不能依赖循环的速度。尽管所有现有的编译器都努力使循环尽可能高效,因此它们可能会产生非常相似的结果(给定足够的开发时间),但编译器并不是影响这一点的唯一想法。

即使抛开其他因素,不同的机器也有不同的性能。没有两台机器会产生完全相同的循环速度。事实上,即使在同一台机器上启动该程序两次也会产生略有不同的性能。这取决于其他程序正在运行、CPU 今天的感觉如何以及月亮是否发光。

You can never depend on the speed of a loop. Although all existing compilers strive to make loops as efficient as possible and so they probably produce very similar results (given enough development time), the compilers are not the only think influencing this.

And even leaving everything else aside, different machines have different performance. No two machines will yield the exact same speed for a loop. In fact, even starting the program twice on the same machine will yield slightly different performances. It depends on what other programs are running, how the CPU is feeling today and whether or not the moon is shining.

鲸落 2024-10-07 18:44:28

不,循环在所有系统中发生的情况并不相同。这个问题有很多因素,如果没有代码就无法明显回答。

这是一个简单的循环:

int j;
for(int i = 0; i < 100; i++) {
  j = j + i;
}

这个循环太简单了,它只是一对加载、添加、存储操作,以及一个跳转和一个比较。这将只有几个微指令,而且速度非常快。然而,这些微指令的速度将取决于处理器。如果处理器可以在十亿分之一秒(大约一千兆赫)内完成一个微操作,那么循环将花费大约 6 * 100 微操作(这都是粗略的估计,涉及的因素太多,我只提供近似值)或 6 * 十亿分之一秒,或略小于百万分之一秒。对于整个循环。您几乎无法用大多数操作系统功能来衡量这一点。

我想演示循环的速度。我在上面提到了每秒 10 亿微操作的处理器。现在考虑一个每秒可以执行 40 亿次微操作的处理器。该处理器将比第一个处理器快四倍(大约)。而且我们没有更改代码。

这能回答问题吗?

对于那些想要提及编译器可能会循环展开此内容的人,请为了学习而忽略它。

No, loops do not occur the same in all systems. There are so many factors to this question that it can not be appreciable answered without code.

This is a simple loop:

int j;
for(int i = 0; i < 100; i++) {
  j = j + i;
}

this loop is too simple, it's merely a pair of load, add, store operations, with a jump and a compare. This will be only a few microops and will be really fast. However, the speed of those microops will be dependent on the processor. If the processor can do one microop in 1 billionth of a second (roughly one gigahertz) then the loop will take approximately 6 * 100 microops (this is all rough estimation, there are so many factors involved that I'm only going for approximation) or 6 * 100 billionths of a second, or slightly less than one millionth of a second. For the entire loop. You can barely measure this with most operating system functions.

I wanted to demonstrate the speed of the looping. I referenced above a processor of 1 billion microops per second. Now consider a processor that can do 4 billion microops per second. That processor would be four times faster (roughly) than the first processor. And we didn't change the code.

Does this answer the question?

For those who want to mention that the compiler might loop unroll this, ignore that for the sake of the learning.

一世旳自豪 2024-10-07 18:44:28

控制此问题的一种方法是使用秒表来控制执行逻辑的时间。请参阅此示例代码:

int noofrunspersecond = 30;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / noofrunspersecond;
while (true) {
  ticks2 = Stopwatch.GetTimestamp();
  if (ticks2 >= ticks1 + interval) {
    ticks1 = Stopwatch.GetTimestamp();
    //perform your logic here
  }
  Thread.Sleep(1);
}

这将确保只要系统能够跟上,逻辑就会按照给定的时间间隔执行,因此,如果您尝试每秒执行 100 次,根据执行的逻辑,系统可能无法执行这个逻辑每秒 100 次。在其他情况下,这应该可以正常工作。

这种逻辑有利于获得平滑的动画,例如在不同的系统上不会加速或减慢。

One way of controlling this is by using the Stopwatch to control when you do your logic. See this example code:

int noofrunspersecond = 30;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / noofrunspersecond;
while (true) {
  ticks2 = Stopwatch.GetTimestamp();
  if (ticks2 >= ticks1 + interval) {
    ticks1 = Stopwatch.GetTimestamp();
    //perform your logic here
  }
  Thread.Sleep(1);
}

This will make sure that that the logic is performed at given intervals as long as the system can keep up, so if you try to execute 100 times per second, depending on the logic performed the system might not manage to perform that logic 100 times a second. In other cases this should work just fine.

This kind of logic is good for getting smooth animations that will not speed up or slow down on different systems for example.

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