Have a loop in QB64 concerning loop optimization:
DIM N AS DOUBLE, X(100000000) AS DOUBLE
T! = TIMER
FOR N = 1 to 100000000
IF X(N) THEN
PRINT X(N)
EXIT FOR
END IF
NEXT
PRINT TIMER - T!
is it any faster than:
DIM N AS DOUBLE, X(100000000) AS DOUBLE
T! = TIMER
FOR N = 1 to 100000000
IF X(N) <> 0 THEN
PRINT X(N)
EXIT FOR
END IF
NEXT
PRINT TIMER - T!
EDITED: 09-18-2018 to include variable types
I written this code to evaluate your test:
The two different routines are inserted into two functions:
TRYA
andTRYB
.I launched this SW with a loop that runs 999 times the functions and the result is:
then I launched with a 10 times loop and the result is:
then I launched with a 15 times loop and the result is:
Cause we launch the SW in a multi-thread ambient I don't believe this is a very good test, but there's some results:
TRYA
is slower.TRYB
is slower.Looking at these results, I think, we may consider the two functions equivalent!
You obtain more than 10 loops running the code from command line (or modifying the command$ parameter into the QB64 menu) as:
# ./test n
Where
n
is the number of loops you desire.The SW was compiled using gcc with -O3 optimizations option. (To do this you have to modify the file
[/opt/]qb64/internal/c/makeline_lnx.txt
)