By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,316 Members | 2,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,316 IT Pros & Developers. It's quick & easy.

how to decrease the cpu usage

P: n/a
Hello everyone.

i want to know how to decrease the cpu usage in my program.

i have to update quickly many data on gui,

so i tried to decrease the cpu usage on gui but i couldn't solve the problem.

i found the code with probloem why the cpu usage increased by debug..

Rectangle invalidateRect = new Rectangle(_detailVisibleRect.Left,
_detailVisibleRect.Top + item.Y - _vScrollBar.Value, _detailRect.Width,
item.Height);
this.Invalidate(invalidateRect);

if i don't call this code, then the cpu usage is ok,

but after i call this code, the cpu usage of my problem become between 10 ~
15%.

could you tell me how to solve this kind of problem if you have to update
many data
on GUI as fast as possible.

^^....help me
Mar 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
lightdoll wrote:
Hello everyone.

i want to know how to decrease the cpu usage in my program.

i have to update quickly many data on gui,

so i tried to decrease the cpu usage on gui but i couldn't solve the
problem.

i found the code with probloem why the cpu usage increased by debug..

Rectangle invalidateRect = new Rectangle(_detailVisibleRect.Left,
_detailVisibleRect.Top + item.Y - _vScrollBar.Value,
_detailRect.Width, item.Height);
this.Invalidate(invalidateRect);

if i don't call this code, then the cpu usage is ok,

but after i call this code, the cpu usage of my problem become
between 10 ~ 15%.

Well, that's what causes the screen to redraw, so I would expect it to use
some CPU. Maybe it doesn't need as much as it currently uses though.
>
could you tell me how to solve this kind of problem if you have to
update many data
on GUI as fast as possible.
First thing that will help is to recognize that "as fast as possible" isn't
needed. Just "as fast as is perceptible to the user". So, for example, you
could limit animation to 30/second or data updates to 5/second and the user
won't notice the delay.

Next, although your code does a great job of defining the update region
correctly, if your paint code doesn't use the update region, there's no
benefit.

You might simply try this:

Add a timer with a period of 150ms.
Change the invalidate code to simply set a boolean dirty flag.
In the timer callback, if the dirty flag is set, invalidate the whole window
and clear the flag. (If you have multiple threads, you should use
Interlocked.Exchange to clear the boolean flag and give you the old value)

This will probably decrease the number of repaints dramatically and
therefore save you a lot of CPU.
>
^^....help me

Mar 7 '08 #2

P: n/a
Jon
'First thing that will help is to recognize that "as fast as possible" isn't needed. Just "as fast
as is perceptible to the user".'

Ben: that can be pretty difficult to define, since you don't always know how powerful the end user's
computer is.
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:OF**************@TK2MSFTNGP05.phx.gbl...
lightdoll wrote:
Hello everyone.

i want to know how to decrease the cpu usage in my program.

i have to update quickly many data on gui,

so i tried to decrease the cpu usage on gui but i couldn't solve the
problem.

i found the code with probloem why the cpu usage increased by debug..

Rectangle invalidateRect = new Rectangle(_detailVisibleRect.Left,
_detailVisibleRect.Top + item.Y - _vScrollBar.Value,
_detailRect.Width, item.Height);
this.Invalidate(invalidateRect);

if i don't call this code, then the cpu usage is ok,

but after i call this code, the cpu usage of my problem become
between 10 ~ 15%.

Well, that's what causes the screen to redraw, so I would expect it to use
some CPU. Maybe it doesn't need as much as it currently uses though.
>
could you tell me how to solve this kind of problem if you have to
update many data
on GUI as fast as possible.
First thing that will help is to recognize that "as fast as possible" isn't
needed. Just "as fast as is perceptible to the user". So, for example, you
could limit animation to 30/second or data updates to 5/second and the user
won't notice the delay.

Next, although your code does a great job of defining the update region
correctly, if your paint code doesn't use the update region, there's no
benefit.

You might simply try this:

Add a timer with a period of 150ms.
Change the invalidate code to simply set a boolean dirty flag.
In the timer callback, if the dirty flag is set, invalidate the whole window
and clear the flag. (If you have multiple threads, you should use
Interlocked.Exchange to clear the boolean flag and give you the old value)

This will probably decrease the number of repaints dramatically and
therefore save you a lot of CPU.
>
^^....help me


Mar 10 '08 #3

P: n/a
Jon
"Your requirements should include a minimum spec at which the client responds reasonably."

Even then it can be difficult to predict how fast that system is. Processor speed and memory size
and the other commonly mentioned parameters don't always give the full picture - two apparently
identical PCs can perform differently. You may not even have such a PC to test on.

I agree with your comment about 0.00001%, but I get the feeling that some programmers might just
check to ensure that it's fast enough on their development PC, and not consider that very worthwhile
speed improvements on slower PCs can still be made by making code changes.

I tend to mentally convert phases like "as fast as possible" into "as fast as is reasonably
possible".
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Your requirements should include a minimum spec at which the client
responds reasonably.

It's certainly easier to define that than it is to optimise *forever*.
(There's always going to be something you can do to squeeze an extra
0.0001% of performance out of a system - but it's not a useful thing to
do.)
Mar 10 '08 #4

P: n/a
On Mar 10, 11:24 am, "Jon" <.wrote:
"Your requirements should include a minimum spec at which the client responds reasonably."

Even then it can be difficult to predict how fast that system is. Processor speed and memory size
and the other commonly mentioned parameters don't always give the full picture - two apparently
identical PCs can perform differently. You may not even have such a PC to test on.
While it's true that there are lots of factors to consider, it seems
fairly pointless making performance improvements if you've got no idea
when you're "done". I would say it's important practice to have a test
machine somewhere which is used as a lowest common denominator. It
won't cover *all* bases, but it's a good starting point.
I agree with your comment about 0.00001%, but I get the feeling that some programmers might just
check to ensure that it's fast enough on their development PC, and not consider that very
worthwhile speed improvements on slower PCs can still be made by making code changes.
On the other hand, other programmers tend to waste time on micro-
optimisations which will have no useful effect, and make code harder
to maintain.
I tend to mentally convert phases like "as fast as possible" into "as fast as is reasonably
possible".
But the definition of "reasonably" depends on who you ask, and without
both measurement and a goal, you're likely to waste time optimising
code that isn't really a problem to anyone.

Jon
Mar 10 '08 #5

P: n/a
Jon
Jon: I agree with much of what you say. "Reasonable" to me is a balancing act.

Jon
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:0a**********************************@u69g2000 hse.googlegroups.com...
On Mar 10, 11:24 am, "Jon" <.wrote:
"Your requirements should include a minimum spec at which the client responds reasonably."

Even then it can be difficult to predict how fast that system is. Processor speed and memory size
and the other commonly mentioned parameters don't always give the full picture - two apparently
identical PCs can perform differently. You may not even have such a PC to test on.
While it's true that there are lots of factors to consider, it seems
fairly pointless making performance improvements if you've got no idea
when you're "done". I would say it's important practice to have a test
machine somewhere which is used as a lowest common denominator. It
won't cover *all* bases, but it's a good starting point.
I agree with your comment about 0.00001%, but I get the feeling that some programmers might just
check to ensure that it's fast enough on their development PC, and not consider that very
worthwhile speed improvements on slower PCs can still be made by making code changes.
On the other hand, other programmers tend to waste time on micro-
optimisations which will have no useful effect, and make code harder
to maintain.
I tend to mentally convert phases like "as fast as possible" into "as fast as is reasonably
possible".
But the definition of "reasonably" depends on who you ask, and without
both measurement and a goal, you're likely to waste time optimising
code that isn't really a problem to anyone.

Jon
Mar 10 '08 #6

P: n/a
Jon wrote:
'First thing that will help is to recognize that "as fast as
possible" isn't needed. Just "as fast as is perceptible to the
user".'

Ben: that can be pretty difficult to define, since you don't always
know how powerful the end user's computer is.
The maximum perceptible refresh rate is not computer dependent.

The CPU usage needed to attain that frame rate is.
Mar 10 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.