Extremely slow console/Graphics program [SOLVED]

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

Re: Extremely slow console/Graphics program [SOLVED]

Post by Trinity »

srvaldez wrote:chasing after timer is a waste of time.
Sorry , I do not understand exactly what your comment is aimed at expressing (?)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Extremely slow console/Graphics program [SOLVED]

Post by dodicat »

Trinity
If you want to use a resolution of 1 ms then:

Code: Select all


#include "windows.bi"
#include "win/mmsystem.bi"

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=300 '< ---------------- 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 
    
    'wrap up sleep 
    timeBeginPeriod(1)
    sleep sleeptime ,1         '<------------ Used here
    timeEndPeriod(1)
    
    loop until len(inkey)
    

 
But it is windows only.
Better I think to assume sleep 1,1 sets about 64 fps max i.e. (assume no web page resetting resolution)
This way you can write graphical applications to suit all.
Use some sort of regulator to fix the outgoing framerate <= 64, to suit your program.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Extremely slow console/Graphics program [SOLVED]

Post by srvaldez »

the slowdown on execution time is due to compiling with -exx and nothing to do with timer.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

The last code by dodicat is Windows only because it uses timeBeginPeriod(1)/timeEndPeriod(1) as do my macros. The (1) part is changing the timer resolution to 1ms.

Running TimerRes we can see the resolution oscillating between 1ms and 15.625ms. This is due to the Windows functions being invoked around 'sleep sleeptime , 1' with the rest of the do/loop 'ticking over at 15.625ms.

There is something which I have not mentioned yet and that is a changed resolution does not occur until the next clock tick. MIcrosoft does not mention this either at MSDN. So, timeBeginPeriod(1) may not bite until up to 15.625ms have elapsed if we are dropping down from 15.625ms. In my macro you will see

Code: Select all

Sleep (16,1) ' Tests have shown that the new resolution will not 'bite' until next 'old' tick
This is not important in dodicat's code because of 'sleeptime' but in some GUI work where we need a resolution change to be immediate then it is best to get that 'delay before kicking in' out of the way earlier in the code where its effect will been absorbed.

Added: I am sure that I don't need to mention this but if we have, say, 'Sleep 500' in some code for some reason then changing the timer interval will not make a blind bit of difference unless we needed to get very close to 500. Timer interval considerations normally only come into play as we approach the 15.625ms default resolution; beyond which we go 'below the radar', so to speak.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED]

Post by Trinity »

@ deltarho[1859] , dodicat , srvaldez

Thank you very much for good explanations , your help and kind comments :-)
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

Slightly off topic but it has been a while since I tested browsers with TimerInterval. On board I have Microsoft Edge, Internet Explorer 11 and FlashPeak Slimjet (based on Chromium (64 bit)). My OS is Windows 10. Needless to say only one browser was loaded at a time.

Whilst reading a page all of them were using a resolution of 15.625ms, with occasional uses of a higher resolution. Browsers use a lot of threads and, no doubt, some threads are written to use a higher resolution.

During navigation both Edge and IE switched to 4ms. Slimjet, on the other hand, switched to 1ms.

Edge was interesting because whilst reading I noticed resolutions of 0.5ms and 2ms being used; not so with IE and Slimjet. It would seem then that the Edge designers have put a little more thought into what timer interval to use for the different threads.

This is very different to a few years ago when browsers would use a high resolution for the lions share of a browsing session.

Whilst dong this my email checker told me that I had some mail. I use 'eM Client' and on opening that I noticed that the resolution changed to a steady 1ms. I closed the open browser and closed my email client. On opening my email client again the resolution changed to a steady 1ms.

I stopped using the Mozilla Thunderbird email client when some of the computing press advised that the issuing of security patches was poor. I should add that Thunderbird is now 'under new management'. Mozilla admitted that most of it's resources were being employed on Firefox. Anyway, a friend of mine uses Thunderbird and has a copy of TimerInterval. Thunderbird does not change the resolution.

A few years ago a PowerBASIC forum member reported that some of his PB applications ran faster when Microsoft Solitaire was open. At the the time I was using TimerRes on my Windows 7 machine. It transpired that Solitaire was using a resolution of 1ms.

As they say in America, go figure. <smile>
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Extremely slow console/Graphics program [SOLVED]

Post by dodicat »

It seems to depend on which particular page.
a FreeBASIC forum page is guaranteed 1 ms. in chrome.
And I know from my last computer that running a Java graphics snippet guarantees 1 ms.
(I had a little Java graphics thingy which i used when offline)
So, perhaps the web page must be running java to get the 1 ms. resolution.
But how can you detect if a web page is actually using java somewhere?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Extremely slow console/Graphics program [SOLVED]

Post by dodicat »

Of course, view page source.
Silly me.
Yes, the forum page runs java code.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

dodicat wrote:a FreeBASIC forum page is guaranteed 1 ms. in chrome.
I don't have Chrome so I cannot dispute that but, if true, then that is bad news for anyone using battery power on their laptop. My PC uses about 30% more power with a 1ms resolution compared with 15.625ms.

With Edge and Slimjet, with my arms folded, my resolution is 15.6260ms at a forum page. If I start scrolling Slimjet 'ups the ante' to 1ms. Edge, on the other hand, does not change the resolution.

I don't allow Java on my system. This page is running JavaScript but only because I allow it via ScriptBlock.

BTW, the reason why TimerInterval is showing 15.6260ms and not 15.6250ms is because the timers are calibrated during a boot up. Calibration is not allowed too much time otherwise it would affect the boot time and Microsoft will not entertain that. Learnt that yesterday in a Microsoft paper. They will not be getting any complaints from me for a one micro-second 'error'. <smile>
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Extremely slow console/Graphics program [SOLVED]

Post by dodicat »

Here firefox and chrome set the resolution to 1 ms.
Microsoft edge and internet explorer don't
I don't have the java runtime, but I see a javascript dll file.
So, java script, I reckon, in invoked by chrome and firefox and not the others.
Dependency walker doesn't seem to work here, so I cannot say for sure.
I'll maybe try using the java script dll via disphelper, to get it fired up offline, and test the resolution.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

dodicat wrote:Here firefox and chrome set the resolution to 1 ms.
That really surprised me. I thought that all browsers were behaving themselves nowadays. There is no reason why a 1ms resolution should be imposed upon us for the whole of a browsing session.

Good for Microsoft.

Added: I can now understand why going online made a difference to some FB graphics code.

I do not think that javascript alters the system clock's resolution when it is invoked. Many web sites use Javascript and will not function as intended if not allowed to run. Each time that I go to a website which I have never visited before Javascript is automatically disabled by my ScriptBlock extension ( designed for Chrome but also works with SlimJet ). The Firefox equivalent is NoScript. Only when I reckon it is is safe to do will I allow Javascript to execute. It can be allowed temporarily or permanently. I have just gone to a website where I allow Javascript and toggled between allowing and disallowing and there was no change in the resolution.

It seems to me then that it is the browsers, Chrome and Firefox, who are setting the resolution and not Javscript. If Edge or Slimjet did not invoke Javascript then a lot of websites would not work properly.

I went to a news site and the resolution was changed to 1ms. That had me foxed for a wile until I scrolled down and saw a video clip running. That page has changed the resolution for the video.

We go to YouTube to watch videos. The resolution is immediately changes to 1ms and it stays at that during the whole of the YouTube session.

Have a look at Slimjet. It will use 15.625ms as much as it can. It has an inbuilt ad blocker, but that can be disabled on a per site basis. I use one news site which refuses to to give me all it can unless I turn off ad blocking. I gave in and allowed it. <smile>. Although built from Chromium it does not 'report back to mother' as Chrome does. It also has anti-tracking built in. Scroll down the home page and you will find a comparison with IE, Chrome and Firefox. It is blinding fast. I have been using it for a couple of years now. I used Firefox for many years, moved onto Chrome then Slimjet. If Slimjet ever changed to a 'paid for' product I would pay, and getting me to open my wallet is no easy task! I am as canny with money as the Scots are. Who said that? <smile>
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

Just found out that some browsers, including Chrome, notice when battery power is being used and switch to the default resolution. This subject is bigger than I thought it was.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED]

Post by Trinity »

Update :
For best solution to my "Extremely slow console/Graphics program" problem please look at the code for the light version of my CallInkey function for key input with flashing cursor on graphics screen (this solution ought to be independent of Windows Timer Resolution) : viewtopic.php?f=7&t=26018&p=237707#p237707
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Extremely slow console/Graphics program [SOLVED]

Post by deltarho[1859] »

@Trinity

Well, it looks good but it is very CPU intensive. Polling Timer on a modern Windows machine is effectively polling the Performance Counter.

On my machine, Task Manager tells me that CallInkey is using about 13% CPU time. In fact, one of the cores on my i7 is maxed out.

The beauty of Sleep is (from MSDN): "This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds."

In other words no CPU time.

The 'best' solution then is to use an accurate Sleep and not hammer the CPU by polling a timer.

Imagine your favourite IDE consuming 13% CPU time just to flash a cursor.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Extremely slow console/Graphics program [SOLVED]

Post by Trinity »

deltarho[1859] wrote:The 'best' solution then is to use an accurate Sleep and not hammer the CPU by polling a timer.
< SNIP >
Imagine your favourite IDE consuming 13% CPU time just to flash a cursor.
I think that you are right , the load n my CPU is also 10 to 12 % (i7) , but there is no satisfying everybody , so unless I put up two versions and let people pick then then you will have the camp leaning towards never use sleep timer or the camp never use CPU intensive not being happy, so the basic problem is the same as it has been for all those years passed and that is the no cursor in graphics mode....

In an attempt to cater to most then I have but back the "SLEEP, Macro, Timer Resolution" dependent versions also :-)
Post Reply