B4J Tutorial Newton Fractals - all in B4J

USE THE B4J PROJECT IN POST #19 BELOW.

I have converted this Github project to a B4J project (a number of B4J classes in the B4J project to match the classes in the Github project). The project is just about 99% B4J but using a bit of JavaObject for "java.awt.Point" and "java.awt.Color". No other external libraries required.

When the B4J project starts the default formula is z^6 -1. It takes a while to draw the fractal so just be patient until the fractal is displayed. You can also edit the formula in the text fields and then click on button "Generate". You can create some very nice looking fractals (code is in the B4J project so amend it to your liking - probably mostly in class NewtonFractal depending on what you want to accomplish).

There is however something strange (timing wise) that I cannot put my finger on and maybe someone can shed some light on it:
1. When I run the B4J project for the first time it takes a "long" time to create the image (eg z^6 -1)
2. Once the image has been rendered I edit/change the formula (eg to z^8 + 15z^4 - 16z^0 --->>> as per image below) without quitting the B4J project and click on Generate again - the image is rendered much FASTER than on the initial run of the B4J project
3. If I then edit the formula to be the same as the startup formula (z^6 -1) without quitting the B4J project the image is also rendered much FASTER" than when the B4J project runs for the first time.

Why this initial "SLOW" rendering?




1690622052723.png


1690624522123.png


1690624805611.png


1690625952419.png


1690627905594.png


1690629669591.png


1690630398803.png


1690631313821.png


1690631564767.png


1690632467582.png


1690632722301.png


1690632974568.png
 

Attachments

  • b4aNewtonFractalViewer.zip
    7.7 KB · Views: 117
Last edited:

Sandman

Expert
Licensed User
Longtime User
There is however something strange (timing wise) that I cannot put my finger on
This is very far from my expertise, but judging by your description I'd say it sounds as if the actual rendering is fast. It's something initial that is slow. And the only thing I can think of is allocation of memory, setting up a complex array, or something like that. Apologies all around if I'm way off. :)
 

Johan Schoeman

Expert
Licensed User
Longtime User
This is very far from my expertise, but judging by your description I'd say it sounds as if the actual rendering is fast. It's something initial that is slow. And the only thing I can think of is allocation of memory, setting up a complex array, or something like that. Apologies all around if I'm way off. :)
It can probably be sped up using BitmapCreator rather than my primitive rendering process but still need to understand why the initial slow rendering vs rendering after the B4J project ran the first time.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I have experienced first pass slowness in the past and looked at the Java Performance link in Wiki.
Very interesting.

https://en.wikipedia.org/wiki/Java_performance

Especially, Just-In-Time compiling and Split-Bytecode-Verification

Split bytecode verification
Before executing a class, the Sun JVM verifies its Java bytecodes (see bytecode verifier). This verification is performed lazily: classes' bytecodes are only loaded and verified when the specific class is loaded and prepared for use, and not at the beginning of the program. However, as the Java class libraries are also regular Java classes, they must also be loaded when they are used, which means that the start-up time of a Java program is often longer than for C++ programs, for example.
 

Johan Schoeman

Expert
Licensed User
Longtime User

William Lancee

Well-Known Member
Licensed User
Longtime User
I'll look at your code but in all likelihood, my answer will be "I don't know"

You may have to take a page from the performing arts - play an overture,
and hide the first run.
 

Johan Schoeman

Expert
Licensed User
Longtime User
I'll look at your code but in all likelihood, my answer will be "I don't know"

You may have to take a page from the performing arts - play an overture,
and hide the first run.
Open to a sample project that can speed this up. I have not looked into BitmapCreator - I guess it might speed it up
 

klaus

Expert
Licensed User
Longtime User
I played a bit with your program.
The calculation takes about 80000 milliseconds.
The display takes about 128000 milliseconds.
With BitmapCreator the display takes 60 milliseconds, about 2000 times faster !!!
Two remarks about the display:
1. Draw a Circle to display a Pixel is probably very time consuming.
2. You transform the color to ARGB with Dim col(4) As Int = GetARGB(ab(2)).
And then you need to set the color with fx.Colors.ARGB(col(0), col(1), col(2), col(3))
With BitmapCreator no need, ab(2) is the color and can be used directly.

I have not yet looked at the calculation routines.
In the list mylist, you have three values in each item, the x, y coordinates and the color.
Are the x, y coordinates necessary for the calculation ?
If no, maybe an array of Ints containing only the pixel colors could be a soltion.

For the BitmapCreator they are not necessary.

The code for the display with BitmapCreator, it needs an ImageView set as a B4XView instead of the Canvas.

B4X:
    Private bmc As BitmapCreator
    Private x, y, Width, Height As Int
    Width = MainForm.Width
    Height = MainForm.Height
    Log(Width & "  " & Height)
    bmc.Initialize(Width, Height)
    For i = 0 To mylist.Size - 1
        Dim ab(3) As Object
        ab = mylist.Get(i)
        x = i Mod Width
        y = i / Width
        bmc.SetColor(x, y, ab(2))
    Next
    bmc.SetBitmapToImageView(bmc.Bitmap, ImageView1)
 

Johan Schoeman

Expert
Licensed User
Longtime User
I played a bit with your program.
The calculation takes about 80000 milliseconds.
The display takes about 128000 milliseconds.
With BitmapCreator the display takes 60 milliseconds, about 2000 times faster !!!
Two remarks about the display:
1. Draw a Circle to display a Pixel is probably very time consuming.
2. You transform the color to ARGB with Dim col(4) As Int = GetARGB(ab(2)).
And then you need to set the color with fx.Colors.ARGB(col(0), col(1), col(2), col(3))
With BitmapCreator no need, ab(2) is the color and can be used directly.

I have not yet looked at the calculation routines.
In the list mylist, you have three values in each item, the x, y coordinates and the color.
Are the x, y coordinates necessary for the calculation ?
If no, maybe an array of Ints containing only the pixel colors could be a soltion.

For the BitmapCreator they are not necessary.

The code for the display with BitmapCreator, it needs an ImageView set as a B4XView instead of the Canvas.

B4X:
    Private bmc As BitmapCreator
    Private x, y, Width, Height As Int
    Width = MainForm.Width
    Height = MainForm.Height
    Log(Width & "  " & Height)
    bmc.Initialize(Width, Height)
    For i = 0 To mylist.Size - 1
        Dim ab(3) As Object
        ab = mylist.Get(i)
        x = i Mod Width
        y = i / Width
        bmc.SetColor(x, y, ab(2))
    Next
    bmc.SetBitmapToImageView(bmc.Bitmap, ImageView1)
Thanks Klaus. I have shut down for the day but will modify my project tomorrow according to your code above. I browsed some code of @Erel using BC but have not tried to use it. Great that it is so much faster! If I have questions tomorrow about implementing your solution I will post in this thread.
 

Johan Schoeman

Expert
Licensed User
Longtime User
S
Attached the test program, it is yours but with the new new display routine and Logs for the time measurements.
Super - will run it tomorrow. You are a star!
 
Top