Extremely slow console/Graphics program [SOLVED]

Windows specific questions.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program

Post by Trinity »

deltarho[1859] wrote:Blimey, in Windows a lot of you guys are using something like 'Sleep 1,1' in loops and are expecting a 1ms Sleep. Sleep is governed by the System clock which defaults to a resolution of 15.625ms.
< SNIP >
Use the above method for any graphics program which uses 'Sleep 1' in a loop.
I am completely new to FB and I am just trying to get what for me is the must basic stuff to work , such as I/O , screen control and what not. I use the code from the manual and examples as basis to learn from and if necessary improved with other peoples suggestions. So not my fault if stuff or code is not up to specs or whatever.
But thank you very much for your kind help and instructions that I will try to digest and learn from.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program

Post by Trinity »

dodicat wrote:In my regulate snippet
Hi Dodicat ,
Sorry , I have been awake like 4 hours and still haven't even managed to get something to eat because I was searching the internet checking up on the windows timer stuff and writing in the forum. I will go over over your regulate snippet code as soon as I can manage it and then I will quote you again and answer.
Thank you :-)
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by deltarho[1859] »

@Trinity
So I ran the Windows Timestamp Project! G_Kernel which set the timer to 1.000 ms
That is using a sledgehammer to crack a nut.

Here is your opening post 'tweaked' with 'StartHiResSleep' - flashing like a treat.<smile>

Code: Select all

#include "windows.bi"
#include "win/mmsystem.bi"
#include "fbgfx.bi"
Using FB ' namespace

#Macro StartHiResSleep
Scope
  Dim As TIMECAPS tc
  TimeGetDevCaps( @tc, Sizeof(tc) )
  TimeBeginPeriod(tc.wPeriodMin)
End Scope
Sleep (16,1) ' Tests have shown that the new resolution will not 'bite' until next 'old' tick
#EndMacro

StartHiResSleep

Dim As Integer W,H
Screeninfo W,H 
W=W-200 : H = H-200
Screenres W,H,,, GFX_NO_FRAME   
Width W\8, H\16
Declare Function Getflash() As Integer
Dim As Integer foo

Do
  
  Locate 8,20   :                                 foo=Getflash
  Locate (10,20,1) :     Print "total return: " & foo
  
  If( foo > 255 ) Then
    Locate (12,20,1) : Print "extended code: " & (foo And &hff)
    Locate (14,20,1) : Print "regular code: " & (foo Shr 8)
  Else
    Locate (12,20,1) : Print "regular code: " & (foo)
  End If
  Print
Loop Until foo = 27


Function Getflash() As Integer
  #define mk(a,b) a Or b Shl 8
  Dim As String s,c,i
  Static As Long ctr
  Dim As Integer p=Pos,cl=Csrlin
  While Len(Inkey):Wend
    Do
      i=Inkey
      ctr+=1
      Locate cl,p
      If ctr Mod 200=0 Then c ="__"
      If ctr Mod 400=0 Then c= "  ":ctr=0
      Print c
      If Len(i) Then Cls: Return mk(i[0],i[1])
      Sleep 1,1
    Loop
    End Function 
The System clock has it's resolution changed to 1ms. This is system wide change but will only persist for as long as the process which changes it continues. As soon as your code terminates the System clock will return to 15.625ms.

This what my TimerRes application tells me whilst your code is running.

Image

Ignore the Title Bar - my system has been tweaked with regard the buttons - my windows are usually much wider.

I shall post a download link to TimerRes shortly. It is better than the SysInternals offering because it does more than monitor the resolution.
Trinity wrote:I am completely new to FB
So am I - came here in January. It is a cracking BASIC but can be a right PITA sometimes.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by deltarho[1859] »

Here is a zip of TimerRes.exe.

TimerRes

The 0.5ms setting will actually give 1ms for Windows 7 and earlier.

It should work from Windows NT onward, including Windows 10. The Windows 10 version utilises the continuous function aspect but has been configured to being discrete with resolutions from 0.5ms, 1ms, 2ms, ..., 14ms, 15ms, 15.625ms. If anyone wants the Windows 10 version - just shout.

TimerRes has a property sheet and is copyrighted.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by Trinity »

deltarho[1859] wrote:Here is your opening post 'tweaked' with 'StartHiResSleep' - flashing like a treat.<smile>
Thank you very much , that did the trick. I had actually just made a more deeper read of your previous post and had tested the code while I began eating my very late breakfast (like half past two here now) and am still eating. And found out that it worked.
deltarho[1859] wrote:I shall post a download link to TimerRes shortly. It is better than the SysInternals offering because it does more than monitor the resolution.
Thank you
deltarho[1859] wrote:So am I - came here in January. It is a cracking BASIC but can be a right PITA sometimes.
Yes , the process as new apparently is a bit complicated at times :-D
deltarho[1859] wrote:Here is a zip of TimerRes.exe.
Downloaded! Thank you very much that was both kind and generous of you :-)
deltarho[1859] wrote:If anyone wants the Windows 10 version - just shout.
TimerRes has a property sheet and is copyrighted.
I have two old PCs donated to me by my brother and I have the free Windows 10 upgrade on them so I would only be happy to also have a version that will run on Windows 10.
Feel free to send me a link using the send email function on profile page if you prefer that to posting a link in the forum. :-)
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by deltarho[1859] »

With regard the System clock it is worth mentioning that Windows employs the principle that the current timer interval is the smallest timer interval chosen by any open process.

If we run TimerRes and choose, say, 1.0ms then the resolution will change to 1.0ms. If we now run another instance of TimerRes and choose, say, 10ms it will be ignored as the first instance is 'in the chair', so to speak. If we close the first instance then the resolution will change to 10ms because that is what was requested by the second instance.

If we now close the second instance and reopen TimerRes again we will get 15.625ms; assuming that another process has not requested a smaller resolution.

This is pretty clever stuff and the guys at Microsoft who developed this should get mentioned in dispatches.

If a process in Windows 10 chooses a resolution of 4ms, say, then TimerRes will mention it as it is using the 'old technology' where a 4ms resolution is not available. It won't crash but will display a message asking you to tell me. <laugh>. This is why I wrote a Windows 10 version. I will post a link to the Windows 10 version later today.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program

Post by Trinity »

dodicat wrote:Do you still get 150 framerate?
Sorry , no! , it only gives 65 frames in that number shown at the top left of Window .
Your code is slightly complicated for me to read (just saying it will take time to fully grasp). So I think that I will try to keep the improvement using your original code suggestion (the MOD thing) and then adding the FXM & deltarho[1859] code referred to by deltarho[1859].
(Macros from here : https://freebasic.net/forum/viewtopic.p ... od#p230497 )

By the way , the TimerRes by deltarho[1859] is extremely useful trying to troubleshoot here because it allows for changing timer on the fly with just one mouse click , and doing so , changing timer from 15.6 ms to 1 ms , made your timer thing work with 150 frames as number shown in top left corner.

Thank you very much for your help here :-)
Last edited by Trinity on Oct 10, 2017 13:34, edited 1 time in total.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by Trinity »

deltarho[1859] wrote:With regard the System clock it is worth mentioning that Windows employs the principle that the current timer interval is the smallest timer interval chosen by any open process.
< SNIP >
This is why I wrote a Windows 10 version. I will post a link to the Windows 10 version later today.
Nice to know :-) I will not pretend to know much about basic Windows internals , and it is not often that I learn new stuff about it but today , clawing my way through trying to learn basic FreeBASIC ;-) , then I learned something new about the clock timer thing.
Thank you :-)

P.S.
I thought that maybe this would interest you (includes two links to some code , if you want to compare notes) :
https://randomascii.wordpress.com/2013/ ... ts-wasted/
(1: TimerOverhead.cpp : https://github.com/randomascii/blogstuf ... erhead.cpp )
(2: TimerFrequencyRaising.cpp : https://github.com/randomascii/blogstuf ... aising.cpp )
Last edited by Trinity on Oct 10, 2017 13:24, edited 2 times in total.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by Trinity »

P.S.
I might as well ask here and now as any other place , so :
When I want to post my version of the Inkey flashing cursor function , then where would be a good place to do it ? Tips and Tricks ? , or is there a better place where it would be easier to pick up and more visible to other (intermediate) beginners that might want to use it ?
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by deltarho[1859] »

With the Windows 10 version I have used a minmalist approach and on opening we get this:

Image

To change the resolution we simply right click on the title bar to be presented with a list of presets when we choose 'Set'.

You will notice that the 'Current timer interval' is not perfect but we are in the micro-second domain.

Clicking on 'Revoke' cancels our request and returns the resolution to whatever it was before our request.

'TimerInterval (Win10)' is a little smarter than TimerRes. If we request a resolution which is greater than that set by another process we will be advised and told that our request has been noted. If we minimise the application then monitoring is pointless and will be stopped - and we get told. On restoring, monitoring will be restarted - and we get told.

On closing the application we will be briefly advised what the current resolution will be after closure.

I have written applications which check the host operating system. TimerInterval (Win10) is not one of them. If I went 'commercial' with it then I would do that. So, don't run it on OS < Win10.

Property sheet and copyrighted.

TimerInterval (Win10)

@Trinity

I had read the 'Megawatts wasted' article. That was written four years ago and browsers have improved since. Checked the other two links - nothing new for me.

@All

I am not one for blowing my own trumpet but don't have a problem doing so. <smile> As far as I know 'TimerInterval (Win10)' is the only application on the planet which leverages Windows 10 behaviour in this respect. This new approach was probably developed for some aspects of Windows 10 and, for some reason, Microsoft decided that there was no need to 'go public'. Perhaps Mark Russinovich doesn't even know.
Last edited by deltarho[1859] on Oct 10, 2017 14:34, edited 1 time in total.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by Trinity »

deltarho[1859] wrote: Windows 10 version <SNIP>
'TimerInterval (Win10)' is a little smarter than TimerRes.
Property sheet and copyrighted.
Thank you very much , I've saved that to drive for if I get to use FB on the older PCs with Win10. (I find Win 10 inferior to Win 7 when it comes to run PC games, among other then that's why I'm stuck with Win 7 on this rig)
deltarho[1859] wrote:I had read the 'Megawatts wasted' article. Checked the other two links - nothing new for me.
Didn't really expect you to learn anything from it but no harm done in presenting the links to you (and at the same time to other readers ;-) )
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by dodicat »

Trinity
You get full 150 frames per second when online.
You get about 65 frames per second when offline.
Same here.

My regulate function returns the time to sleep to achieve your requested frame rate.
All the rest of the code is concerned with the moving ball, which is not needed, it is only an extra visual.

The regulate function also returns the parameter fps, which is the running framerate, which can be printed somewhere if wanted.

Code: Select all





Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,_lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

dim as long MySpeed=70 '< ---------------- Adjust Here 

screen 18

dim as long x=10,y=10,dx=1,dy=1 'stuff for the ball only

dim as long sleeptime,fps
do
    '======= ball motion ==========
    x+=dx
    y+=dy
    if x<10 or x>630 then dx=-dx
    if y<10 or y>470 then dy=-dy
    '==================
    screenlock
    cls
    print "FrameRate ";fps 'returned from Regulate via the fps parameter.
    
    circle(x,y),10 'draw the ball
    
    draw string(200,200),"Sleep " &sleeptime &" , 1"
    screenunlock
    sleeptime=regulate(MySpeed,fps) 'set in your requested speed and the long fps 
    sleep sleeptime ,1         '<------------ Used here
    loop until len(inkey)
    

      

Both our computers seem to suffer from the online booster syndrome.
The cure is Ebay, I would say.
The regulate function is independent of timer intervals. So long as a second is the S.I. recommendation, then it will do the job.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by deltarho[1859] »

Trinity wrote:Didn't really expect you to learn anything from it but no harm done in presenting the links to you
Don't think twice about it.

Nearly everything I learnt about this topic was got from 'trial and error' and my 'What if' approach to coding. There is no mention of the presets in TimerRes at Microsoft. I wrote a lot of code which did nothing but test the system. The MSDN site is huge so it is understandable that some areas are bereft of information. Having said that they do have many teams but as with many large organisations all the teams are not in communication with all others. If they did then new versions of Windows would probably never get past the front door.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED] for now !

Post by Trinity »

dodicat wrote:You get full 150 frames per second when online. You get about 65 frames per second when offline. Same here.
Very sorry , my bad , please excuse my confusion , I thought that I had written it. My computer , with timer = 15.6 ms ran at 65 FPS both on an off-line .
The only cure were to set timer to 1ms using deltarho[1859]'s TimerRes program or to use the FXM code & Macro by deltarho[1859] referred to by deltarho[1859] .
dodicat wrote:The regulate function is independent of timer intervals. So long as a second is the S.I. recommendation, then it will do the job.
I will have to study your code more later :-)
Thank you
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Extremely slow console/Graphics program [SOLVED]

Post by srvaldez »

hello Trinity
compiling with -exx option is good for catching some bugs but it slows the execution time, chasing after timer is a waste of time.
Post Reply