CPU使用率,当我运行一个for-in循环:OS-x应用程序 [英] CPU usage shooting up when I am running a for-in loop: OS-x app

查看:232
本文介绍了CPU使用率,当我运行一个for-in循环:OS-x应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的循环运行大约15k次太多的CPU被使用。它总是在90 -100%的范围内。我在循环中做的是一系列核心数据实体更新/创建。为什么会这样呢?我在后台运行我的进程,我已经将线程优先级指定为最小值太!

When my loop runs for about 15k times too much CPU is being used. It's in the range of 90 -100 % always. What I am doing inside the loop is a series of Core data entity updating/creation. Why does that happen? I am running my process in background and I have specified the thread priority to minimum value too!

推荐答案

Paul R是正确的 - 系统将尽可能快地实现循环中的任务,在这种情况下,它是在托管对象上下文中操作Core Data对象,而不是提交到磁盘,因此对它可以执行的速度的唯一限制是CPU时间。

Paul R is correct - the system will achieve the tasks in the loop as quickly as possible given the resources it has. In this case, it is manipulating Core Data objects in the Managed Object Context and not committing to disk so the only limitation to how fast it can execute is CPU Time.

这导致UI线程问题?理论上,如果UI线程想要CPU时间,它应该采取它。 sleep()会解决这个问题,但是因为你的后台任务需要更长时间才能完成。

Is this causing a UI thread issue? Theoretically, if the UI thread wants CPU time, it should take it. The sleep() would fix that, but is inideal because your background task would take longer to complete.

如果是这样,有办法打破工作可以比15k项循环更简单的分块?

If so, is there a way to break out the work so it can be more simply chunked than a 15k item loop?

循环中的某些东西是否使用mutex / @synchronise?这可能会导致延迟,它会减少它的CPU时间,当请求,可以翻译为laggy UI。

Is something in the loop running using mutex / @synchronise? This can cause a delay before it will ramp down it's CPU-time when requested which can translate to laggy UI.

这篇关于CPU使用率,当我运行一个for-in循环:OS-x应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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