关于CPU占用率和程序执行时间的问题

简单小小丫 2014-05-05 03:45:38
最近在优化一个算法以降低算法的执行时间,算法用opencv写的,调用了很多系统自带的函数,在C代码上能优化的空间比较小。算法预计应用在android手机上,现在的手机大部分都是双核甚至4核的。算法用单线程写时,CPU占用率只有20%多,耗时1000毫秒左右;改写成双个线程,CPU占用率提高到50%左右,耗时下降到650毫秒左右。以上都是在双核手机上测试。在优化C代码空间很小的情况下,产生这样的疑惑:我是不是想办法提高CPU的利用率,就能降低算法执行时间?现在即使改写成两个线程,但好像两个线程都在同一个核上跑,每次用top命令查看CPU占用率,该程序在0和1的CPU上来回跳跃。
归纳一下疑惑:1.CPU占用率和程序执行时间是否成反比?
2.能把两个线程绑定到不同的核上跑吗?若能,绑定到不同核上之后,两个线程之间对资源的竞争减小了,算法执行时间是否会降低?
望大牛们给答疑解惑,小女子感激不尽。
...全文
627 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobo_包子 2014-05-06
  • 打赏
  • 举报
回复
并行计算的限制是 1.算法的可并行度 2.通信开销 从你描述来看,你的算法有一定的并行度,而且是在一个cpu上,所以也就没有啥限制,唯一所要关注的是算法的可并行度了。 1.CPU占用率和程序执行时间是否成反比? 从某种意义上可以这么说,比如计算量是一定的,CPU占用越高说明CPU在不停计算,那总是可以很快计算完的。前提是不能总让CPU做无用功。 2.能把两个线程绑定到不同的核上跑吗? 可以设置线程和进程的亲和性。
enic 2014-05-06
  • 打赏
  • 举报
回复
什么应用啊? 貌似可以用一些并发算法库,不然自能自己埋头优化算法了,别人也帮不了
赵4老师 2014-05-06
  • 打赏
  • 举报
回复
SetThreadIdealProcessor The SetThreadIdealProcessor function is used to specify a preferred processor for a thread. The system schedules threads on their preferred processors whenever possible. DWORD SetThreadIdealProcessor( HANDLE hThread, // handle to the thread DWORD dwIdealProcessor // ideal processor number ); Parameters hThread Handle to the thread whose preferred processor is to be set. The handle must have the THREAD_SET_INFORMATION access right associated with it. dwIdealProcessor Specifies the number of the preferred processor for the thread. A value of MAXIMUM_PROCESSORS tells the system that the thread has no preferred processor. Return Values If the function succeeds, the return value is the previous preferred processor or MAXIMUM_PROCESSORS if the thread does not have a preferred processor. If the function fails, the return value is – 1. To get extended error information, call GetLastError. Remarks You can use the GetSystemInfo function to determine the number of processors on the computer. You can also use the GetProcessAffinityMask function to check the processors on which the thread is allowed to run. Note that GetProcessAffinityMask returns a bit mask whereas SetThreadIdealProcessor uses an integer value to represent the processor. QuickInfo Windows NT: Requires version 4.0 or later. Windows: Unsupported. Windows CE: Unsupported. Header: Declared in winbase.h. Import Library: Use kernel32.lib. See Also Processes and Threads Overview, Process and Thread Functions, GetProcessAffinityMask,GetSystemInfo, SetThreadAffinityMask
简单小小丫 2014-05-06
  • 打赏
  • 举报
回复
为啥没人回答呢?

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧