Recent

Author Topic: Sleep less then 10ms - avoid CPU 100%  (Read 7057 times)

bambamns

  • Full Member
  • ***
  • Posts: 226
Sleep less then 10ms - avoid CPU 100%
« on: January 19, 2013, 02:35:07 am »
Hi ,

If I make a while , repeat or for loop (it's the same) and count until 1000 inside it - to complete counting it takes around 65 ms to be done and it takes all of CPU utilization (off course it does).
If I put application.processmessages it takes around 340 ms to be done , but it also takes all of CPU utilization (again, of course it does).
IF I add sleep(10); or less then 10 ms it takes 15 600ms to be done, but no CPU utilization is taken (of course it doesn't) - this is what I need - form refresh with application.processmessages and no CPU utilization with sleep() .

So what is the question ?

Well ,
    10 ms * 1000 = 10 000 ms + 340 ms = 10 340 ms which is less then 15 600 ms
      1 ms * 1000 = 1000 ms + 340 ms = 1 340 ms which is , also less then 15 600 ms

How to avoid this kind of problem and reduce time difference, inside of an loop , not using a thread ?

Thx





Lazarus 3.6 on Windows 11

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #1 on: January 19, 2013, 03:18:41 am »
Without threads? A compromise:
Instead of "for i:=0 to 999 do ..." try to separate it to two loops:
Code: [Select]
for j:=0 to 19 do
  begin
    for i:=0 to 49 do
      begin
        k:=j*50+i;
        //task
      end;
    application.processmessages;  //if you need ...
    sleep(10);
  end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

bambamns

  • Full Member
  • ***
  • Posts: 226
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #2 on: January 19, 2013, 03:52:52 am »
Thx,

But the example is just to show that sleep(10) take more time then 10ms and sleep(1) take the same amount.

I have a lot complicated procedures with database commiting which I wont to speed up , but not to use sleep, because it slows down more then 10ms - those procedures are moving a lot of data from one kind of database to another and I (currently) use sleep(10) to avoid CPU utilization, but it is more then 10ms

I tried to make it with using GetTickCount but result is the same.
Lazarus 3.6 on Windows 11

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #3 on: January 19, 2013, 06:04:33 am »
Sleep works at milliseconds level (well, approximately) and depending on your processor speed and other code in the loop (prior or after the Sleep), it might take more than the amount you give. Especially for Application.ProcessMessages, of course it will take a lot. I don't have anything in mind without threads...

circular

  • Hero Member
  • *****
  • Posts: 4471
    • Personal webpage
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #4 on: January 19, 2013, 10:20:15 am »
bambamns, why Blaazen idea does not satisfy you ?

You can also do the same thing without using 2 loops :
Code: [Select]
const
  SleepDivideFactor = 4;
var
  SleepCounter: Integer;
begin
  SleepCounter := 0;
  for ...
  begin
    ...
    Inc(SleepCounter);
    if SleepCounter = SleepDivideFactor then
    begin
      SleepCounter := 0;
      Sleep(10);
    end;
  end;

end;
Conscience is the debugger of the mind

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #5 on: January 19, 2013, 12:01:39 pm »
Why not use TTimer? Like Blaazen said, you need to let application do more stuff before sleeping. There is a difference of 0.000001% cpu utilization and 0.1%, not that most taskmanagers are that accurate. There is no second sleep() or any other function you can call to make it sleep less. Well.. for starters, don't sleep(10), but sleep(1). That is enough to save a lot of cpu.

bambamns

  • Full Member
  • ***
  • Posts: 226
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #6 on: January 20, 2013, 06:45:34 am »
Thx

Quote
There is no second sleep() or any other function you can call to make it sleep less.

This is an answer I was looking for.


Thank you all again
Lazarus 3.6 on Windows 11

ausdigi

  • Jr. Member
  • **
  • Posts: 52
  • Programmer
    • RNR
Re: Sleep less then 10ms - avoid CPU 100%
« Reply #7 on: January 21, 2013, 01:33:19 am »
In Windows the length of the delay when calling Sleep is variable (in older OS versions circa XP it was limited to about 15ms). You can specify a value of 0 (zero), which will usually allow other threads that want to go to go off and get busy. The Windows (MSDN) documentation is not very clear on this but you can work out some things.

As others have commented - I think a timer would be more appropriate for "database commiting which I [want] to speed up".
Win10/64: CT 5.7 32&64

 

TinyPortal © 2005-2018