Extremely slow console/Graphics program [SOLVED]

Windows specific questions.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

@Trinity

I added a bit to my last post with regard side effects.

Nobody has spotted the fact that my TimerRes 'gizmo' and 'TimerInterval (Win10)' have a resolution of 0.5ms when timeBeginPeriod cannot go below 1ms. The reason is that I don't use timeBeginPeriod but a function lower down in the system - the one that timeBeginPeriod uses. Anyway, I won't go into details - it is mentioned in the first link of my last post. I read that some years go but see that it was updated in February 2017. Thanks for reminding of that paper. I have tested my system and I can get 0.5ms but I doubt that I will ever use it; although Microsoft Edge seems to.
my experiments showed me that
Often worthwhile doing as we sometimes learn something that our OS vendor has not got around to telling us. <smile>

Added: Just had a thought. When my gizmos are minimised it may be worthwhile to have a message box pop up along the lines "Sorry to bother you, but you have been using a 1ms resolution for the last three hours and were probably wondering why the system had become sluggish". There again, maybe not. <Ha,Ha>
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

BTW, when I mentioned 'Sleep 100' above I should have written 'Sleep 100,1'. 'Sleep 100' will take longer because it is keeping it's eyes open for a key press and that, it seems, is not free.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

Yours truly wrote:grindstone's code is a good solution for a flashing cursor.
Firstly, grindtone's code is not OS specific and, secondly, it does not require altering the System Clock resolution.

It is worth noting that if we comment 'Sleep 1' then we have a tight loop which will hold on to the time-slice (quantum) with a potential overloaad of the CPU. Process Hacker tells me that the CPU load is about 5%. Obviously this is far too high for just flashing a cursor. Allowing 'Sleep 1' back into the code sees the CPU load drop to about 0.04%, So, there is a lesson to be learned there.

One of the loads on the CPU is reading Timer. It occurred to me that we can relieve the CPU of that if we passed the responsibility of timing to a multimedia timer ie from software to hardware.

The following code uses the Windows API timeSetEvent. This gives grindstone's code a higher ranking. My saving grace is that we are in the Windows sub-forum. <smile>

I have 'pinched' grindstone's cursor displaying code in timeSetEvent's callback function with cursoron as a Static.

I have requested a resolution of 20ms. We will not notice that with a 500ms blink. See timeSetEvent for details.

The CPU load is now 0.02%.

So, we don't have a grindstone replacement but simply another way.

Code: Select all

#include once "windows.bi"
#Include Once "win/mmsystem.bi"
 
Dim As Uint blinkingtime
Dim TimerID As Uint_Ptr
 
Sub BlinkProc( uTimerID As Uint, uMsg As Uint, dwUser As Dword, dw1 As Dword, dw2 As Dword)
Static cursoron As boolean
  Locate Csrlin, Pos - 1, 0 'set cursor one position left
  cursoron = Not cursoron 'toggle cursor on/off
  Print Iif(cursoron, " ", "|"); 'print actual cursor
End Sub
 
Locate 10,20,0
blinkingtime = 500
TimerID = timeSetEvent( blinkingtime, 20, Cast( LPTIMECALLBACK, @BlinkProc ), 0, TIME_PERIODIC )
 
Sleep
 
timeKillEvent TimerID
 
Sleep
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Extremely slow console/Graphics program [SOLVED]

Post by grindstone »

Worthwhile approach, I'll keep it in mind.
MSN wrote:Note This function is obsolete. New applications should use CreateTimerQueueTimer to create a timer-queue timer.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

I have read that CreateTimerQueueTimer has issues and for straight forward jobs some prefer timeSetEvent.

timeSetEvent is very easy to use. Have a look at Using Timer Queues. What a 'dogs dinner' that is. <laugh>
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Extremely slow console/Graphics program [SOLVED]

Post by grindstone »

I agree.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

Strictly, the last three parameters of BlinkProc should be Dword_Ptr. Not that it matters with the last two as they are reserved but the third from last is not.

We can have further fun and games if we made use of the fouth parameter of timeSetEvent.
Post Reply