LIMIT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (QBasic capitalisation)
Tag: visualeditor
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_LIMIT}}
{{DISPLAYTITLE:_LIMIT}}
The [[_LIMIT]] statement sets the loop repeat rate of a program to so many per second, relinquishing spare CPU cycles to other applications.  
The [[_LIMIT]] statement sets the loop repeat rate of a program to so many per second, relinquishing spare CPU cycles to other applications.




{{PageSyntax}}  
{{PageSyntax}}
: [[_LIMIT]] {{Parameter|framesPerSecond!}}
: [[_LIMIT]] {{Parameter|framesPerSecond!}}


Line 11: Line 11:
* _LIMIT measures its interval from the previous time that it was called and minor adjustments are automatically made to ensure that the number of times a loop is repeated is correct overall.
* _LIMIT measures its interval from the previous time that it was called and minor adjustments are automatically made to ensure that the number of times a loop is repeated is correct overall.
* Loop cycle rates of 1000 or less can '''significantly reduce CPU usage''' in programs.
* Loop cycle rates of 1000 or less can '''significantly reduce CPU usage''' in programs.
* Do not use it to limit a loop to '''less than once every 60 seconds'''(.0167) or an [[ERROR Codes|ILLEGAL FUNCTION CALL error]] will occur.
* Do not use it to limit a loop to '''run less than once every 60 seconds''' (i.e. _LIMIT .0167 or _LIMIT 1/60) or an [[ERROR Codes|ILLEGAL FUNCTION CALL error]] will occur.
* Do not use _LIMIT as a timing delay outside of loops. Use [[_DELAY]] instead.
* Do not use _LIMIT as a timing delay outside of loops. Use [[_DELAY]] instead.
* Use _LIMIT to slow down old QBasic program loops that run too fast and use too much CPU.
* Use _LIMIT to slow down old QBasic program loops that run too fast and use too much CPU.
Line 18: Line 18:
{{PageExamples}}
{{PageExamples}}
''Example:'' Limits loop execution to 30 frames per second and limits the program's CPU usage.
''Example:'' Limits loop execution to 30 frames per second and limits the program's CPU usage.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|PRINT}} "To Quit press ESC key!"
{{Cl|PRINT}} {{Text|<nowiki>"To Quit press ESC key!"</nowiki>|#FFB100}}
{{Cl|DO}}
{{Cl|DO}}
  {{Cl|_LIMIT}} 30
    {{Cl|_LIMIT}} {{Text|30|#F580B1}}
  {{Cl|PRINT}} {{Cl|CHR$}}(26);
    {{Cl|PRINT}} {{Cl|CHR$}}({{Text|26|#F580B1}});
  {{Cl|IF...THEN|IF}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|EXIT DO}} '' ''
    {{Cl|IF}} {{Cl|INKEY$}} = {{Cl|CHR$}}({{Text|27|#F580B1}}) {{Cl|THEN}} {{Cl|EXIT DO}}
{{Cl|LOOP}}  
{{Cl|LOOP}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}

Latest revision as of 07:21, 13 April 2023

The _LIMIT statement sets the loop repeat rate of a program to so many per second, relinquishing spare CPU cycles to other applications.


Syntax

_LIMIT framesPerSecond!


  • The framesPerSecond! SINGLE parameter value adjusts the loops per second of a program loop. Do not use negative values.
  • The loop code is executed before the loop is delayed. Loop cycles below once per second may delay program _EXITs.
  • _LIMIT measures its interval from the previous time that it was called and minor adjustments are automatically made to ensure that the number of times a loop is repeated is correct overall.
  • Loop cycle rates of 1000 or less can significantly reduce CPU usage in programs.
  • Do not use it to limit a loop to run less than once every 60 seconds (i.e. _LIMIT .0167 or _LIMIT 1/60) or an ILLEGAL FUNCTION CALL error will occur.
  • Do not use _LIMIT as a timing delay outside of loops. Use _DELAY instead.
  • Use _LIMIT to slow down old QBasic program loops that run too fast and use too much CPU.


Examples

Example: Limits loop execution to 30 frames per second and limits the program's CPU usage.

PRINT "To Quit press ESC key!"
DO
    _LIMIT 30
    PRINT CHR$(26);
    IF INKEY$ = CHR$(27) THEN EXIT DO
LOOP
To Quit press ESC key!
→→→→→→→→→→→→→→→→→→→→
Note: In the above example, _LIMIT has to be within the loop.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link