To demonstrate the benefit of having a built-in C compiler to the common BASIC programmer, here's a quick rundown of how to speed up @SanguineBrah's brotexplorer by a factor of 5 with these steps:
- Replace this BASIC code:
iteration=0
WHILE x*x+y*y<=(2*2) AND iteration<maxiteration
xtemp=x*x-y*y+x0
y=2*x*y+y0
x=xtemp
iteration=iteration+1
WEND
IF iteration<>maxiteration THEN c=iteration ELSE c=0
with this native procedure call:
c = |iterbrot(#x0, #y0, #x, #y, maxiteration)
- Write a 1:1 translation of the BASIC code into C and save it in a file, say "bexturbo.c":
int iterbrot(double x0, double y0, double x, double y, int maxiteration)
{
int iteration = 0;
double xtemp;
while (x*x + y*y <= 4 && iteration < maxiteration) {
xtemp = x*x - y*y + x0;
y = 2 * x * y + y0;
x = xtemp;
++iteration;
}
if (iteration >= maxiteration)
return 0;
else
return iteration;
}
- Write a loader. This is required because native calls like
|iterbrot ...
can only be tokenized correctly when the symbol that is called already exists.
TCC "bexturbo.c"
TCCLINK "bexturbo"
CHAIN "bexturbo_imp.bas"
- Done.
You can find both the interpreted and the sped-up version in the basicengine-demos repo. (@SanguineBrah: please shout if you don't approve of this)
I have also modified it to do incremental rendering for increased responsiveness.