为什么执行时间不同? [英] Why does the execution time vary?

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

问题描述

我这样测量执行时间:

function testFn() {
        return true;
    }
console.time('test');
testFn();
console.timeEnd('test');

每次我重新运行时,我都会得到不同的结果.在其他语言中,我猜这可能是由于操作系统试图优化和调度程序而发生的,但这在浏览器中是如何工作的?

Every time I rerun this I get different result. In other languages I guess this could happen due to the OS trying to optimize and schedule programs, but how this works in the browser?

推荐答案

你的浏览器也是操作系统调度的应用,所以影响操作系统的东西也会影响浏览器.

Your browser is also an application that is scheduled by the operating system, so the things that affect the OS also affect the browser.

可能有很多事情会影响时间,包括您的操作系统内核正在执行的内部事务,例如调度进程、刷新文件系统缓存等.除非您使用的是实时操作系统.不保证代码执行的持续时间.

There could be tons of things affecting the time including internal stuff that your OS kernel is doing like Scheduling processes, flushing file system caches and so on. Unless you are using a real-time operating system. The time duration of execution of your code is not guaranteed.

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

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