简体   繁体   English

每次运行python脚本花费的时间不同?

[英]Different time taken by python script every time it is runned?

I am working on a Opencv based Python project. 我正在研究一个基于Opencv的Python项目。 I am working on program development which takes less time to execute. 我正在从事程序开发,这需要更少的时间来执行。 For that i have tested my small program print hello world on python to test the time taken to run the program. 为此,我已经在python上测试了我的小程序print hello world,以测试运行该程序所花费的时间。 I had run many time and every time it run it gives me a different run time. 我已经运行了很多次,每次运行都会给我不同的运行时间。

Can you explain me why a simple program is taking different time to execute? 您能解释一下为什么一个简单的程序需要花费不同的时间来执行吗?

I need my program to be independent of system processes ? 我需要我的程序独立于系统进程吗?

Python gets different amounts of system resources depending upon what else the CPU is doing at the time. Python获得不同数量的系统资源,具体取决于当时的CPU在做什么。 If you're playing Skyrim with the highest graphics levels at the time, then your script will run slower than if no other programs were open. 如果您当时正在玩具有最高图形级别的Skyrim,那么您的脚本将比没有其他程序打开时运行得慢。 But even if your task bar is empty, there may be invisible background processes confounding things. 但是,即使您的任务栏为空,也可能会有看不见的后台进程使事情变得混乱。

If you're not already using it, consider using timeit . 如果尚未使用它,请考虑使用timeit It performs multiple runs of your program in order to smooth out bad runs caused by a busy OS. 它可以执行程序的多次运行,以消除由繁忙的OS引起的不良运行。

If you absolutely insist on requiring your program to run in the same amount of time every time, you'll need to use an OS that doesn't support multitasking. 如果您绝对要求每次都在相同的时间内运行程序,则需要使用不支持多任务的OS。 For example, DOS. 例如,DOS。

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

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