为什么测量循环的执行时间会在执行后给出不同的结果执行? [英] Why measuring the execution time of a loop gives different results execution after execution?

查看:53
本文介绍了为什么测量循环的执行时间会在执行后给出不同的结果执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这很愚蠢,但是我仍然不得不问这个问题,因为它困扰了我好几天了.

I know it's stupid, but I still have to ask this question because it's been bothering me for days.

我使用多种方法进行测试,但结果差异很大.

I use a variety of methods to test, but the results vary widely.

一百万个double-int转换的执行时间与空循环相同

推荐答案

又好又聪明的问题.

秒表的运行结果会有所不同,因为现代计算机和现代操作系统(甚至是现在的智能手机)与旧的Dos不同,都是多任务的.

The Elapsed result of a StopWatch varies because modern computers and modern Operating Systems, even on smartphones now, unlike old Dos, are multitask.

因此,当您进行循环和测量时,具有软件线程的其他一些软件进程以及OS内核本身会同时运行.

So while you do your loop and measure it, some other software processes having software threads run at the same time, as well as the OS kernel itself.

现代CPU能够同时执行多条指令:每个真实内核一条指令.错误的核心或硬件线程使用名为 Hyper-Threading 的上下文保存系统用于英特尔的CPU.例如,英特尔销售的Core i7-4770具有4个内核和8个线程,每个内核两个.

Modern CPU are able to execute several instructions at same time: one per real core. False cores or hardware threads use a context saving system named Hyper-Threading for Intel's CPU. For example Intel sells a Core i7-4770 having 4 cores and 8 threads, two per core.

多线程在进程内部.多重处理用于流程.它们都是多任务的.进程是应用程序启动的过程,它可以具有一些线程.多任务处理是CPU同时执行多条指令的能力.例如,现代Intel上的Windows 10支持多处理和多线程.

Multithreading is inside a processes. Multiprocessing is for processes. They are both multitasking. A process is the application launched and it can have some threads. Multitasking is the capacity of the CPU to execute several instructions at the same time. For example Windows 10 on modern Intels supports both multiprocessing and multithreading.

计算机多任务处理

真正的多任务处理

CPU多任务

如果在Dos单任务中测量循环,则运行后将得到相同的结果.

If you measure a loop in Dos mono-task, you'll get the same result run after run.

但是在多任务操作系统中,您应该进行一些运行,例如十几个半,然后进行平均计算.

But in multitask OS, you should do some runs, say a dozen or a half, and take the avarage.

因此,在测量循环时,已注册到系统事件的所有正在运行的进程都会收到通知,内核也会执行某些操作,Windows资源管理器也会执行其他操作,而其他所有正在运行的应用程序也将执行...因为任务管理器和CPU管理着所有这些正在运行的进程以及I/O中断(例如键盘和鼠标事件和驱动器操作)以及驱动程序(例如图形输出).

Hence while you measure a loop, all running processes having registered to system events receive notifications, the kernel do something, the windows explorer too, and all others running apps too... as the task manager and the CPU manage all these running processes as well as I/O interrupts like keyboard and mouse events and drive operations, and drivers like graphic outpout.

您还可以阅读这些答案,这些答案解释了早期迭代速度较慢的大多数加速情况.

You can also read these answers which explain most of the ramp-up where early iterations are slow.

  • Why does this delay-loop start to run faster after several iterations with no sleep
  • Why can't my CPU maintain peak performance in HPC

全速运行循环也会产生各种结果,这可能是由于中断处理程序是否正在运行而引起的,如果您是在非常短的时间间隔内进行测量,而不是在较长的运行时间内进行平均计算.

Running a loop in full speed does produce various results too, and that can come from an interrupt handler happening to run or not, if you're measuring over very short intervals instead of averaging over a long run time.

这篇关于为什么测量循环的执行时间会在执行后给出不同的结果执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆